Skip to content
Back to formatted view

Raw Message

Message-ID: <16987.51689.900870.284564@stat.math.ethz.ch>
Date: 2005-04-12T13:15:21Z
From: Martin Maechler
Subject: removing characters from a string
In-Reply-To: <20050412125455.86170.qmail@web31301.mail.mud.yahoo.com>

>>>>> "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