简体   繁体   English

从列表列表中删除空列表

[英]Remove empty list from a list of lists

I have a list of lists where some of them are NA eg empty lists . 我有一个列表列表,其中一些是NA例如empty lists I want to extract all the lists which are filled with data and remove all the lists which are empty(NA) . 我想提取所有填充数据的列表,并删除所有empty(NA)的列表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. 我只想提取包含5s的列表。 The first 6 lists should be ignored eg removed. 应忽略前6个列表,例如删除。

Any help is appreciated 任何帮助表示赞赏

So, to remove NA at the first level, you could use is.na directly: 因此,要在第一级删除NA ,您可以直接使用is.na

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. 或者,您也可以使用Filter尝试将已计算函数的结果强制转换为逻辑,并返回那些求值为TRUE的元素。 You could do, for example: 你可以做,例如:

Filter(function(x) !is.na(x), l)

(or) equivalently (as @flodel writes under comment) (或)等效地(如@flodel在评论中写)

Filter(Negate(is.na), l)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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