0
text = c("Hello abc01","Go to abc02")
value = c(0,1)
symbol=c("abc01","abc02")
df1 = data.frame(text)
df2 = data.frame(symbol,value)

I want to replace the symbols contained as text in df1 with the corresponding value in df2, to obtain: 'Hello 0', 'Go to 1'.

Typically for string-replacement I used gsub(pattern, replacement, x)

Ex: If I want to replace "abc01" and "abc02" with "OK":

df1 = apply(df1,2,function(x) gsub("abc[0-9]{2}","OK",x))

My idea is to use a function in replace section:

gsub(df1,2,function(x) gsub("(abc)", Support(KKK),x)

in which I'll do the substitution, but I don't know how I can passing as argument KKK, the matching-strings determined(abc01,abc02).

M.T.
  • 51
  • 1
  • 3
  • 1
    Welcome to Stack Overflow. Please have a look at [link1](http://stackoverflow.com/help/how-to-ask) and [link2](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and revise your question accordingly. – Sotos Aug 04 '16 at 10:17
  • 1
    Could try `library(gsubfn) ; gsubfn("abc0(\\d)", ~ as.integer(x) - 1, text)` – David Arenburg Aug 04 '16 at 11:25
  • @DavidArenburg Good catch. In that case, maybe `stri_replace_all_fixed(text, df2$symbol, df2$value, vectorize_all = FALSE)`? – Jota Aug 04 '16 at 13:16
  • @Jota, Yeah it would be the ideal solution I guess. No need to use the `data.frame` neither. Just `stringi::stri_replace_all_fixed(text, symbol, value, vectorize_all = FALSE)`. You could post that. – David Arenburg Aug 04 '16 at 13:19
  • @DavidArenburg Thank you all for your help. This is a simple example, but in my research I need to keep everything in dataframe. My solution: `(library(gsubfn) ; f = function(x) {return (subset(df2,subset=(symbol==x),select=value))} ; df1 = apply(df1,2,function(x) gsubfn("abc[0-9]{2}", f, x))` – M.T. Aug 04 '16 at 14:12
  • Why can't you simply use @Jota s solution? It is simple and very efficient – David Arenburg Aug 04 '16 at 14:14
  • @DavidArenburg 'cause in my dataframe df1 has more than one column, and do `df1 = stri_replace_all_fixed(text, symbol, value, vectorize_all = FALSE)` it causes loss of information. – M.T. Aug 04 '16 at 17:22
  • @M.T. you would assign the results to a single column (e.g. `df1[["newText"]] <- stri_replace_blah_blah(blah)`), and not overwrite the entire dataframe. – Jota Aug 04 '16 at 19:34
  • @Jota Thanks , I started using R from this morning. – M.T. Aug 04 '16 at 21:36

2 Answers2

0

Here is an idea (not as slick as the one in comments). What this does, it basically replaces the last word of df1$text with the df2$value of the matched df2$symbol

sapply(df1$text, function(i) 
       gsub(paste(df2$symbol, collapse = '|'), 
                df2$value[match(sub('^.* ([[:alnum:]]+)$', '\\1', i), df2$symbol)], i))

#[1] "Hello 0" "Go to 1" 

P.S. I borrowed the sub('^.* ([[:alnum:]]+)$', '\\1', i) from here

Community
  • 1
  • 1
Sotos
  • 51,121
  • 6
  • 32
  • 66
0
df1[["text"]] <- stri_replace_all_fixed(text, symbol, value, vectorize_all = FALSE)

Thanks Jota for the solution.

M.T.
  • 51
  • 1
  • 3