I am new to plyr (and R) and looking for a little help to get started. Using the baseball dataset as an exaple, how could I calculate the year-over-year (yoy) change in "at batts" by league and team (lg and team)?
library(plyr) df1
After doing a little aggregating to simplify the data fame, the data looks like this:
head(df1) year lg team ab 1884 UA ALT 108 1997 AL ANA 1703 1998 AL ANA 1502 1999 AL ANA 660 2000 AL ANA 85 2001 AL ANA 219
I would like to end up with someting like this
year lg team ab yoy 1997 AL ANA 1703 NA 1998 AL ANA 1502 -201 1999 AL ANA 660 -842 2000 AL ANA 85 -575 2001 AL ANA 219 134
I started by writign the following function, which I think is wrong:
yoy.func
Without sucess, I used the following code to attempt return the yoy change.
df2
Any guidance woud be appreciated.
Thanks