[R] removing characters from a string

Deepayan Sarkar deepayan at stat.wisc.edu
Tue Apr 12 15:14:13 CEST 2005


On Tuesday 12 April 2005 08:03, Liaw, Andy wrote:
> Just gsub() non-numerics with ""; e.g.:
> > gsub("[a-zA-Z]", "", "aB9c81")
>
> [1] "981"
>
> [I'm really bad in regular expressions, and don't know how to
> construct "non-numerics".]

(So am I, but) perhaps "[^0-9]".

Deepayan

>
> Andy
>
> > From: Vivek Rao
> >
> > Is there a simple way in R to remove all characters
> > from a string other than those in a specified set? For
> > example, I want to keep only the digits 0-9 in a
> > string.
> >
> > In general, I have found the string handling abilities
> > of R a bit limited. (Of course it's great for stats in
> > general). Is there a good reference on this? Or should
> > R programmers dump their output to a text file and use
> > something like Perl or Python for sophisticated text
> > processing?
> >
> > I am familiar with the basic functions such as nchar,
> > substring, as.integer, print, cat, sprintf etc.
> >
> > ______________________________________________
> > R-help at 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
>
> ______________________________________________
> R-help at 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




More information about the R-help mailing list