Reputation: 3973
I have a list as follows:
mtlist = as.list(mtcars)
mtlist[[2]] = NA
mtlist[[5]] = NA
What's the most concise way to extract the names of the list elements which are NA?
I came up with the solution below. Yet I wonder if there are other better options. Especially all(is.na(x))
seems error prone to me.
names(which(sapply(mtlist, function (x) all(is.na(x)))))
Upvotes: 0
Views: 35
Reputation: 3833
You can use is.na()
function to check which of the elements of list are NA and extract their names using names()
function.
names(mtlist)[is.na(mtlist)]
# [1] "cyl" "drat"
Upvotes: 2