如何将字符串的每个元素与R中的单词列表进行比较? [英] How to compare each element of a string to a list of words in R?

查看:52
本文介绍了如何将字符串的每个元素与R中的单词列表进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个字符向量

Say for example, I have a character vector

a [1]他快来了,节目太棒了"我感冒了"
d [1]"asap","awsome","cold","lol","rofl"

a [1] "hi come asap, the show is awsome" "I am suffering from cold"
d [1] "asap" "awsome" "cold" "lol" "rofl"

如果在"a"中找到任何单词("d"中的单词),我应该将其替换为空白.如何在R中实现?

I should replace any word(from "d") if found in "a" with empty space. How do I implement in R?

推荐答案

我想我理解,但可能是错误的.您可以尝试:

I think I understand but could be wrong. You could try:

a  <- c("hi come asap, the $#!+ show is awsome", "I am suffering from cold")
d <- c("asap", "awsome", "cold", "lol", "rofl")

library(qdap)
mgsub(d, "", a)

收益:

> mgsub(d, "", a)
[1] "hi come , the $#!+ show is" "I am suffering from" 

这篇关于如何将字符串的每个元素与R中的单词列表进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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