How to check if a vector exists in a list in R?


To check if a vector exists in a list, we can use %in%, and read the vector as list using list function. For example, if we have a list called LIST and a vector called V then we can check whether V exists in LIST using the command LIST %in% list(V).

Example

Consider the below list −

 Live Demo

List<-list(x1=LETTERS[1:26],x2=1:50,x3=rpois(50,5),x4=rnorm(50),x5=rpois(50,2),x6=rnorm(50,5,1),x7=letters[1:26],x8=runif(50,2,5),x9=rexp(50,3.24))
List

Output

$x1
[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"
$x2
[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
$x3
[1] 4 5 5 5 7 4 7 3 2 9 11 4 5 3 10 5 4 4 8 3 1 7 3 6 5
[26] 2 5 3 1 3 1 4 4 9 7 5 6 2 6 6 5 11 5 5 5 7 3 6 3 3
$x4
[1] -0.09560671 0.41644849 1.52185081 1.25478269 -0.10120845 0.92858571
[7] -1.75769674 0.08586818 0.83654211 0.44516897 -0.42887927 -0.84480895
[13] -1.03752988 1.39581547 0.45523535 -1.23186748 0.28840459 0.09653478
[19] 0.27613359 0.25938658 1.35128069 0.21522862 -0.20182570 1.53706539
[25] 0.95243340 -0.77404491 -0.94918391 0.07563642 1.22060920 1.10518441
[31] 0.79664162 0.88648980 -0.22657289 0.00663885 1.66687832 -0.89556504
[37] -0.42351873 1.42195589 0.23710129 0.69048295 0.23026881 -0.31919684
[43] -0.71827618 -0.33974962 2.41356273 0.27326890 -1.35680540 -1.24595936
[49] 0.40430027 -0.53664646
$x5
[1] 0 2 2 6 0 2 1 3 2 4 2 1 5 3 2 0 2 2 2 1 1 5 1 1 3 5 2 3 6 5 3 2 1 0 2 0 0 0
[39] 5 3 2 3 0 2 2 1 0 3 1 3
$x6
[1] 6.134097 3.882553 4.462413 7.044883 4.229043 3.482663 3.943874 4.747332
[9] 3.167549 5.517625 4.169625 4.237116 4.316943 2.921994 3.610351 4.353406
[17] 5.295587 6.611960 2.964135 4.458357 4.936820 4.894477 3.117890 6.904135
[25] 5.057749 4.856171 5.242526 4.819986 4.443272 4.243939 5.945440 5.811889
[33] 6.494682 6.669360 5.126852 5.151575 4.993319 4.923821 6.486212 4.865304
[41] 5.446804 5.038247 4.934172 5.313578 3.632589 5.350681 4.257391 5.483359
[49] 5.142934 4.562802
$x7
[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"
$x8
[1] 3.402047 4.446528 4.097843 3.979449 3.728044 4.324007 4.674882 4.360976
[9] 4.041636 3.853124 4.577922 3.594319 2.346518 3.195564 4.742620 3.547263
[17] 4.849656 3.062701 2.655747 3.456749 2.829216 4.407436 4.146494 4.866745
[25] 3.854683 2.255636 2.898519 3.255778 2.125811 3.399427 4.240987 4.598209
[33] 3.877593 4.557378 3.241529 3.862941 3.133698 3.670895 2.159567 3.583178
[41] 4.878772 4.984912 3.880342 4.502762 3.098179 4.472519 2.171510 2.299131
[49] 4.778286 4.869166
$x9
[1] 0.588381300 0.004375135 0.369618958 0.021075609 0.071974796 0.053904244
[7] 0.354839422 0.083627786 0.129399610 0.289138850 0.011676473 0.183556635
[13] 0.666047243 0.280067965 0.368349921 0.031696706 0.218040137 0.001709351
[19] 0.145331193 0.160332704 0.422142772 0.083291753 0.086747291 0.171843138
[25] 0.337125534 0.110894449 0.122360436 0.233614876 0.216746519 0.054293163
[31] 0.162507216 0.111237748 0.932469720 0.069711156 0.173526116 0.452977953
[37] 0.844435056 0.180822992 0.002493007 0.302044948 0.434001206 0.037101571
[43] 0.255578660 0.148020738 0.036570471 0.349629033 0.168946321 0.354580846
[49] 0.242166925 0.243347151

Consider the below vector −

y1=LETTERS[1:26]

Checking whether y1 exist in List −

List %in% list(y1)

[1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

Updated on: 16-Mar-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements