Hi, try this: paste(code,gsub("\\d+","",name)[match(code,gsub("\\D+","",name))],sep=" ") #[1] "101001 Alta" "1032 Media" "102 Bassa" "101001 Alta" "102 Bassa" #[6] "1032 Media"
A.K. ----- Original Message ----- From: irene <irenerube...@yahoo.it> To: r-help@r-project.org Cc: Sent: Monday, December 3, 2012 10:32 AM Subject: [R] match and substitute two variables 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.