Search code examples
rnlptext-mining

How to apply grepl for data frame


I want to use grepl for multiple patterns defined as a data frame. df_sen is presented as

sentence
"She would like to go there"
"I had it few days ago"
"We have spent few millions"

df_triggers is presented as follows:

trigger
few days
few millions

And I want to create a matrix where sentence x triggers and on the intersection to see 1 if trigger was found in a sentence and 0 if it was not.

I have tried to do it like this:

matrix <- grepl(df_triggers$trigger, df_sen$sentence)

But I see the error message that I have more than 1 pattern in grepl().

The desired output is:

                                 few days    few millions
"She would like to go there"        0              0
"I had it few days ago"             1              0
"We have spent few millions         0              1

Solution

  • sapply(df_triggers$trigger, grepl, df_sen$sentence)
    

    from @docendodiscimus worked.