如何将字符串的每个元素与R中的单词列表进行比较?

时间:2013-02-04 07:26:05

标签: r list merge vectorization

比如说,我有一个字符向量

  

一   [1]“嗨来吧,节目很棒”“我感冒了”   d   [1]“asap”“awsome”“cold”“lol”“rofl”

如果在空格的“a”中找到,我应该替换任何单词(来自“d”)。我如何在R中实现?

2 个答案:

答案 0 :(得分:4)

以下内容可能适合您:

a  <- c("hi come asap, the show is awsome", "I am suffering from cold")
d <- c("asap", "awsome", "cold", "lol", "rofl")
d[d %in% gsub("[[:punct:]]", "", unlist(strsplit(a, " ")))] <- " "
d
# [1] " "    " "    " "    "lol"  "rofl"

或者,相反的方式:

a  <- c("hi come asap, the show is awsome", "I am suffering from cold")
d <- c("asap", "awsome", "cold", "lol", "rofl")
gsub(paste(d, collapse = "|"), " ", a)
# [1] "hi come  , the show is  " "I am suffering from  "  

答案 1 :(得分:2)

我想我理解但可能是错的。你可以尝试:

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"