在R中按组总结数据的所有数字列

时间:2022-11-15 07:36:35

I have a data frame that has unique groups defined by 3 character variables

我有一个数据框,其中包含由3个字符变量定义的唯一组

catvars <- c("DATE", "COUNTRY_FULL_NAME", "TENOR")

The rest of the data frame consists of 20 numeric variables (condensing it to 3 in the sample below)

数据框的其余部分由20个数字变量组成(在下面的示例中将其浓缩为3)

numvars <- c("X1", "Y1, "Z1")

I am trying to create a new data frame with the mean for each numeric variable calculates by group

我正在尝试创建一个新的数据框,每个数字变量的平均值按组计算

For a single variable, I can use ddply from the plyr package:

对于单个变量,我可以使用plyr包中的ddply:

DFsum <- ddply(DF, catvars, summarize, X1mean = mean(X, na.rm=TRUE))

But I can't figure out how to modify this ddply command to include all numeric variables. Any suggestions? Thank you

但我无法弄清楚如何修改此ddply命令以包含所有数字变量。有什么建议么?谢谢

1 个解决方案

#1


6  

I think you're looking for numcolwise?

我想你在寻找numcolwise?

ddply(diamonds,.(cut),numcolwise(mean,na.rm = TRUE))
        cut     carat    depth    table    price        x        y        z
1      Fair 1.0461366 64.04168 59.05379 4358.758 6.246894 6.182652 3.982770
2      Good 0.8491847 62.36588 58.69464 3928.864 5.838785 5.850744 3.639507
3 Very Good 0.8063814 61.81828 57.95615 3981.760 5.740696 5.770026 3.559801
4   Premium 0.8919549 61.26467 58.74610 4584.258 5.973887 5.944879 3.647124
5     Ideal 0.7028370 61.70940 55.95167 3457.542 5.507451 5.520080 3.401448

#1


6  

I think you're looking for numcolwise?

我想你在寻找numcolwise?

ddply(diamonds,.(cut),numcolwise(mean,na.rm = TRUE))
        cut     carat    depth    table    price        x        y        z
1      Fair 1.0461366 64.04168 59.05379 4358.758 6.246894 6.182652 3.982770
2      Good 0.8491847 62.36588 58.69464 3928.864 5.838785 5.850744 3.639507
3 Very Good 0.8063814 61.81828 57.95615 3981.760 5.740696 5.770026 3.559801
4   Premium 0.8919549 61.26467 58.74610 4584.258 5.973887 5.944879 3.647124
5     Ideal 0.7028370 61.70940 55.95167 3457.542 5.507451 5.520080 3.401448