[R] Pattern Matching help
Uwe Ligges
ligges at statistik.uni-dortmund.de
Wed Jan 17 13:43:47 CET 2001
Samir Mishra wrote:
>
> 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"
grep("\\.", test.dat, value = TRUE, extended = FALSE)
> 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] "_" "___" "_____" "_____"
gsub("\\.", "_", test.dat, extended = FALSE)
> Am I doing something wrong?
Yes. Think about the "." in regular expressions.
Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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