我一直在尝试使用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,所以现在我将这些表放在名为countries的列表中。
如果我尝试使用循环:
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))&#34;
感谢您的帮助,
答案 0 :(得分:1)
我让它工作正常。我同意@akrun,我不知道concentration
是什么。我认为将其更改为countries
可能会解决您的问题。这是我的工作范例。
c1 <- "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"
t1 <- read.table(text = c1, header = T)
t4 <- t3 <- t2 <- t1
t2$country <- "ZZ"
t3$country <- "YY"
t4$country <- "XX"
countries <- list(t1, t2, t3, t4)
mySummarise <- function (db){
db <- (db)[,-1]
db <- db %>%
group_by(country) %>%
summarise_each(funs(sum))
return (data.frame(db))
}
total <- lapply (countries, mySummarise)
do.call(rbind.data.frame, total)