[R] regexpr virtue

Petr PIKAL petr.pikal at precheza.cz
Wed Jul 29 15:43:20 CEST 2009


Thank you

Gabor Grothendieck <ggrothendieck at gmail.com> napsal dne 29.07.2009 
15:20:40:

> Here are a couple of possibilities:
> 
> sub(".*\\.", "", s)

This seems to be the target. Can you be so kind and translate for me what 
it really does?

".* matches several dots?
\\. what is this?

I thought sub finds a match and replaces "replacement" in a string. But 
from this solution it seems that the command replaces all characters in a 
string with empty string and leave only value after three or two dots? Am 
I right?

Best regards
Petr



> 
> library(gsubfn)
> strapply(s, "[0-9]*$", simplify = c)
> 
> 
> 
> On Wed, Jul 29, 2009 at 9:10 AM, Petr PIKAL<petr.pikal at precheza.cz> 
wrote:
> > Hi all
> >
> > I have got something like that (actually those are column names)
> >
> > [51] "X19.2.300b...80"          "X19.2.400v...80" "X19.2.400b...80"
> > "X19.2.300v...90"          "X19.2.300b...90"
> > [56] "X19.2.400v...90"          "X19.2..400b..90" "X19.2.300v...100"
> > "X19.2.300b...100"         "X19.2.400v...100"
> >
> > in character vector. I would like to get last n figures from this
> > character vector but my regexpr unerstanding is inferior and I did not
> > succeed.
> > Please, is there anybody who can extract those n digits from end of 
each
> > character string? If there were only 2 digits I could use substr but 
there
> > can be one, two or three digits.
> >
> > I tried several variations of
> >
> > gsub("0:9$", "\\1" ,names(foto)[10])
> >
> > but either got whole string or an error.
> >
> > Best regards
> > Petr
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >




More information about the R-help mailing list