比如说,我有一个字符向量
a [1] “hi come asap, the show is awsome” “我感冒了”
d [1] “asap” “awsome” “cold” “lol” “rofl”
如果在“a”中找到任何单词(来自“d”),我应该用空格替换。我如何在 R 中实现?
比如说,我有一个字符向量
a [1] “hi come asap, the show is awsome” “我感冒了”
d [1] “asap” “awsome” “cold” “lol” “rofl”
如果在“a”中找到任何单词(来自“d”),我应该用空格替换。我如何在 R 中实现?
也许像下面这样的东西对你有用:
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 "
我想我理解但可能是错的。你可以试试:
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"