[R] removing characters from a string

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Tue Apr 12 15:14:27 CEST 2005


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




More information about the R-help mailing list