简体   繁体   中英

R remove list full of NA from a list of lists

I have a list of lists, which may contain NA values. How can I remove the lists that are full of NA values, keeping in mind that if there are non-NA values on the list these values and the NA values should not be removed?

An input example:

myList <- list()
myList[[1]] <- c(1,2,3)
myList[[2]] <- c(4,5)
myList[[3]] <- c(NA,NA,NA,NA,NA)
myList[[4]] <- c(NA, 6, 7, NA)
myList[[5]] <- NA

The desired output is:

[[1]]
[1] 1 2 3

[[2]]
[1] 4 5

[[3]]
[1] NA  6  7 NA

So far I was able to do:

test <- lapply(myList, function(x) x[!all(is.na(x))])

and got as output:

[[1]]
[1] 1 2 3

[[2]]
[1] 4 5

[[3]]
logical(0)

[[4]]
[1] NA  6  7 NA

[[5]]
logical(0)

Another option is discard

library(purrr)
discard(myList, ~ all(is.na(.x)))
#[1]]
#[1] 1 2 3

#[[2]]
#[1] 4 5

#[[3]]
#[1] NA  6  7 NA

Filter to the rescue:

Filter(function(x) !all(is.na(x)), myList)
#[[1]]
#[1] 1 2 3
#
#[[2]]
#[1] 4 5
#
#[[3]]
#[1] NA  6  7 NA

You could subset list elements with at least one non NA value:

> myList[sapply(myList, function(x) sum(!is.na(x))) > 0]
[[1]]
[1] 1 2 3

[[2]]
[1] 4 5

[[3]]
[1] NA  6  7 NA

您可以通过为其分配NULL来“杀死”元素。

myList[sapply(myList,function(x) all(is.na(x)))] <- NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM