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