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" 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] "_" "___" "_____" "_____" Am I doing something wrong? Samir.> versionplatform i386-pc-mingw32 arch x86 os Win32 system x86, Win32 status major 1 minor 2.0 year 2000 month 12 day 15 language R Samir Mishra ==========================================Please send replies to : sqmishra at acm.org ========================================== _______________________________________________________ Send a cool gift with your E-Card http://www.bluemountain.com/giftcenter/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> From: Thomas Vogels <tov at ece.cmu.edu> > Date: 17 Jan 2001 12:26:56 -0500 > > > Prof Brian Ripley <ripley at stats.ox.ac.uk> writes: > > <snip> > > `.' 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 .... > </snip> > > Since perl is a prerequisite for the installation of R, you might > assume that its man pages are also available.Unfortunately it isn't, for a binary installation (and this was on Windows).> 'man perlre' gives an > overview of the special characters in the subsection "Regular > Expressions". So, could you refer the reader of the help page for > gsub and friends to the man page of perlre? (I don't what you would > do for the Windows people...)But these are POSIX not Perl regexps. The latter are more powerful, and have more special characters, AFAIK. -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._