R从数据框中的句子中删除单词 [英] R remove words from sentences in dataframe

查看:18
本文介绍了R从数据框中的句子中删除单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列的数据框,每列包含句子,我想从另一列中减去一列.我以某种方式无法轻松找到执行以下操作的方法:

I have one dataframe with two columns which each containing sentences and I would like to subtract one from the other. I somehow can't easily find a method to do the following:

> c1 <- c("A short story","Not so short")
> c2 <- c("A short", "Not so")
> data.frame(c1, c2)

应该给出 c1 - c2 的结果

which should give the result of c1 - c2

"story","short"

任何想法都有帮助.

推荐答案

我们可以使用矢量化的 str_remove

We can use str_remove which is vectorized

library(stringr)
library(dplyr)
df1 %>%
   mutate(c3 = str_remove_all(c1, c2))
         c1      c2     c3
#1 A short story A short  story
#2  Not so short  Not so  short

这篇关于R从数据框中的句子中删除单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆