本文介绍了如何将grepl应用于数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对定义为数据帧的多个模式使用grepl.df_sen表示为

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表示如下:

trigger
few days
few millions

我想创建一个矩阵,句子x会在此触发,并在相交处查看1如果在句子中找到了触发,而0如果没有找到.

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.

我试图这样做:

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

但是我看到错误消息,指出我在grepl()中具有多个模式.

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

所需的输出是:

                                 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

推荐答案

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

来自@docendodiscimus,工作.

from @docendodiscimus worked.

这篇关于如何将grepl应用于数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 17:29