Reputation: 1080
I have Set of Strings like "United Kingdom","United States","China","India", And Other String Has to be compared with the set of above strings,And can be combination of multiple values of the set of above strings seperated by "|" .
Like Example:
String1 <- "China"
String2 <- "United States|China"
String3 <- "United States|India|China"
SetoFStrings <- c("United Kingdom","United States","China","India")
So in all Case When Compared String1,String2,String3 with SetofStrings,The Result value has to be true. How can this be done
Upvotes: 1
Views: 119
Reputation: 887168
We can use any
with grepl
any(grepl(String1, SetoFStrings))
#[1] TRUE
any(grepl(String2, SetoFStrings))
#[1] TRUE
any(grepl(String3, SetoFStrings))
#[1] TRUE
If the objective is to create the 'String's as in the input post
sapply(dat2$Strings, function(pat) any(grepl(pat, SetoFStrings)))
dat1 <- data.frame(Col1 = c('China', 'UnitedStates', 'India'), stringsAsFactors= FALSE)
dat2 <- data.frame(Strings = Reduce(function(...) paste(..., sep="|"),
dat1$Col1, accumulate = TRUE), stringsAsFactors=FALSE)
Upvotes: 1