我想找到并用另一个单词列表替换单词列表。
说我的数据是
1) plz suggst med for flu
2) tgif , m goin to have a blast
3) she is getting anorexic day by day
要替换的单词列表是
1) plz -- please
2) pls -- please
3) sugg -- suggest
4) suggst -- suggest
5) tgif -- thank god its friday
6) lol -- laughed out loud
7) med -- medicine
我想有2个列表,列表" A" - 要找到的单词列表和列表" B" - 要替换的单词列表。这样我就可以在需要时继续为这些列表添加术语。我需要一种机制来搜索列表中的所有单词" A"然后将其替换为列表中的相应单词" B"。
在R中实现这一目标的最佳方式是什么。先谢谢。
答案 0 :(得分:1)
看看?gsub
x <- c("plz suggst med for flu", "tgif , m goin to have a blast", "she is getting anorexic day by day")
gsub("plz", "please", x)
答案 1 :(得分:1)
试试这个:
#messy list
listA <- c("plz suggst med for flu",
"tgif , m goin to have a blast",
"she is getting anorexic day by day")
#lookup table
list_gsub <- read.csv(text="
a,b
plz,please
pls,please
sugg,suggest
suggst,suggest
tgif,thank god its friday
lol,laughed out loud
med,medicine")
#loop through each lookup row
for(x in 1:nrow(list_gsub))
listA <- gsub(list_gsub[x,"a"],list_gsub[x,"b"], listA)
#output
listA
#[1] "please suggestst medicine for flu"
#[2] "thank god its friday , m goin to have a blast"
#[3] "she is getting anorexic day by day"