【发布时间】:2017-09-08 01:43:57
【问题描述】:
我一直在尝试使用 dplyr 在具有相同结构的表列表中汇总多个表:
LUZ_code Type1 Type2 Type3 Type4 country
AT001L2 90142 752310 70700 7368 AT
AT002L2 82693 193892 30264 496 AT
AT003L2 119690 203394 28737 420 AT
AT004L2 42259 85892 14512 189 AT
AT005L2 113768 59841 15464 224 AT
AT006L1 126001 102170 9344 134 AT
我已经在脚本中应用了几个 lapply,所以现在我在一个名为国家的列表中拥有了这些表。
如果我尝试使用循环:
for (i in 1:length(countries)){
years <- c("2010", "2030", "2030_ECL")
db <- as.data.frame(countries[i])[,-1]
db <- db %>%
group_by(country) %>%
summarise_each(funs(sum))
write.table(db, paste("country_conc",years[i], ".txt", sep = ""),
col.names = TRUE, row.names = FALSE, sep = "\t", quote = FALSE)
}
这似乎没有问题,但我想知道是否有办法使用 lapply。到目前为止,我的尝试是:
summarise <- function (db){
db <- (db)[,-1]
db <- db %>%
group_by(country) %>%
summarise_each(funs(sum))
return (db)
}
total <- lapply (concentration, summarise)`
我收到此错误消息:
汇总错误(tbl, Type1 = sum(Type1), Type2 = sum(Type2), Type3 = sum(Type3), : 未使用的参数 (Type1 = sum(Type1), Type2 = sum(Type2), Type3 = sum(Type3), Type4 = sum(Type4))"
感谢您的帮助,
【问题讨论】:
-
concentration在lapply调用中是什么? -
你为什么不
bind_rows表列表(同时分配一个id)然后计算摘要? -
您可以使用
summarise_at()或summarise_if()指定要汇总的列。