I am working on R
with strings. My dataframe DF
has next structure:
DF <- data.frame(v1=c(1,2,3),v2=c("Oranges are fruits","Hit music","Ferrari is red"),stringsAsFactors = F)
v1 v2
1 1 Oranges are fruits
2 2 Hit music
3 3 Ferrari is red
And I have a vector d
which contains:
d <- c("fruits","red")
I am looking for a way to test if all strings in v2
have coincidence with d
. In this way, I have tried next code:
DF$v3 <- grepl(d,DF$v2)
But I get this result:
v1 v2 v3
1 1 Oranges are fruits TRUE
2 2 Hit music FALSE
3 3 Ferrari is red FALSE
Which is not correct because string in third row of v2
has word red
that is contained in d
. Is it any way to obtain an output like this:
v1 v2 v3
1 1 Oranges are fruits TRUE
2 2 Hit music FALSE
3 3 Ferrari is red TRUE
My original dataset is larger and DF
is a sample of it. Many thanks for your help.