Remove Nulls from multiple lists in list

寵の児 提交于 2020-01-14 10:12:11

问题


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.


回答1:


We can try Filter

Filter(Negate(is.null), A) 



回答2:


another option using Hadley's terrific purrr package:

library(purrr)
compact(A)



回答3:


you can try this

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


来源:https://stackoverflow.com/questions/34221981/remove-nulls-from-multiple-lists-in-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!