Hi, Am I missing something in using regexps in R? Below, 'egrep' means invokation of the command from the shell prompt. # I have> as.character(block.dist.vals)[1] "1e+06" "2e+06" "5e+06" # that I wish to convert to: "1" "2" "5" # OK (R and egrep)> sub( "e.+06", "", as.character(block.dist.vals) )[1] "1" "2" "5" # OK (R), egrep will *not* match pattern> sub( "e\\+06", "", as.character(block.dist.vals) )[1] "1" "2" "5" # egrep will match pattern; R will not.> sub( "e\+06", "", as.character(block.dist.vals) )[1] "1e+06" "2e+06" "5e+06" As-far-as I can tell the last attempt should have worked, too. Could someone explain to me why it doesn't? I have R 1.7.0 on RedHat 9. Thanks in advance, Itay Furman ======================================================================== Fred Hutchinson Cancer Research Center email: itayf at fhcrc.org 1100 Fairview Avenue N., Mailstop D4-100 phone: +1 (206) 667 5921 P.O. Box 19024 fax: +1 (206) 667 2917 Seattle, WA 98109-1024
In R (and C) \ must be escaped in a character string. This is mentioned on the help page. On Tue, 30 Dec 2003, Itay Furman wrote:> > Hi, > > Am I missing something in using regexps in R? > Below, 'egrep' means invokation of the command from the shell > prompt. > > # I have > > as.character(block.dist.vals) > [1] "1e+06" "2e+06" "5e+06" > # that I wish to convert to: "1" "2" "5" > > # OK (R and egrep) > > sub( "e.+06", "", as.character(block.dist.vals) ) > [1] "1" "2" "5" > # OK (R), egrep will *not* match pattern > > sub( "e\\+06", "", as.character(block.dist.vals) ) > [1] "1" "2" "5" > > # egrep will match pattern; R will not. > > sub( "e\+06", "", as.character(block.dist.vals) ) > [1] "1e+06" "2e+06" "5e+06" > > As-far-as I can tell the last attempt should have worked, too. > Could someone explain to me why it doesn't? > > I have R 1.7.0 on RedHat 9. > > Thanks in advance, > Itay Furman > > ========================================================================> Fred Hutchinson Cancer Research Center > email: itayf at fhcrc.org 1100 Fairview Avenue N., Mailstop D4-100 > phone: +1 (206) 667 5921 P.O. Box 19024 > fax: +1 (206) 667 2917 Seattle, WA 98109-1024 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >-- 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
Just as an elaboration, this might help further clarify it. Spit displays each character in x -- one per line.> spit <- function(x) for(i in 1:nchar(x)) cat(i,substring(x,i,i),"\n") > spit("e\+06") # note that the resulting string does not contain \1 e 2 + 3 0 4 6> spit("e\\+06") # this time its there1 e 2 \ 3 + 4 0 5 6 Date: Tue, 30 Dec 2003 21:50:27 +0000 (GMT) From: Prof Brian Ripley <ripley at stats.ox.ac.uk> To: Itay Furman <itayf at fhcrc.org> Cc: <r-help at stat.math.ethz.ch> Subject: Re: [R] regexp problem on R 1.7.0 In R (and C) \ must be escaped in a character string. This is mentioned on the help page. On Tue, 30 Dec 2003, Itay Furman wrote:> > Hi, > > Am I missing something in using regexps in R? > Below, 'egrep' means invokation of the command from the shell > prompt. > > # I have > > as.character(block.dist.vals) > [1] "1e+06" "2e+06" "5e+06" > # that I wish to convert to: "1" "2" "5" > > # OK (R and egrep) > > sub( "e.+06", "", as.character(block.dist.vals) ) > [1] "1" "2" "5" > # OK (R), egrep will *not* match pattern > > sub( "e\\+06", "", as.character(block.dist.vals) ) > [1] "1" "2" "5" > > # egrep will match pattern; R will not. > > sub( "e\+06", "", as.character(block.dist.vals) ) > [1] "1e+06" "2e+06" "5e+06" > > As-far-as I can tell the last attempt should have worked, too. > Could someone explain to me why it doesn't? > > I have R 1.7.0 on RedHat 9. > > Thanks in advance, > Itay Furman > > ========================================================================> Fred Hutchinson Cancer Research Center > email: itayf at fhcrc.org 1100 Fairview Avenue N., Mailstop D4-100 > phone: +1 (206) 667 5921 P.O. Box 19024 > fax: +1 (206) 667 2917 Seattle, WA 98109-1024 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > >-- 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 ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help