R: Checks whether multiple elements of the vector appear in the string vector
•
Java
I'm trying to create a function to check whether all elements of the vector appear in the string vector The test code is as follows:
test_values = c("Alice","Bob") test_list = c("Alice,Chris,Mark","Alice,Bob,Chris",Mark,Zach","Mark,Bob",Zach")
I want this output to be false true false true false true false
I first thought that I could switch to & in the command grepl (paste (test_values, collapse = '|'), test_ List), but I can't get the correct answer
I also don't want to use the command: grepl (test_values [1], test_list) & grepl (test_values [2], test_list) because test_ The values vector will change dynamically (from length 0 to 3), so I'm looking for something that takes this into account
Solution
We can use redpl and grepl
Reduce(`&`,lapply(test_values,grepl,test_list)) #[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码