[英]R aggregating data frame
If our marriage data frame looks like this 如果我们的婚姻数据框看起来像这样
Month Year marriage_counts
1 Jan 2011 50
2 Jan 2011 30
3 Jan 2011 20
4 Feb 2011 80
5 Feb 2011 10
and our Business data looks like this 我们的业务数据如下所示
Month Year
1 Jan 2011
2 Jan 2011
3 Jan 2011
4 Feb 2011
5 Feb 2011
this should return a data frame that looks like 这应该返回一个看起来像
Month Year marriage_count
1 Jan 2011 100
2 Feb 2011 90
but I'm stuck in here.. can anybody please help me? 但我被困在这里..有人可以帮助我吗?
In base R: 在基数R中:
agg <- aggregate(marriage_counts ~ Month + Year, marriage, sum)
Via Dplyr: 通过Dplyr:
library(dplyr)
df_marriage %>% group_by(Month, Year) %>%
summarise(marriage_count = sum(marriage_counts))
Via Data.table: 通过Data.table:
data.table::setDT(marriage)[, .(marriage_count = sum(marriage_counts)) , by = .(Month, Year)]
Another alternative using {purrr}
. 使用{purrr}
另一种选择。 slice_rows()
is equivalent to dplyr's group_by()
. slice_rows()
等同于dplyr的group_by()
。
library(purrr)
df_marriage <- data.frame(Month = c("Jan", "Jan", "Jan", "May", "May"),
Year = 2011,
marriage_counts = c(50, 30, 20, 80, 10))
df_marriage %>% slice_rows(c("Month", "Year")) %>% by_slice(map, sum)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.