title: How to run a for loop for each group in a dataframe?
body: <p>This question is similar to <a href="https://stackoverflow.com/questions/25314336/extract-the-maximum-value-within-each-group-in-a-dataframe">this one asked earlier</a> but not quite. I would like to iterate through a large dataset (~500,000 rows) and for each unique value in one column, I would like to do some processing of all the values in another column.</p>
  
  <p>Here is code that I have confirmed to work:</p>
  
  <pre><code>df = matrix(nrow=783,ncol=2)
  counts = table(csvdata$value)
  p = (as.vector(counts))/length(csvdata$value)
  D = 1 - sum(p**2)
  </code></pre>
  
  <p>The only problem with it is that it returns the value <code>D</code> for the entire dataset, rather than returning a separate <code>D</code> value for each set of rows where <code>ID</code> is the same.</p>
  
  <p>Say I had data like this:<br>
  <a href="https://i.stack.imgur.com/IsFaq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IsFaq.png" alt="enter image description here"></a></p>
  
  <p>How would I be able to do the same thing as the code above, but return a D value for each group of rows where <code>ID</code> is the same, rather than for the entire dataset? I imagine this requires a loop, and creating a matrix to store all the D values in with <code>ID</code> in one column and the value of <code>D</code> in the other, but not sure.</p>