[英]Aggregating frequency tables in R
I would like to aggregate data frames A, B, and C by rows and columns to obtain D.我想按行和列聚合数据框 A、B 和 C 以获得 D。
A <- data.frame(A = c("John","Fred","Paul"), Money = c(5,20,10), Hats = c(1,2,2))
B <- data.frame(A = c("John","Fred"), Money = c(15,10), Hats = c(1,2))
C <- data.frame(A = c("Paul"), Money = c(20), Hats = c(1))
D <- data.frame(A = c("John","Fred","Paul"), Money = c(20,30,30), Hats = c(2,3,3))
Which one would it be the fastest way in R?哪一种是 R 中最快的方法?
You could do:你可以这样做:
aggregate(.~A, do.call(rbind,list(A,B,C)), sum)
A Money Hats
1 Fred 30 4
2 John 20 2
3 Paul 30 3
or simply或者干脆
aggregate(.~A, rbind(A,B,C), sum)
A Money Hats
1 Fred 30 4
2 John 20 2
3 Paul 30 3
Using dplyr:使用 dplyr:
library(dplyr)
bind_rows(A,B,C) %>% group_by(A) %>% summarise(Money = sum(Money), Hats = sum(Hats))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 3
A Money Hats
<chr> <dbl> <dbl>
1 Fred 30 4
2 John 20 2
3 Paul 30 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.