2015-02-11 106 views
2

我有一些数据,我想总结一下:可变长度不同错误的aggragate

studentid friend Gfriend 
214 30401006  0  0 
236 30401006  0  0 
208 30401006  1  0 
229 30401006  0  0 
207 30401006  0  0 
278 30401007  1  0 
250 30401007  1  0 
266 30401007  1  0 
254 30401007  1  1 
277 30401007  1  1 
243 30401007  1  1 

结果应该是这个样子:

studentid friend Gfriend 
30401006 1  0 
30401007 6  3 

当我尝试:agg=aggregate(c(friend)~studentid,data=df,FUN=sum)我得到的所需的结果(但仅限于朋友变量)。 但是当我尝试:agg=aggregate(c(friend,Gfriend)~studentid,data=df,FUN=sum)我得到:

错误model.frame.default(式= C(朋友,Gfriend)〜studentid, :可变长度不同(发现 'studentid')

我检查变量的长度(长度(VAR)),他们都是一样的,再加上有没有不适用的,所以我不知道在哪里这个错误的来源。

这究竟是为什么?

+3

你接近,尽量'cbind',而不是'C'在'骨料(cbind(朋友,Gfriend)〜studentid,DF,总和)' – 2015-02-11 13:39:51

+0

工作就好了。非常感谢大卫! – user3821211 2015-02-11 13:53:38

+0

或者,如果您的数据中没有其他列,则可以这样做:'aggregate(。〜studentid,df,sum)' – 2015-02-11 13:54:22

回答

0

你也可以尝试“通过”

studentid < c(30401006,30401006,30401006,30401006,30401006,30401007, 
+ 30401007,30401007,30401007,30401007,30401007) 
friend <- c(0,0,1,0,0,1,1,1,1,1,1) 
Gfriend <- c(0,0,0,0,0,0,0,0,1,1,1) 
df <- data.frame(studentid,friend,Gfriend) 
df 

> result <- by(df[c(2:3)], df$studentid, FUN=colSums) 

> result 
df$studentid: 30401006 
friend Gfriend 
1  0 
df$studentid: 30401007 
friend Gfriend 
6  3 
0

编辑:添加na.rm = T,以解决有关排除来港

时退房“plyr”包注释。

library(plyr) 

#split by "studentid" and sum all numeric colums 

ddply(df, .(studentid), numcolwise(sum, na.rm=T)) 

studentid friend Gfriend 
1 30401006  1  0 
2 30401007  6  3