我想使用 ddply 函数将相同的函数写入多个列,但我尝试将它们写在一行中,想看看有没有更好的方法?
这是数据的简单版本:
data<-data.frame(TYPE=as.integer(runif(20,1,3)),A_MEAN_WEIGHT=runif(20,1,100),B_MEAN_WEIGHT=runif(20,1,10))
我想通过这样做找出列 A_MEAN_WEIGHT 和 B_MEAN_WEIGHT 的总和:
ddply(data,.(TYPE),summarise,MEAN_A=sum(A_MEAN_WEIGHT),MEAN_B=sum(B_MEAN_WEIGHT))
但在我目前的数据中,我有超过 8 个“*_MEAN_WEIGHT”,我厌倦了将它们写 8 次
ddply(data,.(TYPE),summarise,MEAN_A=sum(A_MEAN_WEIGHT),MEAN_B=sum(B_MEAN_WEIGHT),MEAN_C=sum(C_MEAN_WEIGHT),MEAN_D=sum(D_MEAN_WEIGHT),MEAN_E=sum(E_MEAN_WEIGHT),MEAN_F=sum(F_MEAN_WEIGHT),MEAN_G=sum(G_MEAN_WEIGHT),MEAN_H=sum(H_MEAN_WEIGHT))
有没有更好的方法来写这个?谢谢您的帮助!!