Вставьте все stopwords
вместе и используйте gsub
для их удаления.
df$new_text <- trimws(gsub(paste0(stopwords, collapse = "|"), "", df$text))
df
# a text new_text
#1 a1 today the weather looks hot the weather looks
#2 a2 its so rainy outside rainy
#3 a3 today its sunny sunny
Или с помощью str_remove_all
stringr::str_remove_all(df$text, paste0(stopwords, collapse = "|"))
Просто для большей безопасности добавьте словограницы вокруг каждого stopwords
, так что "so"
из "something"
или "some"
не заменяется.
df$new_text <- trimws(gsub(paste0("\\b", stopwords, "\\b",
collapse = "|"), "", df$text))