get the mean of a variable subset of data in R
<p>Imagine I have the following data:</p>

<pre><code>    Year   Month  State  ppo 
    2011   Jan     CA    220 
    2011   Feb     CA    250
    2012   Jan     CA    230 
    2011   Jan     WA    200 
    2011   Feb     WA    210
</code></pre>

<p>I need to calculate the mean for each state for the year, so the output would look something like this: </p>

<pre><code>    Year   Month  State  ppo  annualAvg
    2011   Jan     CA    220    230
    2011   Feb     CA    240    230
    2012   Jan     CA    260    260
    2011   Jan     WA    200    205
    2011   Feb     WA    210    205
</code></pre>

<p>where the annual average is the mean of any entries for that state in the same year. If the year and state were constant I would know how to do this, but somehow the fact that they are variable is throwing me off.</p>

<p>Looking around, it seems like maybe ddply is what I want to be using for this (<a href="https://stats.stackexchange.com/questions/8225/how-to-summarize-data-by-group-in-r">https://stats.stackexchange.com/questions/8225/how-to-summarize-data-by-group-in-r</a>), but when I tried to use it I was doing something wrong and kept getting errors (I have tried so many variations of it that I won't bother to post them all here). Any idea how I am actually supposed to be doing this?</p>

<p>Thanks for the help!</p>