- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to test if strings stored in a vector are present in an R list?
To test if strings stored in a vector are present in an R list or not, we can use mapply function. For example, if we have a vector of strings say V and a list called LIST then we can check whether elements in V are present in LIST by using the following command −
mapply(`%in%`,V,LIST)
Example 1
Following snippet creates a vector and list −
x<-LETTERS[1:5] List<-list(a=sample(LETTERS[1:5],100,replace=TRUE),b=sample(LETTERS[21:26],100,replace=TRUE),c=letters[1:26],d=sample(LETTERS[1:10],100,replace=TRUE),e=sample(letters[1:5],50,replace=TRUE)) List
The following vector and list are created −
$a [1] "C" "B" "D" "D" "C" "D" "B" "D" "B" "D" "B" "E" "A" "B" "E" "D" "C" "D" [19] "A" "B" "B" "D" "C" "D" "C" "D" "A" "D" "E" "D" "A" "E" "A" "B" "C" "A" [37] "A" "B" "A" "B" "A" "A" "A" "C" "B" "E" "D" "E" "D" "C" "C" "C" "D" "C" [55] "C" "D" "C" "B" "E" "D" "D" "E" "D" "E" "C" "D" "A" "E" "A" "B" "C" "B" [73] "A" "D" "B" "C" "E" "B" "D" "A" "B" "A" "B" "D" "A" "A" "A" "B" "A" "C" [91] "C" "D" "C" "A" "A" "A" "C" "A" "E" "A" $b [1] "Z" "Z" "U" "Z" "Y" "U" "Y" "U" "X" "Y" "Y" "Z" "X" "Z" "Z" "Z" "Y" "Z" [19] "X" "Y" "X" "W" "V" "X" "Z" "Z" "Z" "Z" "U" "W" "W" "Y" "W" "Z" "U" "W" [37] "V" "W" "W" "X" "Y" "V" "V" "W" "W" "Y" "Y" "Z" "X" "Y" "Z" "X" "U" "Z" [55] "Y" "Y" "Y" "W" "V" "U" "W" "X" "V" "Y" "V" "Y" "U" "X" "V" "X" "Z" "W" [73] "Y" "X" "U" "V" "U" "Y" "W" "V" "U" "Y" "V" "Y" "Z" "U" "V" "W" "Y" "U" [91] "U" "Y" "W" "U" "Z" "Y" "X" "Y" "Y" "X" $c [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" $d [1] "G" "B" "G" "C" "F" "H" "J" "A" "H" "E" "H" "E" "I" "A" "E" "A" "I" "D" [19] "A" "D" "D" "C" "D" "J" "G" "C" "D" "G" "G" "A" "D" "C" "F" "G" "G" "J" [37] "F" "E" "D" "E" "E" "G" "F" "F" "I" "C" "C" "F" "C" "E" "D" "C" "D" "F" [55] "J" "G" "D" "G" "C" "H" "H" "A" "D" "B" "G" "C" "A" "J" "I" "D" "J" "C" [73] "F" "H" "C" "F" "G" "C" "G" "D" "J" "I" "C" "E" "I" "H" "D" "G" "B" "D" [91] "H" "I" "B" "E" "J" "I" "C" "G" "F" "J" $e [1] "a" "e" "b" "e" "d" "c" "c" "c" "e" "e" "e" "d" "e" "b" "c" "b" "a" "a" "c" [20] "b" "b" "d" "c" "a" "e" "a" "b" "e" "a" "e" "e" "c" "a" "b" "d" "a" "e" "e" [39] "b" "b" "e" "e" "d" "a" "e" "b" "e" "c" "d" "e"
Now, to check whether elements of x are in List or not, add the following code to the above snippet −
x<-LETTERS[1:5] List<-list(a=sample(LETTERS[1:5],100,replace=TRUE),b=sample(LETTERS[21:26],100,replace=TRUE),c=letters[1:26],d=sample(LETTERS[1:10],100,replace=TRUE),e=sample(letters[1:5],50,replace=TRUE)) mapply(`%in%`,x,List)
Output
If you execute all the above given snippets as a single program, it generates the following output −
A B C D E TRUE FALSE FALSE TRUE FALSE
Example 2
Following snippet creates a vector and list −
y<-letters[1:5] List<-list(a=sample(letters[1:5],100,replace=TRUE),b=sample(letters[21:26],100,replace=TRUE),c=LETTERS[1:26],d=sample(letters[1:10],100,replace=TRUE),e=sample(LETTERS[1:5],50,replace=TRUE)) List
The following vector and list are created −
$a [1] "c" "e" "b" "b" "a" "e" "b" "e" "a" "e" "a" "c" "c" "d" "c" "d" "e" "a" [19] "a" "a" "e" "b" "a" "a" "e" "a" "c" "e" "e" "e" "e" "e" "e" "e" "d" "a" [37] "e" "a" "c" "d" "c" "e" "a" "e" "a" "a" "a" "d" "d" "e" "e" "a" "d" "e" [55] "e" "e" "a" "a" "a" "a" "a" "c" "e" "a" "e" "a" "b" "c" "e" "d" "a" "a" [73] "e" "b" "a" "a" "b" "e" "b" "e" "b" "d" "c" "c" "d" "e" "c" "b" "d" "c" [91] "a" "a" "b" "e" "d" "a" "d" "e" "e" "b" $b [1] "v" "v" "w" "y" "y" "w" "z" "u" "z" "x" "z" "y" "v" "z" "v" "u" "x" "v" [19] "y" "y" "u" "x" "u" "u" "w" "y" "y" "z" "y" "z" "v" "y" "v" "z" "u" "x" [37] "w" "z" "x" "x" "w" "z" "w" "y" "z" "v" "v" "z" "v" "x" "v" "z" "v" "z" [55] "y" "x" "y" "v" "u" "z" "v" "y" "z" "w" "v" "z" "v" "z" "z" "v" "u" "u" [73] "u" "z" "y" "w" "w" "y" "w" "z" "x" "z" "y" "v" "v" "z" "v" "z" "y" "u" [91] "x" "y" "x" "u" "x" "w" "y" "z" "u" "w" $c [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" $d [1] "b" "f" "d" "f" "a" "b" "h" "e" "c" "j" "b" "e" "i" "b" "e" "c" "h" "a" [19] "g" "d" "h" "c" "c" "b" "b" "h" "g" "b" "d" "c" "e" "a" "a" "a" "a" "h" [37] "f" "a" "i" "a" "a" "j" "f" "i" "i" "e" "g" "a" "j" "e" "i" "g" "c" "g" [55] "h" "c" "c" "i" "b" "g" "b" "b" "g" "g" "g" "a" "h" "a" "i" "e" "g" "a" [73] "g" "h" "e" "b" "h" "h" "g" "e" "e" "i" "c" "g" "d" "h" "d" "f" "d" "b" [91] "h" "g" "e" "b" "g" "c" "a" "i" "c" "c" $e [1] "B" "E" "D" "B" "E" "B" "B" "B" "C" "B" "C" "E" "A" "B" "D" "D" "C" "B" "B" [20] "A" "A" "D" "B" "E" "C" "E" "E" "C" "E" "C" "B" "A" "B" "A" "A" "B" "E" "B" [39] "A" "E" "E" "A" "D" "B" "A" "A" "B" "E" "C" "D"
Now, to check whether elements of y are in List or not, add the following code to the above snippet −
y<-letters[1:5] List<-list(a=sample(letters[1:5],100,replace=TRUE),b=sample(letters[21:26],100,replace=TRUE),c=LETTERS[1:26],d=sample(letters[1:10],100,replace=TRUE),e=sample(LETTERS[1:5],50,replace=TRUE)) mapply(`%in%`,y,List)
Output
If you execute all the above given snippets as a single program, it generates the following output −
a b c d e TRUE FALSE FALSE TRUE FALSE
- Related Articles
- How to multiply matrices elements if matrices are stored in a list in R?
- How to check if a vector exists in a list in R?
- How to extract strings that contains a particular substring in an R vector?
- How to test if a List is an Unmodifable List in Java?
- How to replace numbers with ordinal strings for a survey in an R vector?
- How to take transpose of vectors stored in an R list?
- How to convert a named vector to a list in R?
- How to extract strings based on first character from a vector of strings in R?
- How to find similar words in vector of strings in R?
- How to combine data frames stored in a list in R?
- How to subset a data frame by excluding the column names that are stored in a vector in R?
- How to convert a string vector into an integer vector in R?
- How to find the maximum value in each matrix stored in an R list?
- How to change the name of data frames stored in an R list?
- How to find the mean of all matrices stored in an R list?
