Function “diff” over various groups in R

旧街凉风 提交于 2019-12-04 19:32:46

The plyr package is going to be your friend. The function ddply takes a data.frame, applies a function for each defined subset, then returns a data.frame of all the recombined pieces.

The simplest solution is to use summarize and diff(value) for each combination of .(class, name):

library(plyr)
ddply(df, .(class, name), summarize, diff(value))

   class name ..1
1     c1    a -67
2     c1    a  47
3     c1    b -10
4     c1    b  20
5     c2    a -10
6     c2    a  20
7     c2    b -10
8     c2    b -10
9     c3    a -10
10    c3    a -10
11    c3    b -19
12    c3    b  20

To get your years in the results, it's a little bit more involved:

ddply(df, .(class, name), summarize, year=head(year, -1), value=diff(value))
   class name year value
1     c1    a 2010   -67
2     c1    a 2009    47
3     c1    b 2010   -10
4     c1    b 2009    20
5     c2    a 2010   -10
6     c2    a 2009    20
7     c2    b 2010   -10
8     c2    b 2009   -10
9     c3    a 2010   -10
10    c3    a 2009   -10
11    c3    b 2010   -19
12    c3    b 2009    20
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!