Remove Nulls from multiple lists in list

2019-06-27 12:47发布

I have a big list (A) of lists of SpatialPolygonsDataFrames. Some of the lists have null values (means there is no 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?

EDIT:

I can't do str(A)because A has 1000 lists and is huge. 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.

标签: r list null
3条回答
祖国的老花朵
2楼-- · 2019-06-27 12:54

We can try Filter

Filter(Negate(is.null), A) 
查看更多
姐就是有狂的资本
3楼-- · 2019-06-27 13:02

you can try this

A[!sapply(A, is.null)]
查看更多
ら.Afraid
4楼-- · 2019-06-27 13:06

another option using Hadley's terrific purrr package:

library(purrr)
compact(A)
查看更多
登录 后发表回答