简体   繁体   中英

subset dataframe by column in a list based on a vector of column names and summarize the columns

I have a list of 40 dataframes. A subset for example would look like:

d1<-data.frame(a=c(1,2,3,4,5), b=c("2006", "2006", "2006", "2007", "2007"), d=c(6,7,8,9,10), e=c(11,12,13,14,15))
d2<-data.frame(a=c(1,2,3,4,5), b=c("2006", "2006", "2006", "2007", "2007"), d=c(6,7,8,9,10), e=c(11,12,13,14,15))
d3<-data.frame(a=c(1,2,3,4,5), b=c("2006", "2006", "2006", "2007", "2007"), d=c(6,7,8,9,10), e=c(11,12,13,14,15))

mylist <- list(l1=d1, l2=d2, l3=d3)

I want to subset the database based on a vector of column names:

subset_colnames <- c("a", "d", "e")

Such that after subsetting dataframe should look like this:

#Subsetting dataframes based on columns:
d1<-data.frame(a=c(1,2,3,4,5), b=c("2006", "2006", "2006", "2007", "2007"))
d2<-data.frame(d=c(6,7,8,9,10), b=c("2006", "2006", "2006", "2007", "2007"))
d3<-data.frame(e=c(11,12,13,14,15), b=c("2006", "2006", "2006", "2007", "2007"))

mylist_filtered = list(l1=d1, l2=d2, l3=d3)

Eventually I want to summarize the column names in subset_columns for each dataframe in the list like so:

d1 %>% 
  group_by(b) %>% 
  summarise(mean = mean(a), n = n())

d2 %>% 
  group_by(b) %>% 
  summarise(mean = mean(d), n = n())

d3 %>% 
  group_by(b) %>% 
  summarise(mean = mean(e), n = n())

I would like to do this using lapply , looked at solutions here and here but my operation is slightly unique in that I want to subset columns based on a character vector

You can use Map , with a customized function that takes a data frame from the list and a column name from the subset_columns and summarize it; To evaluate the character name as a actual column in summarize , use the rlang/tidyeval syntax:

library(dplyr); library(rlang);

cust_mean <- function(df, col) {
    df %>% 
        group_by(b) %>% 
        summarise(mean = mean(!!sym(col)), n = n())
}

Map(cust_mean, mylist, subset_colnames)
#$l1
# A tibble: 2 x 3
#       b  mean     n
#  <fctr> <dbl> <int>
#1   2006   2.0     3
#2   2007   4.5     2

#$l2
# A tibble: 2 x 3
#       b  mean     n
#  <fctr> <dbl> <int>
#1   2006   7.0     3
#2   2007   9.5     2

#$l3
# A tibble: 2 x 3
#       b  mean     n
#  <fctr> <dbl> <int>
#1   2006  12.0     3
#2   2007  14.5     2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM