我有一个如下数据框
ID VALUE GROUP MODEL
1 id_1 17.6100597 Group A Model B
2 id_10 6814.7109375 Group A Model B
3 id_100 26.5990372 Group C Model F
4 id_1000 0.0000000 Group B Model D
5 id_101 486.4001160 Group C Model C
6 id_102 108.9007416 Group C Model C
7 id_103 1.0070915 Group B Model F
8 id_104 0.5426522 Group B Model F
9 id_105 1.1137601 Group C Model C
10 id_106 0.2867465 Group C Model C
我想创建一个包含基于组和模型的聚合值的汇总表。因此,对于每个组,每个模型的聚合和另一行与该组的总计。像
# Groups Models Aggregates
# Group A Model A aggAA
# Group A Model B aggAB
# Group A Total A aggA
# Group B Model A aggBA
# Group B Model B aggBB
# Group B Model C aggBC
# Group B Total B aggB
我创建此脚本来执行该操作
dat <- read.csv2("data.csv")
dat$GROUP <- factor(dat$GROUP)
groupnames <- levels(dat$GROUP)
resume <- NULL
for (group in groupnames) {
dat1 <- dat1[dat1$GROUP == group,]
aggdat1 <- aggregate(dat1[,"VALUE"],list(dat1[,"MODEL"]),FUN=sum)
colnames(aggdat1) <- c("MODEL", "VALUE")
resumegroup <- rbind(aggdat1,c(" ",colSums(aggdat1[-1])))
resumegroup <- cbind("GROUP"=group,resumegroup)
resume <- rbind(resume,resumegroup)
}
resume
不幸的是,这不起作用,我在子命令之后得到了一个奇怪的错误消息,已经用于第一次迭代(A组),当时很明显必须有一些行。
&lt; 0行&gt; (或0长度的row.names)
aggregate.data.frame(as.data.frame(x),...)中的错误: 没有要聚合的行
答案 0 :(得分:1)
这就是你要找的东西?:
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
或旧的library(dplyr)
dat %>% group_by(GROUP,MODEL) %>% summarise(sum = sum(VALUE))
:
R