R:不同组之间的“差异"函数 [英] R: Function “diff” over various groups
问题描述
在寻找问题的解决方案时,我发现了以下线程: Function" ; diff在R 中的各个组上.我有一个非常类似的问题,所以我将在那里处理示例.
While searching for a solution to my problem I found this thread: Function "diff" over various groups in R. I've got a very similar question so I'll just work with the example there.
这是我期望的输出应为:
This is what my desired output should look like:
name class year diff
1 a c1 2009 NA
2 a c1 2010 67
3 b c1 2009 NA
4 b c1 2010 20
我有两个构成子组的变量-类和名称.因此,我只想比较具有相同名称和类的值.我还希望获得2009年到2010年之间的差异.如果没有2008年,则diff 2009应该返回NA(因为它无法计算出差异).
I have two variables which form subgroups - class and name. So I want to compare only the values which have the same name and class. I also want to have the differences from 2009 to 2010. If there is no 2008, diff 2009 should return NA (since it can't calculate a difference).
我确定它的工作原理与其他线程非常相似,但我无法使其正常工作.我也使用了这段代码(并通过对数据进行不同的排序来简单地解决了升序的一年),但是以某种方式R仍然设法计算出差异并且不返回NA.
I'm sure it works very similarly to the other thread but I just can't make it work. I used this code too (and simply solved the ascending year by sorting the data differently), but somehow R still manages to calculate a difference and does not return NA.
ddply(df, .(class, name), summarize, year=head(year, -1), value=diff(value))
推荐答案
使用dplyr
df %>%
filter(year!=2008)%>%
arrange(name, class, year)%>%
group_by(class, name)%>%
mutate(diff=c(NA,diff(value)))
# Source: local data frame [12 x 5]
# Groups: class, name
# name class year value diff
# 1 a c1 2009 33 NA
# 2 a c1 2010 100 67
# 3 a c2 2009 80 NA
# 4 a c2 2010 90 10
# 5 a c3 2009 90 NA
# 6 a c3 2010 100 10
# 7 b c1 2009 80 NA
# 8 b c1 2010 90 10
# 9 b c2 2009 90 NA
# 10 b c2 2010 100 10
# 11 b c3 2009 80 NA
# 12 b c3 2010 99 19
更新:
有相对差异
Update:
With relative difference df %>%
filter(year!=2008)%>%
arrange(name, class, year)%>%
group_by(class, name)%>%
mutate(diff1=c(NA,diff(value)), rel_diff=round(diff1/value[row_number()-1],2))
这篇关于R:不同组之间的“差异"函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!