Full_Name: Jerome Asselin Version: 1.5.1 OS: linux redhat 7.2 Submission from: (NULL) (142.103.173.179) gsub() return different answers depending on how the input variables were created. Here is an example of code that replicates the problem. The vectors y and yy appear to be the same, but gsub() doesn't return the same answer. It should remove all the blanks when I use the vector y, but it doesn't remove them all. I have also tested that the same problem occurs if sub() is used instead of gsub(). x <- structure(c(1, 3, 4, 5), class = "factor", .Label = c("1 ", "2 ", "3 ", "4 ", "5 ")) y <- as.character(x) y gsub(" ","",y) yy <- c("1 ","3 ","4 ","5 ") yy gsub(" ","",yy) Below is the output that I have when I execute those commands.> x <- structure(c(1, 3, 4, 5), class = "factor",+ .Label = c("1 ", "2 ", "3 ", "4 ", "5 "))> y <- as.character(x) > y[1] "1 " "3 " "4 " "5 "> gsub(" ","",y)[1] "1" "3" "4 " "5 "> > yy <- c("1 ","3 ","4 ","5 ") > yy[1] "1 " "3 " "4 " "5 "> gsub(" ","",yy)[1] "1" "3" "4" "5" -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Those are not blanks! You have character <A0> in there. less on your email gives me> x <- structure(c(1, 3, 4, 5), class = "factor",+ .Label = c("1 ", "2 ", "3 ", "4<A0>", "5<A0>"))> y <- as.character(x) > y[1] "1 " "3 " "4<A0>" "5<A0>"> gsub(" ","",y)[1] "1" "3" "4<A0>" "5<A0>" which is correct. Pilot error, I believe .... On Tue, 23 Jul 2002 jerome@hivnet.ubc.ca wrote:> Full_Name: Jerome Asselin > Version: 1.5.1 > OS: linux redhat 7.2 > Submission from: (NULL) (142.103.173.179) > > > > gsub() return different answers depending on how the input > variables were created. Here is an example of code that > replicates the problem. The vectors y and yy appear to be > the same, but gsub() doesn't return the same answer. > It should remove all the blanks when I use the vector y, > but it doesn't remove them all. I have also tested that > the same problem occurs if sub() is used instead of gsub(). > > x <- structure(c(1, 3, 4, 5), class = "factor", > .Label = c("1 ", "2 ", "3 ", "4 ", "5 ")) > y <- as.character(x) > y > gsub(" ","",y) > > yy <- c("1 ","3 ","4 ","5 ") > yy > gsub(" ","",yy) > > > Below is the output that I have when I execute those commands. > > > > x <- structure(c(1, 3, 4, 5), class = "factor", > + .Label = c("1 ", "2 ", "3 ", "4 ", "5 ")) > > y <- as.character(x) > > y > [1] "1 " "3 " "4 " "5 " > > gsub(" ","",y) > [1] "1" "3" "4 " "5 " > > > > yy <- c("1 ","3 ","4 ","5 ") > > yy > [1] "1 " "3 " "4 " "5 " > > gsub(" ","",yy) > [1] "1" "3" "4" "5" > > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley@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 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 23 Jul 2002 jerome@hivnet.ubc.ca wrote:> Full_Name: Jerome Asselin > Version: 1.5.1 > OS: linux redhat 7.2 > Submission from: (NULL) (142.103.173.179) > > > > gsub() return different answers depending on how the input > variables were created. Here is an example of code that > replicates the problem. The vectors y and yy appear to be > the same, but gsub() doesn't return the same answer. > It should remove all the blanks when I use the vector y, > but it doesn't remove them all. I have also tested that > the same problem occurs if sub() is used instead of gsub(). > > x <- structure(c(1, 3, 4, 5), class = "factor", > .Label = c("1 ", "2 ", "3 ", "4 ", "5 ")) > y <- as.character(x) > y > gsub(" ","",y) > > yy <- c("1 ","3 ","4 ","5 ") > yy > gsub(" ","",yy) > >I can't reproduce this (1.5.1, Debian Linux). I get> y[1] "1 " "3 " "4 " "5 "> yy[1] "1 " "3 " "4 " "5 "> gsub(" ","",y)[1] "1" "3" "4" "5"> gsub(" ","",yy)[1] "1" "3" "4" "5"> identical(y,yy)[1] TRUE> identical(gsub(" ","",y),gsub(" ","",yy))[1] TRUE There used to be a possible bug where the R length and C length of a string would be different (PR#1724), which might cause that sort of thing, but the known manifestations of it were removed in 1.5.1 -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._