0

Let´s say I have a list in R:

a <- c(1,2,3)
f <- NULL
b <- c("a","b","c")
d <- list(a,f,b)

yielding

> d
[[1]]
[1] 1 2 3

[[2]]
NULL

[[3]]
[1] "a" "b" "c"

I know it sounds dumb to build a list like this, but I get this from a previous computation (I could have set f to be 0 or any other character). I tried to remove f since I need the algorithm to perform operations in valid columns a and b, but unsuccessfully so far. d[2] is still there...

So how do I remove d[2] from the list? It should go from length = 3 to length = 2, and in the real instance I don´t know beforehand which elements of the list are NULL, so an if-based response would be great...

Cheers,

Arrebimbomalho
  • 176
  • 1
  • 12

1 Answers1

1

use the is.null function from R to identify which elements are NULL. This will work:

d[-which(lapply(d,is.null) == T)]
Alejandro Andrade
  • 2,196
  • 21
  • 40