title: ddply for sum by group in R
body: <p>I have a sample dataframe "data" as follows:</p>
  
  <pre><code>X            Y  Month   Year    income
  2281205 228120  3   2011    1000
  2281212 228121  9   2010    1100
  2281213 228121  12  2010    900
  2281214 228121  3   2011    9000
  2281222 228122  6   2010    1111
  2281223 228122  9   2010    3000
  2281224 228122  12  2010    1889
  2281225 228122  3   2011    778
  2281243 228124  12  2010    1111
  2281244 228124  3   2011    200
  2281282 228128  9   2010    7889
  2281283 228128  12  2010    2900
  2281284 228128  3   2011    3400
  2281302 228130  9   2010    1200
  2281303 228130  12  2010    2000
  2281304 228130  3   2011    1900
  2281352 228135  9   2010    2300
  2281353 228135  12  2010    1333
  2281354 228135  3   2011    2340
  </code></pre>
  
  <p>I want to use the <code>ddply</code> to compute the income for each <code>Y</code>(not <code>X</code>), if I have four observations for each Y (for example for 2281223 with months 6,9,12 of 2010 and month 3 of 2011). If I have less than four observations (for example for Y =228130), I want to simply ignore it. I use the following commands in <code>R</code> for the above purpose: </p>
  
  <pre><code>require(plyr)
       # the data are in the data csv file
      data&lt;-read.csv("data.csv")
      # convert Y (integers) into factors
      y&lt;-as.factor(y)
      # get the count of each unique Y
      count&lt;-ddply(data,.(Y), summarize, freq=length(Y))
      # get the sum of each unique Y 
      sum&lt;-ddply(data,.(Y),summarize,tot=sum(income))
      # show the sum if number of observations for each Y is less than 4
      colbind&lt;-cbind(count,sum)
      finalsum&lt;-subset(colbind,freq&gt;3)
  </code></pre>
  
  <p>My output are as follows:</p>
  
  <pre><code>&gt;colbind
         Y freq      Y   tot
  1 228120    1 228120  1000
  2 228121    3 228121 11000
  3 228122    4 228122  6778
  4 228124    2 228124  1311
  5 228128    3 228128 14189
  6 228130    3 228130  5100
  7 228135    3 228135  5973
  &gt;finalsum
         Y freq    Y.1  tot
  3 228122    4 228122 6778
  </code></pre>
  
  <p>The above code works, but requires many steps. So,I would like to know whether there is a simple way of performing the above task (using the plyr package). </p>