[R] Pattern Matching help

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 17 13:17:15 CET 2001


> Date: Wed, 17 Jan 2001 04:04:31 -0800 (PST)
> From: Samir Mishra <sqmishra at excite.com>
> 
> Thanks in advance for any help I can get on this.
> 
> I'm trying to change variable names between 2 systems - R and old SPSS,
> Oracle. I'm using the grep() and gsub() commands but I'm getting the
> following result - 
> 
> > test.dat <- c("a", "b.c", "d.e.f", "p_q,r")
> 
> > test.dat
> [1] "a"     "b.c"   "d.e.f" "p_q,r"
> 
> > grep(",", test.dat, value = TRUE, extended = FALSE)
> [1] "p_q,r"
> 
> > grep("_", test.dat, value = TRUE, extended = FALSE)
> [1] "p_q,r"
> 
> > #NOT WHAT I WANT, I EXPECT TO GET ONLY ELEMENTS 2 & 3 RETURNED
> > grep(".", test.dat, value = TRUE, extended = FALSE)
> [1] "a"     "b.c"   "d.e.f" "p_q,r"

Try
> grep("\\.", test.dat, value = TRUE)
[1] "b.c"   "d.e.f"

> And similarly, with gsub(), I get -
> > gsub(",", "_", test.dat, extended = FALSE)
> [1] "a"     "b.c"   "d.e.f" "p_q_r"
> 
> > gsub("_", ".", test.dat, extended = FALSE)
> [1] "a"     "b.c"   "d.e.f" "p.q,r"
> 
> > #NOT WHAT I WANT
> > gsub(".", "_", test.dat, extended = FALSE) #NOT WHAT I WANT
> [1] "_"     "___"   "_____" "_____"

But

> > gsub("\\.", "_", test.dat, extended = FALSE)
[1] "a"     "b_c"   "d_e_f" "p_q,r"


> Am I doing something wrong? 

`.' is a special character in a regexp.   You have to escape it by `\', and
that needs to be escaped in R.

Perhaps we need to say more on the help page, but I am reluctant to
try to define the meaning of `regular expression' there ....


-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list