Reputation: 79
Suppose I've got this data:
ColA ColB
------------ ------------------------
apple tree Mary has an apple tree
orange+apple Lucy loves orange+apple
orange apple Anne loves orange+apple
I want to evaluate if ColB contains ColA and create a logical variable:
ColA ColB Ind
------------ ------------------------ -----
apple tree Mary has an apple tree TRUE
orange+apple Lucy loves orange+apple TRUE
orange apple Anne loves orange+apple FALSE
Any Suggestions using R?
Many thanks!
Upvotes: 2
Views: 83
Reputation: 102625
Here is a base R option using Vectorize
over grepl
within(
df,
Ind <- Vectorize(grepl)(ColA,ColB,fix = TRUE)
)
giving
ColA ColB Ind
1 apple tree Mary has an apple tree TRUE
2 orange+apple Lucy loves orange+apple TRUE
3 orange apple Anne loves orange+apple FALSE
Upvotes: 1
Reputation: 887851
We can use str_detect
which is vectorized for both patterns and string
library(dplyr)
library(stringr)
df1 <- df1 %>%
mutate(Ind = str_detect(ColB, fixed(ColA)))
-output
df1
# ColA ColB Ind
#1 apple tree Mary has an apple tree TRUE
#2 orange+apple Lucy loves orange+apple TRUE
#3 orange apple Anne loves orange+apple FALSE
df1 <- structure(list(ColA = c("apple tree", "orange+apple", "orange apple"
), ColB = c("Mary has an apple tree", "Lucy loves orange+apple",
"Anne loves orange+apple")), class = "data.frame", row.names = c(NA,
-3L))
Upvotes: 2