using lapply on a list of dataframes

拈花ヽ惹草 提交于 2019-12-08 12:37:09

问题


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

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