[英]Summary table for aggregate values in a data frame
I have a data frame like below 我有一个如下数据框
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
and I would like to create a summary table with the aggregate values based on Groups and Models. 我想创建一个汇总表,其中包含基于组和模型的汇总值。 So for every group the aggregates of each models and a further row with the total for that group. 因此,对于每个组,每个模型的集合以及该组的总数的下一行。 Like 喜欢
# 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
I create this script to do that 我创建此脚本来做到这一点
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
Unfortunately this does not work and I got a weird error message after the subset command, already for the first iteration (Group A) when it's clear that must be with some rows. 不幸的是,这不起作用,并且在执行子集命令后,我已经收到了一个奇怪的错误消息,对于第一次迭代(组A),当很明显它必须包含某些行时,已经出现了。
<0 rows> (or 0-length row.names) <0行>(或0长度的行名称)
Error in aggregate.data.frame(as.data.frame(x), ...) : no rows to aggregate Aggregate.data.frame(as.data.frame(x),...)中的错误:没有要聚合的行
is this what you are looking for?: 这是你想要的?:
library(dplyr)
dat %>% group_by(GROUP,MODEL) %>% summarise(sum = sum(VALUE))
or in good old R
: 或在旧R
:
aggregate(a$VALUE , by = list( a$GROUP , a$MODEL ) , FUN = sum)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.