【发布时间】:2017-01-04 12:02:38
【问题描述】:
我有一个数据框,其中有一列中的句子列表。现在我有一个单词列表和另一个替换单词列表,如下所示。
DF
Date Sentences
9-Nov-16 nah, i got rid of them overyears ago i was only watching pbs by then anyway i have xfinity on demand+netflix on my 'puter
9-Nov-16 Omg Netflix is working on my Xfinity!
等等
单词列表
words <- c("nah","'puter","Omg")
trans <- c("No","computer","Oh my God")
等等。
现在我想将上述数据框中的句子中的“Nah”替换为“No”,将“'puter”替换为“computer”,“Omg”替换为“Oh my God”等等。为此,我使用以下代码。
DF$Sentences<- str_replace_all( DF$Sentences, paste0("\\b",words,"\\b"), trans)
但这并不能替换句子中的单词。有人能告诉我这样做的正确方法吗?
【问题讨论】:
标签: r