[R] remove numbers from string of characters

(Ted Harding) ted.harding at wlandres.net
Mon Oct 18 17:31:24 CEST 2010


On 18-Oct-10 15:03:22, Henrique Dallazuanna wrote:
> See agrep function:
> 
> agrep("Frozen Niagara Entrance", "001a Frozen Niagara Entrance") > 0
> 
> To remove the numbers:
> 
> gsub("\\d", "", "001a Frozen Niagara Entrance")

That results in "a Frozen Niagara Entrance", which is not
what he said he wants (his "numbers" are not purely digital)!

You need sub() and "[:[alnum:]]* " as I suggested previously.
Also "\\w* " would work, since this "\\w" is equivalent to
"[[:alnum:]]":

  sub("[[:alnum:]]* ", "", "001a Frozen Niagara Entrance")
  # [1] "Frozen Niagara Entrance"

  sub("\\w* ", "", "001a Frozen Niagara Entrance")
  # [1] "Frozen Niagara Entrance"







> On Mon, Oct 18, 2010 at 12:58 PM, <Kurt_Helf at nps.gov> wrote:
>> Greetings
>> I want to remove numbers  from a string of characters that
>> identify sites so that I can merge two data frames. For example,
>> a site in one frame is called "001a Frozen Niagara Entrance"
>> whereas the same site in the other data frame is called
>> "Frozen Niagara Entrance". It seems to me the easiest thing
>> to do would be to remove the numbers from the first data
>> frame so the two will match. How do I go about removing those
>> numbers?
>> Thanks in advance.
>> Cheers
>> Kurt

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Oct-10                                       Time: 16:31:20
------------------------------ XFMail ------------------------------



More information about the R-help mailing list