[R] removing characters from a string

Martin Maechler maechler at stat.math.ethz.ch
Tue Apr 12 15:15:21 CEST 2005


>>>>> "Vivek" == Vivek Rao <rvivekrao at yahoo.com>
>>>>>     on Tue, 12 Apr 2005 05:54:55 -0700 (PDT) writes:

    Vivek> Is there a simple way in R to remove all characters
    Vivek> from a string other than those in a specified set? For
    Vivek> example, I want to keep only the digits 0-9 in a
    Vivek> string.

    Vivek> In general, I have found the string handling abilities
    Vivek> of R a bit limited. (Of course it's great for stats in
    Vivek> general). Is there a good reference on this? Or should
    Vivek> R programmers dump their output to a text file and use
    Vivek> something like Perl or Python for sophisticated text
    Vivek> processing?

    Vivek> I am familiar with the basic functions such as nchar,
    Vivek> substring, as.integer, print, cat, sprintf etc.

It depends on your "etc":

The above is pretty trivial using gsub(),
but since you sound sophisticated enough to proclaim missing R
abilities, I leave the exercise to you.

Martin




More information about the R-help mailing list