How to check which list element contains a particular value in R?

R ProgrammingServer Side ProgrammingProgramming

A list in R can have large number of elements and also of different types. If we have a list that contain vectors and we want to check which list element contains a particular value then we can use which function along with sapply function.

For Example, if we have a list called LIST then we can find which element of LIST contains 5 then we can use the command given below −

which(sapply(LIST, FUN=function(X) 5 %in% X))

Example 1

Consider the List given below −

List1<-
list(x1=rpois(150,10),x2=rpois(150,2),x3=rpois(150,3),x4=rpois(150,5),x5=rpois(150,10))
List1

Output

If you execute the above given snippet, it generates the following Output −

$x1
[1] 10 11 9 17 13 11 10 12 9 11 2 8 18 18 12 10 5 12 11 12 11 8 9 12 8
[26] 9 12 13 14 8 4 13 15 8 12 10 13 10 12 11 13 13 12 8 6 11 9 14 12 6
[51] 9 9 16 15 9 17 10 11 10 11 14 6 12 7 14 14 12 4 5 13 11 9 8 7 6
[76] 12 15 14 10 10 12 10 8 10 9 10 6 8 14 13 9 11 9 12 3 12 5 14 19 15
[101] 8 6 6 8 15 20 5 18 11 10 12 12 14 12 7 7 13 11 16 11 14 15 12 5 8
[126] 12 7 6 10 13 8 6 11 10 5 7 7 11 10 11 10 9 8 8 18 10 6 9 14 8
$x2
[1] 0 1 2 2 2 1 2 2 0 0 3 2 3 2 3 3 3 0 0 2 4 5 3 0 1 4 1 4 1 0 1 2 4 1 2 2 5
[38] 3 3 5 3 1 2 4 1 2 2 2 1 5 5 1 4 3 5 0 2 0 3 2 3 5 0 3 2 1 0 1 1 3 0 2 0 1
[75] 3 3 2 2 1 3 2 0 2 3 2 0 3 2 4 2 2 2 3 2 2 1 1 2 1 4 4 5 2 3 3 3 3 5 2 2 4
[112] 1 4 1 2 1 1 2 1 4 4 0 3 2 4 3 0 0 1 2 1 3 0 2 4 2 2 2 2 3 3 5 4 1 2 1 2 0
[149] 3 0
$x3
[1] 1 4 1 1 3 1 6 2 4 2 5 2 3 3 4 4 0 2 2 4 3 3 1 1 2 1 2 2 6 7 2 4 2 4 4 2 8
[38] 4 5 3 3 3 6 3 2 5 4 1 7 3 2 3 4 1 2 2 2 1 3 1 2 1 2 3 1 2 0 1 3 2 3 2 3 2
[75] 1 0 2 4 3 7 4 2 0 2 3 3 2 5 6 0 0 3 5 0 3 6 3 8 2 4 2 7 3 3 4 2 2 4 1 0 3
[112] 3 3 2 0 4 1 3 1 2 4 3 6 2 4 1 3 3 4 3 1 3 0 1 3 1 6 0 3 3 4 1 5 3 2 5 4 4
[149] 4 1
$x4
[1] 4 7 3 9 6 6 7 7 2 5 1 7 3 5 4 4 3 2 4 6 4 5 2 6 8
[26] 3 7 4 4 6 2 3 9 10 6 4 5 8 9 6 4 3 8 5 6 3 2 6 8 9
[51] 4 7 8 2 8 7 9 6 5 2 4 7 6 3 5 2 7 6 9 3 4 4 10 4 7
[76] 5 6 5 3 8 4 2 5 4 8 6 3 7 5 3 5 2 5 2 3 8 4 7 8 7
[101] 6 3 5 6 7 6 10 5 6 7 3 4 10 5 4 4 6 0 4 2 5 6 5 8 2
[126] 5 2 4 3 5 6 8 4 7 7 6 6 3 1 6 11 9 5 5 6 5 8 6 5 7
$x5
[1] 17 10 5 8 14 9 9 6 7 12 13 12 8 13 7 13 6 11 6 10 7 8 4 21 8
[26] 8 13 12 6 5 7 12 12 8 9 6 11 14 11 11 11 11 12 8 7 5 9 9 8 9
[51] 16 9 16 13 10 12 10 7 6 9 2 6 9 9 14 8 9 11 14 13 12 16 13 9 4
[76] 9 10 10 6 6 9 11 9 9 11 11 7 5 8 8 11 6 6 12 6 11 10 8 4 9
[101] 11 14 19 8 9 9 6 8 15 12 10 5 7 8 18 12 8 11 7 8 9 5 11 6 9
[126] 21 4 12 17 7 13 9 14 10 5 15 15 13 8 7 11 14 15 9 13 9 8 8 12 8

To check the elements in List1 that contains 12 on the above created data frame, add the following code to the above snippet −

List1<-
list(x1=rpois(150,10),x2=rpois(150,2),x3=rpois(150,3),x4=rpois(150,5),x5=rpois(150,10))
which(sapply(List1, FUN=function(X) 12 %in% X))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

x1 x5
1  5

Example 2

Consider the List given below −

List2<-
list(y1=rpois(50,100),y2=rpois(50,125),y3=rpois(50,110),y4=rpois(50,150),y5=rpois(50,105))
List2

If you execute the above given snippet, it generates the following Output −

$y1
[1] 115 94 118 86 84 91 97 105 115 105 89 100 92 102 104 90 85 113 104
[20] 101 106 98 96 107 106 87 101 119 93 97 100 106 97 99 103 122 85 94
[39] 96 84 86 92 89 85 87 121 95 105 79 99 
$y2
[1] 124 140 122 97 115 114 121 129 148 137 112 116 121 134 108 119 115 136 130
[20] 118 117 117 142 123 136 121 123 117 127 130 127 127 130 149 133 129 124 142
[39] 122 117 125 115 117 123 105 129 115 129 117 131
$y3
[1] 125 119 104 96 132 108 105 113 106 101 125 113 114 128 121 109 114 111 124
[20] 116 96 120 110 94 110 108 114 117 99 123 117 112 114 93 102 109 109 101
[39] 111 108 113 102 108 109 113 96 139 106 91 94
$y4
[1] 146 155 164 169 165 141 149 149 142 154 155 139 141 158 171 126 141 146 160
[20] 158 168 144 150 155 140 161 154 143 155 144 160 165 163 138 161 147 141 148
[39] 145 170 157 135 160 158 153 170 139 175 150 166
$y5
[1] 92 113 93 100 113 95 107 88 106 105 119 117 77 107 102 98 108 115 111
[20] 108 93 104 89 88 93 106 108 102 94 109 86 118 104 99 121 99 108 103
[39] 115 84 121 91 95 98 103 99 95 104 100 87

To check the elements in List2 that contains 100 on the above created data frame, add the following code to the above snippet −

List2<-
list(y1=rpois(50,100),y2=rpois(50,125),y3=rpois(50,110),y4=rpois(50,150),y5=rpois(50,105))
which(sapply(List2, FUN=function(X) 100 %in% X))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

y1 y5
1  5
raja
Published on 28-Oct-2021 08:11:57
Advertisements