Remove NULL elements from list of lists

后端 未结 7 2010
天涯浪人
天涯浪人 2020-11-27 20:20

How do I remove the null elements from a list of lists, like below, in R:

lll <- list(list(NULL),list(1),list(\"a\"))

The object I want

7条回答
  •  迷失自我
    2020-11-27 20:27

    Since you have lists in lists, you probably need to run l/sapply twice, like:

    lll[!sapply(lll,sapply,is.null)]
    
    #[[1]]
    #[[1]][[1]]
    #[1] 1
    #
    #
    #[[2]]
    #[[2]][[1]]
    #[1] "a"
    

提交回复
热议问题