Anton Belyanin Anton Belyanin - 2 months ago 9
R Question

AND grepl operation in R

Suppose I have the following character

c <- "1 NA NA 4 5 6 NA NA 9 10 NA NA 13 14 15 16 17 18 19 20 NA NA 23 NA NA NA NA"


How can I grepl it to test it contains 1 and 5 and 6? Without getting 15, 16, etc. as false positive?

Answer

You can use word boundary \\b around 1,5,6:

grepl("\\b[156]\\b", c)
# [1] TRUE

If you want to check if all of the 1,5,6 are contained in the string, you can vectorize grepl on the pattern position:

all(Vectorize(grepl)(paste("\\b", c(1,5,6), "\\b", sep = ""), c))
# [1] TRUE

When we remove 1 from the string:

c <- "NA NA 4 5 6 NA NA 9 10 NA NA 13 14 15 16 17 18 19 20 NA NA 23 NA NA NA NA"

grepl("\\b[156]\\b", c)   # this is or condition, will still give true
# [1] TRUE

all(Vectorize(grepl)(paste("\\b", c(1,5,6), "\\b", sep = ""), c))  # This gives FALSE since 
# pattern \\b1\\b is not there
# [1] FALSE