我想从 R 中的字符串中删除多个单词,但想使用字符向量而不是正则表达式。
例如,如果我有字符串
"hello how are you"
并想删除
c("hello", "how")
我会回来
" are you"
我可以使用 stringr
中的 str_remove()
来接近
"hello how are you" %>% str_remove(c("hello","how"))
[1] "how are you" "hello are you"
但是我需要做一些事情来将其分解为一个字符串。是否有一个函数可以一次调用完成所有这些操作?
最佳答案
我们可以使用 |
来评估正则表达式或
library(stringr)
library(magrittr)
pat <- str_c(words, collapse="|")
"hello how are you" %>%
str_remove_all(pat) %>%
trimws
#[1] "are you"
数据
words <- c("hello", "how")
关于使用向量而不是 R 中的正则表达式从字符串中删除多个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083274/