[R] removing all non-numeric characters from a string, but not "."

David Winsemius dwinsemius at comcast.net
Tue Jul 26 23:40:22 CEST 2016


> On Jul 26, 2016, at 2:28 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:
> 
> gsub("[^0-9]", "", x)


?regex

I think you might be bit embarrassed because it seems pretty obvious once you know that character class elements like "." don't need to be escaped so it's just this:

> gsub("[^0-9.]", "", x)
[1] "84"     "293.04" "12.5"  

You might want to add in some separator if you are processing expression this way.

> gsub("[^0-9., ]", "", gsub( "[-+*/]", " , ", x) )
[1] "  ,  84"              "  ,  293.04  ,  1200" "  12.5"  

-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list