要测试存储在向量中的字符串是否存在于 R 列表中,我们可以使用 mapply 函数。例如,如果我们有一个字符串向量 V 和一个名为 LIST 的列表,那么我们可以使用以下命令检查 V 中的元素是否存在于 LIST 中 -
mapply(`%in%`,V,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
创建以下向量和列表 -
$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"
现在,要检查 x 的元素是否在 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)) mapply(`%in%`,x,List)输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
A B C D E TRUE FALSE FALSE TRUE FALSE
以下代码段创建了一个向量和列表 -
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
创建以下向量和列表 -
$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"
现在,要检查 y 的元素是否在 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)) mapply(`%in%`,y,List)输出结果
如果您将上述所有给定的片段作为单个程序执行,它会生成以下输出 -
a b c d e TRUE FALSE FALSE TRUE FALSE