Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether string is contained within a list

Tags:

r

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.

like image 295
heds1 Avatar asked Apr 06 '26 11:04

heds1


1 Answers

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
like image 173
hrbrmstr Avatar answered Apr 09 '26 02:04

hrbrmstr