【发布时间】:2020-04-29 09:25:36
【问题描述】:
我有一个数据集,其中包含向不同政客捐款的数据,其中每一行都是特定的捐款。
donor.sector <- c(sector A, sector B, sector X, sector A, sector B)
total <- c(100, 100, 150, 125, 500)
year <- c(2006, 2006, 2007, 2007, 2007)
state <- c(CA, CA, CA, NY, WA)
target_specific <- c(politician A, politician A, politician A, politician B, politician C)
dat <- as.data.frame(donor.sector, total, year, target_specific, state)
我正在尝试计算每位政治家一年的捐款平均值。我可以通过以下方式做到这一点:
library(dplyr)
new.df <- dat%>%
group_by(target_specific, year)%>%
summarise(mean= mean(total))
我的问题是,由于我对此进行了分组,因此结果只有三个变量:平均值、年份和特定目标。有没有办法可以做到这一点并创建一个新的数据框,在其中保留政治级别的变量,例如州?
非常感谢!
【问题讨论】: