1

I want to find and replace a list of words with another list of words.

Say my data is

1) plz suggst med for flu
2) tgif , m goin to have a blast
3) she is getting anorexic day by day

List of words to be replaced are

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

I would like to have 2 lists, list "A" --a list of words to be found and list "B" --a list of words to be replaced with. So that I can keep adding terms to these lists as and when required. I need a mechanism to search for all the words in list "A" and then replace it with corresponding words in list "B".

What is the best way to achieve this in R. Thanks in advance.

Jaap
  • 81,064
  • 34
  • 182
  • 193
user1946217
  • 1,733
  • 6
  • 31
  • 40

2 Answers2

1

have a look at ?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)
Thierry
  • 18,049
  • 5
  • 48
  • 66
  • yes i have knowledge about gsub() and str_replace(), but i have to explicitly mention each word and its replacement. I would like to have 2 lists, list "A" --a list of words to be found and list "B" --a list of words to be replaced with. So that I can keep adding terms to these lists as and when required. I need a mechanism to search for all the words in list "A" and then replace it with corresponding words in list "B". – user1946217 Jun 05 '14 at 11:27
1

Try this:

#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" 
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 1
    You should try an exact word match like in this [post](http://stackoverflow.com/questions/23211923/remove-exact-match-word-from-a-string), it would avoid the problem with `suggest`/`suggestst` – Victorp Jun 05 '14 at 12:40