I have a list of lists where some of them are NA
eg empty lists
. I want to extract all the lists which are filled with data and remove all the lists which are empty(NA)
.
The code i'm trying is:
lapply(outputfile,function(x){
if(outputfile != NA){
test<-lapply(outputfile,unlist)
}})
But this does not work.
The list of lists is like this: (small example of random data)
list(NA, NA, NA, NA, NA, NA, list(c(5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5)))
I only want to extract the list with the 5s in it. The first 6 lists should be ignored eg removed.
Any help is appreciated
So, to remove NA
at the first level, you could use is.na
directly:
l[!is.na(l)]
Alternatively, you can also use Filter
which tries to coerce the results of the evaluated function to logical and returns those elements that evaluated to TRUE. You could do, for example:
Filter(function(x) !is.na(x), l)
(or) equivalently (as @flodel writes under comment)
Filter(Negate(is.na), l)
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.