Re: [R] removing characters from a string

From: Barry Rowlingson <B.Rowlingson_at_lancaster.ac.uk>
Date: Tue 12 Apr 2005 - 23:14:27 EST

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

  Use a ^ to negate a character range:

 > gsub("[^0-9]", "", "aB9c81")
[1] "981"

Baz



R-help@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 Received on Tue Apr 12 23:46:01 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:07 EST