title: Calculating column means based on values in another column
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-vs-by-vs-aggrega">R Grouping functions: sapply vs. lapply vs. apply. vs. tapply vs. by vs. aggregate vs.</a>  </p>
  </blockquote>
  
  
  
  <p>I'm using R and would love some help with a problem I'm having:</p>
  
  <p>I have a dataframe (<code>df</code>) with a column ID and a column Emotion. Each value in ID corresponds with 40-300 values in Emotion (so it's not a set number). I need to calculate the mean of all i's in <code>Emotion</code> for each j in <code>ID</code>. So this is what the data looks like</p>
  
  <pre><code>df$ID = (1, 1, 1, 1, 2, 2, 3)
  df$Emotion = (2, 4, 6, 4, 1, 1, 8)
  </code></pre>
  
  <p>so the vector of means should look like this: <code>(4, 1, 8)</code></p>
  
  <p>Any help would be greatly appreciated!</p>