title: Loop or apply for sum of rows based on multiple conditions in R dataframe
body: <p>I've hacked together a quick solution to my problem, but I have a feeling it's quite obtuse. Moreover, it uses for loops, which from what I've gathered, should be avoided at all costs in R. Any and all advice to tidy up this code is appreciated. I'm still pretty new to R, but I fear I'm making a relatively simple problem much too convoluted.</p>
  
  <p>I have a dataset as follows:</p>
  
  <pre><code>id  count   group
  2   6   A
  2   8   A
  2   6   A
  8   5   A
  8   6   A
  8   3   A
  10  6   B
  10  6   B
  10  6   B
  11  5   B
  11  6   B
  11  7   B
  16  6   C
  16  2   C
  16  0   C
  18  6   C
  18  1   C
  18  6   C
  </code></pre>
  
  <p>I would like to create a new dataframe that contains, for each unique ID, the sum of the first two counts of that ID (e.g. 6+8=14 for ID 2). I also want to attach the correct group identifier. </p>
  
  <p>In general you might need to do this when you measure a value on consecutive days for different subjects and treatments, and you want to compute the total for each subject for the first x days of measurement.</p>
  
  <p>This is what I've come up with:</p>
  
  <pre><code>id &lt;- c(rep(c(2,8,10,11,16,18),each=3))
  count &lt;- c(6,8,6,5,6,3,6,6,6,5,6,7,6,2,0,6,1,6)
  group &lt;- c(rep(c("A","B","C"),each=6))
  df &lt;- data.frame(id,count,group)
  
  newid&lt;-c()
  newcount&lt;-c()
  newgroup&lt;-c()
  for (i in 1:length(unique(df$"id"))) {
    newid[i] &lt;- unique(df$"id")[i]
    newcount[i]&lt;-sum(df[df$"id"==unique(df$"id")[i],2][1:2])
    newgroup[i] &lt;- as.character(df$"group"[df$"id"==newid[i]][1])
  }
  
  newdf&lt;-data.frame(newid,newcount,newgroup)
  </code></pre>
  
  <p>Some possible improvements/alternatives I'm not sure about:</p>
  
  <ul>
  <li>For loops vs apply functions</li>
  <li>Can I create a dataframe directly inside a for loop or should I stick to creating vectors I can late assign to a dataframe?</li>
  <li>More consistent approaches to accessing/subsetting vectors/columns ($, [], [[]], subset?)</li>
  </ul>