How to apply grepl for data frame

99封情书 提交于 2019-12-12 03:38:10

问题


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

回答1:


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

from @docendodiscimus worked.



来源:https://stackoverflow.com/questions/43936428/how-to-apply-grepl-for-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!