I have a list containing lists and NAs. I'd like to filter the NAs out, but I don't know the exact name or position of the NAs.
I googled a lot, but only found how to remove elements by name or by indexing, but this is not what I'm looking for.
Here's an example of how my list looks like:
example <- list(list(1,2,3), list(2,3,4), NA, list(2,3,4))
My output looks like this:
Name Type Value
example list[[4]] List of length 4
[[1]] list[[3]] List of length 3
[[2]] list[[3]] List of length 3
[[3]] logical NA <-I'd like to remove this row
[[4]] list[[3]] List of length 3
I'd like to write a loop that removes an element from my list if the value is "NA" or the type is "logical". Thanks in advance for your help!
It depends on your data structure. With your example, the following will work:
example[!is.na(example) & !is.logical(example)]
It becomes more complicated if the sub-lists can contain NA
's that you want to remove as well.
You can use which
function to find the index of NA
elements and then remove them by index:
example[-which(is.na(example))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With