lapply aggregate columns in multiple dataframes R

試著忘記壹切 提交于 2021-02-20 04:16:08

问题


I have several dataframes in a list in R. There are entries in each of those DF I would like to summarise. Im trying to get into lapply so that would be my preferred way (though if theres a better solution I would be happy to know it and why).

My Sample data:

df1 <- data.frame(Count = c(1,2,3), ID = c("A","A","C"))
df2 <- data.frame(Count = c(1,1,2), ID = c("C","B","C"))
dfList <- list(df1,df2)

> head(dfList)
[[1]]
  Count ID
1     1  A
2     2  A
3     3  C

[[2]]
  Count ID
1     1  C
2     1  B
3     2  C

I tried to implement this in lapply with

dfList_agg<-lapply(dfList, function(i) {
  aggregate(i[[1:length(i)]][1L], by=list(names(i[[1:length(i)]][2L])), FUN=sum)
})

However this gives me a error "arguments must have same length". What am I doing wrong?

My desired output would be the sum of Column "Count" by "ID" which looks like this:

>head(dfList_agg)
[[1]]
  Count ID
1     3  A
2     3  C

[[2]]
  Count ID
1     3  C
2     1  B

回答1:


I think you've overcomplicated it. Try this...

dfList_agg<-lapply(dfList, function(i) {
  aggregate(i[,1], by=list(i[,2]), FUN=sum)
})

dflist_agg

[[1]]
  Group.1 x
1       A 3
2       C 3

[[2]]
  Group.1 x
1       B 1
2       C 3



回答2:


Here is a third option

lapply(dfList, function(x) aggregate(. ~ ID, data = x, FUN = "sum"))
#[[1]]
#  ID Count
#1  A     3
#2  C     3
#
#[[2]]
#ID Count
#1  B     1
#2  C     3



回答3:


I guess this is what you need

library(dplyr)
lapply(dfList,function(x) ddply(x,.(ID),summarize,Count=sum(Count)))



回答4:


An option with tidyverse would be

library(tidyverse)
map(dfList, ~ .x %>%
                group_by(ID) %>% 
                summarise(Count = sum(Count)) %>%
                select(names(.x)))
#[[1]]
# A tibble: 2 x 2
#   Count ID    
#  <dbl> <fctr>
#1  3.00 A     
#2  3.00 C     

#[[2]]
# A tibble: 2 x 2
#  Count ID    
#  <dbl> <fctr>
#1  1.00 B     
#2  3.00 C     


来源:https://stackoverflow.com/questions/48721243/lapply-aggregate-columns-in-multiple-dataframes-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!