Reputation: 2703
I have an list object below, which contains several NULL values. Now I would like to drop these and have a list without these values, but I can't really figure out the right syntax.
Now as I understand it a lapply loop would not work and I can't seem to get into their respective index list index number, I'm trying foo[[!is.null(foo)]] <- NULL
but this gives me the list objects while the NULL values it contained within, and foo[[]][!is.null(foo)] <- NULL
doesn't work either.
List object
Length Class Mode
0 0 -none- NULL
10 29 data.frame list
20 29 data.frame list
30 29 data.frame list
40 29 data.frame list
50 29 data.frame list
60 29 data.frame list
70 29 data.frame list
80 29 data.frame list
90 29 data.frame list
100 29 data.frame list
110 29 data.frame list
120 29 data.frame list
130 29 data.frame list
140 29 data.frame list
150 0 -none- NULL
160 0 -none- NULL
170 0 -none- NULL
180 0 -none- NULL
190 0 -none- NULL
200 0 -none- NULL
210 0 -none- NULL
220 0 -none- NULL
230 0 -none- NULL
250 0 -none- NULL
260 0 -none- NULL
270 0 -none- NULL
280 0 -none- NULL
300 0 -none- NULL
330 0 -none- NULL
Upvotes: 2
Views: 74
Reputation: 887168
You can use Filter
to remove the NULL
list elements
Filter(Negate(is.null), lst)
lst <- list(data.frame(1:5, 6:10), NULL, NULL, data.frame(1:10))
Upvotes: 4