How to subset data.frames stored in a list?

后端 未结 2 1096
南方客
南方客 2020-12-30 12:38

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

相关标签:
2条回答
  • 2020-12-30 13:23

    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.)

    0 讨论(0)
  • 2020-12-30 13:27

    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.

    0 讨论(0)
提交回复
热议问题