Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly omit NAs from a nested list?

Tags:

r

I'm working with a list of lists - let's call it L - in R, where the sub-lists are all the same length & padded with NAs. Ideally, I'd like to remove just the NA elements from each sublist, and one solution I've come up with is L <- lapply(L, na.omit). It seems to almost work; however, for each sub-list, the behavior is such that, for example,

[[1]]
[1] "0"     "12345"  "12346"  "12347" "12348"  "12349"  "12340"  "12341"  "12342" NA      NA          NA      NA      NA      NA     

[16] NA      NA      NA      NA      NA      NA      NA      NA      NA      NA      NA    

becomes

[[1]]
[1] "0"     "12345"  "12346"  "12347" "12348"  "12349"  "12340"  "12341"  "12342"
attr(,"na.action")
[1] 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
attr(,"class")
[1] "omit"

I'm a little bemused, to be honest - what's up with the extra attr()s and such in my list? Is there a solution out there that won't append them to the list? I've tried na.exclude, but it gives the same result. Is there something I'm missing? Thanks in advance : )

like image 374
a barking spider Avatar asked Dec 21 '22 07:12

a barking spider


1 Answers

No reason to be bemused... the Details section of ?na.omit says:

If ‘na.omit’ removes cases, the row numbers of the cases form the ‘"na.action"’ attribute of the result, of class ‘"omit"’.

Try subsetting each list element with is.na:

L <- lapply(L, function(x) x[!is.na(x)])
like image 104
Joshua Ulrich Avatar answered Dec 24 '22 01:12

Joshua Ulrich