如何用一个词替换数据框列中的多个词?

How do I replace multiple words in the column of a data frame, with a single word?

我正在尝试清理一个数据库,其中因素被归因于不同的词,但含义相同。

例如:来自 "Purring cat with tail, 4 legs and fur""European tabby cat""Cat CAT cat" "Cat".

我研究了 gsub 类型的命令和 stringr 命令,但无法实现我的目标。

有什么建议吗?

编辑: 抱歉不清楚,这是我的第一个 post。

我想用一个替换多个单词。如果 "cat" 出现在条目中的任何位置,我希望将整个条目重命名为 "Cat"。到目前为止,我只能用其他内容替换条目的某些部分,但不能删除多余的单词。

试试这个

x <- c("Purring cat with tail, 4 legs and fur", "European tabby cat", 
"dog", "Cat CAT cat", "bird")

replace(x, grepl("(?i)cat", x, perl=TRUE), "Cat")
# [1] "Cat"  "Cat"  "dog"  "Cat"  "bird"