I'm trying to find a way to check whether a list contains an element that itself contains a particular string. Finding an exact match is simple with %in%:
list1 <- list("a","b","c")
"a" %in% list1
[1] TRUE
But it only works if the element is identical, i.e. it doesn't return TRUE if the element only contains the string:
list2 <- list("a,b","c,d")
"a" %in% list2
[2] FALSE
Is there a way to generate TRUE for the second example? Thanks in advance.
library(stringi)
list2 <- list("a,b","c,d")
stri_detect_fixed(list2, "a")
## [1] TRUE FALSE
stri_detect_fixed(list2, "b")
## [1] TRUE FALSE
stri_detect_fixed(list2, "c")
## [1] FALSE TRUE
stri_detect_fixed(list2, "d")
## [1] FALSE TRUE
stri_detect_fixed(list2, "q")
## [1] FALSE FALSE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With