Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Nulls from multiple lists in list

Tags:

list

null

r

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.

like image 655
geo_dd Avatar asked Feb 08 '23 07:02

geo_dd


1 Answers

another option using Hadley's terrific purrr package:

library(purrr)
compact(A)
like image 108
Matthew Plourde Avatar answered Feb 16 '23 04:02

Matthew Plourde