-1

I am looking forward to an R solution that can check whether a word or a sentence (in column 1) is present in the sentence (in column 2) of a data frame or not. If the word/words are present in the sentence, then it should return 1 (TRUE) or else 0 (FALSE). This is how my DF Looks now and This is how it should look like

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
Fraxxx
  • 114
  • 1
  • 11
  • 1
    use this: https://stackoverflow.com/questions/26319567/use-grepl-to-search-either-of-multiple-substrings-in-a-text-in-r like `grepl(gsub(" ", "|", "my new phone"), "this is my mobile phone")` – Roman Aug 02 '17 at 13:12
  • 1
    Maybe `mapply` and `grepl`? – Mike H. Aug 02 '17 at 13:12
  • I tried mapply() and grepl() but they only takes the first word of the substring (column1) to compare with the string (column2). – Fraxxx Aug 02 '17 at 13:19

2 Answers2

1

This should work for you:

df[, "lookup"] <- gsub(" ", "|", df[,"substring"])
df[,"t"] <- mapply(grepl, df[,"lookup"], df[,"string"])

df
#                 substring                 string                   lookup     t
#1             my new phone this is a mobile phone             my|new|phone  TRUE
#2 She would buy new phones Yes, I have two phones She|would|buy|new|phones  TRUE
#3            telephonessss       my old telephone            telephonessss FALSE
#4             telephone234           telephone234             telephone234  TRUE

You could get more fancy with creating the lookup column, but for this case there is no need so I used a simple gsub.


Data:

df <- data.frame(substring = c("my new phone", "She would buy new phones", "telephonessss", "telephone234"),
                 string = c("this is a mobile phone", "Yes, I have two phones", "my old telephone", "telephone234"))
Mike H.
  • 13,960
  • 2
  • 29
  • 39
1

Or use a dplyr & stringr solution. But in principle its the same idea:

library(tidyverse)
library(stringr)
df %>% 
  mutate(result=str_detect(df$string,gsub(" ", "|", df$substring)))
                 substring                 string result
1             my new phone this is a mobile phone   TRUE
2 She would buy new phones Yes, I have two phones   TRUE
3            telephonessss       my old telephone  FALSE
4             telephone234           telephone234   TRUE
Roman
  • 17,008
  • 3
  • 36
  • 49