Jason Rupert
2009-Mar-12 18:54 UTC
[R] R grep & gsub issue - sign seems to be causing an issue...
I would like to use grep and gsub to manipulate a vector to make the names used consistent, i.e. reduce a level or two. However, here is what I found when I attempted to use grep and gsub:> tmp_test<-c("House 1 Plot Plus +100","House 2 Plot Plus +100","House 3 Plot Plus -100","House 4 Plot Plus -100","House 1 Plus +100","House 2 Plus +100","House 3 Plus -100","House 4 Plus -100")> gsub(tmp_test[2], tmp_test[1], tmp_test)[1] "House 1 Plot Plus +100" "House 2 Plot Plus +100" "House 3 Plot Plus -100" "House 4 Plot Plus -100" "House 1 Plus +100" "House 2 Plus +100" [7] "House 3 Plus -100" "House 4 Plus -100"> grep(tmp_test[1],tmp_test)integer(0) However,> gsub(tmp_test[3], tmp_test[1], tmp_test)[1] "House 1 Plot Plus +100" "House 2 Plot Plus +100" "House 1 Plot Plus +100" "House 4 Plot Plus -100" "House 1 Plus +100" "House 2 Plus +100" [7] "House 3 Plus -100" "House 4 Plus -100" Thanks for any feedback that can be provided.
Duncan Murdoch
2009-Mar-12 19:24 UTC
[R] R grep & gsub issue - sign seems to be causing an issue...
Jason Rupert wrote:> I would like to use grep and gsub to manipulate a vector to make the names used consistent, i.e. reduce a level or two. >This is dangerous: grep and gsub use regular expressions, so a lot of characters (see ?regexp for the list) have special meanings. For the kind of substitutions you want, just do things like newlist <- tmp_test newlist[newlist == tmp_test[2]] <- tmp_test[1] Duncan Murdoch> However, here is what I found when I attempted to use grep and gsub: > > >> tmp_test<-c("House 1 Plot Plus +100","House 2 Plot Plus +100","House 3 Plot Plus -100","House 4 Plot Plus -100","House 1 Plus +100","House 2 Plus +100","House 3 Plus -100","House 4 Plus -100") >> > > >> gsub(tmp_test[2], tmp_test[1], tmp_test) >> > [1] "House 1 Plot Plus +100" "House 2 Plot Plus +100" "House 3 Plot Plus -100" "House 4 Plot Plus -100" "House 1 Plus +100" "House 2 Plus +100" > [7] "House 3 Plus -100" "House 4 Plus -100" > > >> grep(tmp_test[1],tmp_test) >> > integer(0) > > However, > >> gsub(tmp_test[3], tmp_test[1], tmp_test) >> > [1] "House 1 Plot Plus +100" "House 2 Plot Plus +100" "House 1 Plot Plus +100" "House 4 Plot Plus -100" "House 1 Plus +100" "House 2 Plus +100" > [7] "House 3 Plus -100" "House 4 Plus -100" > > Thanks for any feedback that can be provided. > > ______________________________________________ > 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. >
Prof Brian Ripley
2009-Mar-12 19:44 UTC
[R] R grep & gsub issue - sign seems to be causing an issue...
On Thu, 12 Mar 2009, Duncan Murdoch wrote:> Jason Rupert wrote: >> I would like to use grep and gsub to manipulate a vector to make the names >> used consistent, i.e. reduce a level or two. > > This is dangerous: grep and gsub use regular expressions, so a lot of > characters (see ?regexp for the list) have special meanings.Unless argument fixed=TRUE< which is often useful.> For the kind of substitutions you want, just do things like > > newlist <- tmp_test > newlist[newlist == tmp_test[2]] <- tmp_test[1] > > Duncan Murdoch >> However, here is what I found when I attempted to use grep and gsub: >> >>> tmp_test<-c("House 1 Plot Plus +100","House 2 Plot Plus +100","House 3 >>> Plot Plus -100","House 4 Plot Plus -100","House 1 Plus +100","House 2 Plus >>> +100","House 3 Plus -100","House 4 Plus -100") >>> >> >> >>> gsub(tmp_test[2], tmp_test[1], tmp_test) >>> >> [1] "House 1 Plot Plus +100" "House 2 Plot Plus +100" "House 3 Plot Plus >> -100" "House 4 Plot Plus -100" "House 1 Plus +100" "House 2 Plus +100" >> [7] "House 3 Plus -100" "House 4 Plus -100" >> >>> grep(tmp_test[1],tmp_test) >>> >> integer(0) >> >> However, >>> gsub(tmp_test[3], tmp_test[1], tmp_test) >>> >> [1] "House 1 Plot Plus +100" "House 2 Plot Plus +100" "House 1 Plot Plus >> +100" "House 4 Plot Plus -100" "House 1 Plus +100" "House 2 Plus +100" >> [7] "House 3 Plus -100" "House 4 Plus -100" >> >> Thanks for any feedback that can be provided. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595