2020-10-30

Match and replace words in char-vector

I have a vector with text lines in it, like this:

text<-c("Seat 1: 7e7389e3 ($2 in chips)","Seat 3: 6786517b ($1.67 in chips)","Seat 4: 878b0b52 ($2.16 in chips)","Seat 5: a822375 ($2.37 in chips)","Seat 6: 7a6252e6 ($2.51 in chips)")

And I have to replace some words with other words, that i have in a dataframe like this:

df<-data.frame(codigo=c("7e7389e3","6786517b","878b0b52","a822375","7a6252e6"),
name=c("lucas","alan","ivan","lucio","donald"))

So I would like to 1) Grab the first line of "text" 2) Check if there is any word to replace in df 3) Replace it 4) Do the same with the next "text" line and so on. In order to have something like this:

[1] "Seat 1: lucas ($2 in chips)"
[2] "Seat 3: alan ($1.67 in chips)"
[3] "Seat 4: ivan ($2.16 in chips)"
[4] "Seat 5: lucio ($2.37 in chips)"
[5] "Seat 6: donald ($2.51 in chips)"

There is any formula to do this?



from Recent Questions - Stack Overflow https://ift.tt/2HKacMU
https://ift.tt/eA8V8J

No comments:

Post a Comment