[R] match and substitute two variables

irene ireneruberto at yahoo.it
Mon Dec 3 16:32:15 CET 2012


Hello, 
I have two variables (of different length and from two different data
frames):

code<- c("101001",  "1032", "102", "101001", "102", "1032");
name<- c("101001 Alta", "102 Bassa", "1032 Media");

and I would like to substitute the first variable with the  second variable
according to their shared numerical part, thus obtaining the following
result:

code.new
"101001 Alta"  "1032 Media" "102 Bassa""101001 Alta" "102 Bassa" "1032
Media"

I tried using: <- sapply(code, gsub, pattern="\\d+", replacement=name) but
the replacement cannot be of length more than one, thus my output is only
"101001 Alta" "101001 Alta"... I am not sure how to get the right answer...

Thank you!



--
View this message in context: http://r.789695.n4.nabble.com/match-and-substitute-two-variables-tp4651893.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list