问题
I created a list of dataframes called "list" and want to select only certain columns of every dataset in the list.
library(dplyr)
new_list <- lapply(list, select(list, Date))
It returns an error because class(list[1]) is not dataframe but still a list. class(list[[1]]) is dataframe. I don't understand that because the elements in my list should be dataframes and I also don't know how I can use "lapply" anyway.
Thanks for your help!
回答1:
I think your syntax is just a little off. Try using an anonymous function instead:
l <- list(mtcars,mtcars)
lapply(l,function(x) select(x,cyl,mpg))
回答2:
Also worth bearing in mind [
is a function in itself, so:
new_list <- lapply(list, '[', c("list", "Date"))
来源:https://stackoverflow.com/questions/36985192/using-lapply-on-a-list-of-dataframes