How to extract elements of a list that do not contain NULL in R?


A list sometimes contains NULL elements along with other elements. Therefore, we might want to get rid of that NULL element so that we can use our list without any hustle. To do this, we can use lapply function with the following syntax −

Syntax

“List_name”[!unlist(lapply(“List_name”,is.null))]

Example

Consider the below list −

 Live Demo

x1<-LETTERS[1:20]
x2<-sample(1:50,30)
x3<-rpois(50,5)
x4<-NULL
x5<-sample(1:10,20,replace=TRUE)
x6<-sample(1:10,50,replace=TRUE)
x7<-NULL
List<-list(x1,x2,x3,x4,x5,x6,x7)
List

Output

[[1]]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T"
[[2]]
[1] 27 38 3 48 16 17 50 8 31 21 34 37 46 18 7 11 29 9 49 45 12 44 32 4 13
[26] 22 43 35 39 20
[[3]]
[1] 4 4 6 5 4 11 3 2 4 7 2 0 2 3 9 2 4 8 7 6 4 3 2 8 6
[26] 9 7 5 5 5 3 8 5 1 6 6 2 4 2 4 2 5 4 5 3 2 10 7 5 7
[[4]]
NULL
[[5]]
[1] 4 5 3 9 5 1 8 7 5 1 10 9 10 4 4 8 7 3 6 1
[[6]]
[1] 6 4 8 5 5 10 7 5 3 7 10 5 8 1 10 9 3 8 2 5 9 3 5 9 6
[26] 5 8 6 3 10 6 3 8 4 10 5 3 7 3 4 5 6 1 8 6 6 9 2 5 4
[[7]]
NULL

Extracting list elements that are not NULL −

Example

List[!unlist(lapply(List,is.null))]

Output

[[1]]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T"
[[2]]
[1] 27 38 3 48 16 17 50 8 31 21 34 37 46 18 7 11 29 9 49 45 12 44 32 4 13
[26] 22 43 35 39 20
[[3]]
[1] 4 4 6 5 4 11 3 2 4 7 2 0 2 3 9 2 4 8 7 6 4 3 2 8 6
[26] 9 7 5 5 5 3 8 5 1 6 6 2 4 2 4 2 5 4 5 3 2 10 7 5 7
[[4]]
[1] 4 5 3 9 5 1 8 7 5 1 10 9 10 4 4 8 7 3 6 1
[[5]]
[1] 6 4 8 5 5 10 7 5 3 7 10 5 8 1 10 9 3 8 2 5 9 3 5 9 6
[26] 5 8 6 3 10 6 3 8 4 10 5 3 7 3 4 5 6 1 8 6 6 9 2 5 4

Let’s have a look at another example −

Example

 Live Demo

y1<-runif(45,5,10)
y2<-rexp(30,5)
y3<-rpois(50,5)
y4<-rbinom(50,10,0.5)
y5<-rnorm(50,2,5)
y6<-sample(1:10,50,replace=TRUE)
y7<-NULL
y8<-sample(1:20,50,replace=TRUE)
y9<-LETTERS[1:26]
y10<-1:50
New_List<-list(y1,y2,y3,y4,y5,y6,y7,y8,y9,y10)
New_List

Output

[[1]]
[1] 6.873191 6.832051 7.994383 5.560593 5.948195 6.148714 9.089198 8.024749
[9] 5.358905 8.940129 9.813643 5.225109 8.198397 6.791847 9.445375 9.296775
[17] 8.978427 9.823661 9.559257 9.600359 7.107150 9.533786 9.437307 5.852952
[25] 9.104474 8.204243 5.467284 5.643628 5.698499 5.586102 7.165100 9.540500
[33] 6.690515 9.422854 9.663365 6.047424 9.512649 5.237828 5.176613 9.307774
[41] 9.214194 5.736416 6.611679 9.158329 8.125111
[[2]]
[1] 9.327297e-02 2.628267e-02 5.837356e-01 5.526928e-01 1.493052e-01
[6] 4.684264e-05 1.109019e-02 1.632936e-01 8.791466e-02 1.389368e-01
[11] 6.252034e-02 1.838558e-01 6.541021e-02 6.040159e-01 4.687270e-02
[16] 4.825074e-01 2.566619e-01 1.121949e-01 6.038569e-01 1.542802e-01
[21] 4.781870e-01 4.814911e-02 1.001143e-02 3.015966e-01 7.467835e-02
[26] 1.958277e-01 1.115525e-01 1.445676e-01 2.605108e-01 1.109321e+00
[[3]]
[1] 5 7 3 2 5 5 2 9 10 4 3 2 4 5 3 5 7 1 8 6 3 11 6 3 9
[26] 9 1 4 6 5 5 3 7 4 6 4 1 7 5 8 5 3 4 9 4 5 3 5 8 6
[[4]]
[1] 5 4 2 4 4 7 8 6 5 3 5 5 4 3 5 4 5 8 4 6 7 4 9 7 2 1 6 6 5 3 5 4 5 5 3 4 2 5
[39] 6 6 3 5 5 3 6 5 5 6 4 5
[[5]]
[1] -4.9037570 0.2327350 6.0266983 2.5974054 7.0022107 -6.1438301
[7] 8.7918581 8.4450933 -0.5585743 2.0662291 10.1116677 2.3497613
[13] -0.4061269 -1.1139795 7.3350721 2.5669137 7.3098503 -0.4888469
[19] 4.8192636 10.4241038 -1.3895469 -4.9470356 -0.5330585 6.0830108
[25] 0.3959233 4.4163268 -5.4136557 5.2560139 5.4981337 -4.6610475
[31] -4.1390856 -7.5773305 1.9526344 2.4610800 10.0877877 3.8905763
[37] 2.9887570 4.8073600 6.1438959 2.9197778 -6.8822575 4.5946571
[43] 1.1602598 -4.0467120 5.2462252 10.2414053 -0.7420613 -2.3959838
[49] -3.2863947 6.5189106
[[6]]
[1] 9 7 5 7 6 2 1 7 6 7 10 5 9 10 4 4 8 6 10 3 10 1 5 10 1
[26] 4 7 10 6 6 7 1 3 5 6 5 1 9 4 5 5 1 1 9 7 6 4 4 5 10
[[7]]
NULL
[[8]]
[1] 13 2 9 3 18 2 16 4 8 3 7 3 3 2 3 9 15 4 8 14 17 4 15 20 20
[26] 3 4 6 11 6 14 4 15 18 5 9 19 7 3 18 6 2 4 18 3 4 19 15 17 8
[[9]]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
[[10]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Example

New_List[!unlist(lapply(New_List,is.null))]

Output

[[1]]
[1] 6.873191 6.832051 7.994383 5.560593 5.948195 6.148714 9.089198 8.024749
[9] 5.358905 8.940129 9.813643 5.225109 8.198397 6.791847 9.445375 9.296775
[17] 8.978427 9.823661 9.559257 9.600359 7.107150 9.533786 9.437307 5.852952
[25] 9.104474 8.204243 5.467284 5.643628 5.698499 5.586102 7.165100 9.540500
[33] 6.690515 9.422854 9.663365 6.047424 9.512649 5.237828 5.176613 9.307774
[41] 9.214194 5.736416 6.611679 9.158329 8.125111
[[2]]
[1] 9.327297e-02 2.628267e-02 5.837356e-01 5.526928e-01 1.493052e-01
[6] 4.684264e-05 1.109019e-02 1.632936e-01 8.791466e-02 1.389368e-01
[11] 6.252034e-02 1.838558e-01 6.541021e-02 6.040159e-01 4.687270e-02
[16] 4.825074e-01 2.566619e-01 1.121949e-01 6.038569e-01 1.542802e-01
[21] 4.781870e-01 4.814911e-02 1.001143e-02 3.015966e-01 7.467835e-02
[26] 1.958277e-01 1.115525e-01 1.445676e-01 2.605108e-01 1.109321e+00
[[3]]
[1] 5 7 3 2 5 5 2 9 10 4 3 2 4 5 3 5 7 1 8 6 3 11 6 3 9
[26] 9 1 4 6 5 5 3 7 4 6 4 1 7 5 8 5 3 4 9 4 5 3 5 8 6
[[4]]
[1] 5 4 2 4 4 7 8 6 5 3 5 5 4 3 5 4 5 8 4 6 7 4 9 7 2 1 6 6 5 3 5 4 5 5 3 4 2 5
[39] 6 6 3 5 5 3 6 5 5 6 4 5
[[5]]
[1] -4.9037570 0.2327350 6.0266983 2.5974054 7.0022107 -6.1438301
[7] 8.7918581 8.4450933 -0.5585743 2.0662291 10.1116677 2.3497613
[13] -0.4061269 -1.1139795 7.3350721 2.5669137 7.3098503 -0.4888469
[19] 4.8192636 10.4241038 -1.3895469 -4.9470356 -0.5330585 6.0830108
[25] 0.3959233 4.4163268 -5.4136557 5.2560139 5.4981337 -4.6610475
[31] -4.1390856 -7.5773305 1.9526344 2.4610800 10.0877877 3.8905763
[37] 2.9887570 4.8073600 6.1438959 2.9197778 -6.8822575 4.5946571
[43] 1.1602598 -4.0467120 5.2462252 10.2414053 -0.7420613 -2.3959838
[49] -3.2863947 6.5189106
[[6]]
[1] 9 7 5 7 6 2 1 7 6 7 10 5 9 10 4 4 8 6 10 3 10 1 5 10 1
[26] 4 7 10 6 6 7 1 3 5 6 5 1 9 4 5 5 1 1 9 7 6 4 4 5 10
[[7]]
[1] 13 2 9 3 18 2 16 4 8 3 7 3 3 2 3 9 15 4 8 14 17 4 15 20 20
[26] 3 4 6 11 6 14 4 15 18 5 9 19 7 3 18 6 2 4 18 3 4 19 15 17 8
[[8]]
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
[[9]]
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

Updated on: 08-Sep-2020

365 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements