Re: [R] Regular expressions & sub

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Fri 19 Aug 2005 - 05:17:58 EST

Dirk Eddelbuettel <edd@debian.org> writes:

> Bernd Weiss <bernd.weiss <at> uni-koeln.de> writes:
> > I am struggling with the use of regular expression. I got
> >
> > > as.character(test$sample.id)
> > [1] "1.11" "10.11" "11.11" "113.31" "114.2" "114.3" "114.8"
> >
> > and need
> >
> > [1] "11" "11" "11" "31" "2" "3" "8"
> >
> > I.e. remove everything before the "." .
>
> Define the dot as the hard separator, and allow for multiple digits before it:
>
> > sample.id <- c("1.11", "10.11", "11.11", "113.31", "114.2", "114.3", "114.8")
> > gsub("^[0-9]*\.", "", sample.id)
> [1] "11" "11" "11" "31" "2" "3" "8"

Or, more longwinded, but with less assumptions about what goes before the dot:

> gsub("^.*\\.(.*)$","\\1",sample.id)

[1] "11" "11" "11" "31" "2" "3" "8"

or,

> gsub("^.*\\.([^.]*)$","\\1",sample.id)
[1] "11" "11" "11" "31" "2" "3" "8"

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Fri Aug 19 05:22:50 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:39:53 EST