I created a list and I stored one data frame in each component. Now I would like to filter those data frames keeping only the rows that have NA in a specific column. I would
lapply
's second argument is a function (subset
) and extra arguments to subset
are passed as the ...
arguments to lapply
. Hence:
my.ls <- list(d1 = d1, d2 = d2)
my.lsNA <- lapply(my.ls, subset, is.na(b))
(I am also showing you how to easily create the list of data.frames without using get
, and recommend you don't use ls
as a variable name since it is also the name of a rather common function.)
Regarding the question in @Riccardo's last comment, try:
lapply(my.ls, "[", 1)
or alternately:
lapply(my.ls, "[[", 1)
depending on whether you want the output to be a list of dataframes or a list of vectors.