I would like to aggregate data frames A, B, and C by rows and columns to obtain 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?
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.