title: Simple R function issue
body: <p>I've written a very long winded R function that I'm sure can be recreated in 2 lines or less, but I just can't get a handle on how as yet.</p>
  
  <p>I pass my function a dataframe of greyhound result data and an integer representing the distance run.
  It then returns the winning percentage for each box number (8 boxes in total) over that distance.</p>
  
  <p>Currently:</p>
  
  <pre><code>boxPercents &lt;- function(dist, data) {
  
  ## get each box num of wins
  x1 &lt;- data$position == 1 &amp; data$distance == dist &amp; data$box == 1
  x2 &lt;- data$position == 1 &amp; data$distance == dist &amp; data$box == 2
  x3 &lt;- data$position == 1 &amp; data$distance == dist &amp; data$box == 3
  ...
  x8 &lt;- data$position == 1 &amp; data$distance == dist &amp; data$box == 8
  
  ## count the total num of races at that 
  numRaces &lt;- data$position == 1 &amp; data$distance == dist
  
  ## print out the winning percent for each box
  print(sum(x1) / sum(numRaces))
  print(sum(x2) / sum(numRaces))
  print(sum(x3) / sum(numRaces))
  ...
  print(sum(x8) / sum(numRaces))
  
  }
  </code></pre>
  
  <p>My output is like the below which I then convert to a vector:</p>
  
  <pre><code>[1] 0.2452107
  [1] 0.1340996
  [1] 0.09961686
  [1] 0.1034483
  [1] 0.08045977
  [1] 0.1034483
  [1] 0.09961686
  [1] 0.1340996
  </code></pre>
  
  <p>I'm pretty certain one of the apply functions is what I'm supposed to be using but all efforts have been fruitless.</p>
  
  <p>Edit: Here's the header of the data:</p>
  
  <pre><code>       track      date race position box             name   sp  fave distance
   Warrnambool 02 Jan 14    1        1   1       TOP SECRET  1.7  true      450
   Warrnambool 02 Jan 14    1        2   4     FLASH WILSON  4.7 false      450
   Warrnambool 02 Jan 14    1        3   8 HEAPS OF ABILITY 11.8 false      450
   Warrnambool 02 Jan 14    1        4   7   OCCUPATION LAD 24.1 false      450
   Warrnambool 02 Jan 14    1        5   2   HE'S A VILLIAN 19.3 false      450
   Warrnambool 02 Jan 14    1        6   5 ZAC'S A SIXPENCE  9.7 false      450
  </code></pre>