I have a dataframe like this,
sample | 1 |
---|---|
My car is black | is black |
My car is red | are red |
My car is yellow | yellow |
My car is green | black |
and I want to check if column 1's data is included in column sample.
Expected df:
sample | 1 | 2 |
---|---|---|
My car is black | is black | True |
My car is red | are red | False |
My car is yellow | yellow | True |
My car is green | black | False |
I tried to create this with for loop function, but it doesn't work.
CodePudding user response:
You may use str_detect
which is vectorized over string and pattern.
library(dplyr)
library(stringr)
df <- df %>% mutate(X2 = str_detect(sample, X1))
df
# sample X1 X2
#1 My car is black is black TRUE
#2 My car is red are red FALSE
#3 My car is yellow yellow TRUE
#4 My car is green black FALSE
In base R, you can use Vectorize
grepl
.
df$X2 <- Vectorize(grepl)(df$X1, df$sample)
CodePudding user response:
You can use grepl
df %>%
rowwise %>%
mutate(X2 = grepl(X1,sample))
sample X1 X2
<chr> <chr> <lgl>
1 My car is black is black TRUE
2 My car is red are red FALSE
3 My car is yellow yellow TRUE
4 My car is green black FALSE