Judith Flores
2008-Feb-14 18:57 UTC
[R] Replacing a character string when finding substring match
Dear R-experts, I need to replace the values of a vector(tx) with a word ('Vehicle') when the value of the vector contains the word 'vehicle'. Sometimes, the value could be 'MCT vehicle', or 'control-vehicle', etc. I tried gsub like this treatment<-gsub('vehicle','Vehicle', tx, ignore.case=T) But then I end up with values like 'MCT Vehicle' or 'control-Vehicle'. I want the values to be one word: 'Vehicle'. Thank you in advance for any help. ____________________________________________________________________________________ Be a better friend, newshound, and
Henrique Dallazuanna
2008-Feb-14 19:04 UTC
[R] Replacing a character string when finding substring match
Try this: x[grep("vehicle", x, ignore=T)] <- "Vehicle" On 14/02/2008, Judith Flores <juryef at yahoo.com> wrote:> Dear R-experts, > > I need to replace the values of a vector(tx) with a > word ('Vehicle') when the value of the vector contains > the word 'vehicle'. Sometimes, the value could be 'MCT > vehicle', or 'control-vehicle', etc. > I tried gsub like this > > treatment<-gsub('vehicle','Vehicle', tx, > ignore.case=T) > > But then I end up with values like 'MCT Vehicle' or > 'control-Vehicle'. I want the values to be one word: > 'Vehicle'. > > > > Thank you in advance for any help. > > > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O