比如说,我有一个字符向量
一 [1]“嗨来吧,节目很棒”“我感冒了” d [1]“asap”“awsome”“cold”“lol”“rofl”
如果在空格的“a”中找到,我应该替换任何单词(来自“d”)。我如何在R中实现?
答案 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"