简体   繁体   English

从列表中的多个列表中删除Null

[英]Remove Nulls from multiple lists in list

I have a big list ( A ) of lists of SpatialPolygonsDataFrames. 我有一个SpatialPolygonsDataFrames列表的大清单( A )。 Some of the lists have null values (means there is no SpatialPolygonsDataFrame). 某些列表具有空值(表示没有SpatialPolygonsDataFrame)。 I tried : 我试过了 :

A[!sapply(unlist(A, recursive=FALSE), is.null)]

But with no result and then I tried: 但没有结果,然后我尝试:

A_nonulls=lapply(A, na.omit)

What is the right way to remove the null of every list in the bigger list? 删除较大列表中每个列表的null的正确方法是什么?

EDIT: 编辑:

I can't do str(A)because A has 1000 lists and is huge. 我不能做str(A)因为A有1000个列表并且很大。 The first elements from the first list is like : 第一个列表中的第一个元素如下:

[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
NULL

[[5]]
class       : SpatialPolygons 
features    : 1 
extent      : 722951.5, 726848.9, 4325874, 4329654  (xmin, xmax, ymin, ymax)

So I want to removw the nulls and keep only the not empty elements. 所以我想删除空值并仅保留非空元素。

another option using Hadley's terrific purrr package: 使用Hadley极好的purrr包的另一种选择:

library(purrr)
compact(A)

你可以试试这个

A[!sapply(A, is.null)]

我们可以试试Filter

Filter(Negate(is.null), A) 

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

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