Hello,

Try the following.

for(nm in name){
    code[grep(gsub("[ [:alpha:]]+", "", nm), code)] <- nm
}
code


Hope this helps,

Rui Barradas
Em 03-12-2012 15:32, irene escreveu:
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.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to