[R] converting character strings to numbers

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 15 08:04:14 CEST 2007


There is no support for 'thousands separators' in R's input/conversion 
routines, mainly because C has no support either (not even for output in 
the C standard).  We could of course add our own layer, but as far as I am 
aware this is the first time this has come up.

On Thu, 14 Jun 2007, Andrew J Tyre wrote:

> I have a comma delimited text file in which many columns of numbers are
> also quoted and have commas as well as decimals. I was surprised to find
> read.csv() didn't import this seamlessly, even after messing around with
> the colClasses argument. I did find a solution to convert the character
> strings after reading them in, but wonder if there isn't a better one I
> overlooked.
>
> test = c("10,522.5","11,768.9","11,354.3")
> as.numeric(test) # fails
> as.numeric(gsub(",","",test)) # works
>
> Any suggestions? Or is this as good as it gets? I'm not complaining ...
> just curious!
>
> Drew Tyre
>
> School of Natural Resources
> University of Nebraska-Lincoln
> 416 Hardin Hall, East Campus
> Lincoln, NE 68583-0974
> phone: +1 402 472 4054 fax: +1 402 472 2946
> email: atyre2 at unl.edu
> http://snr.unl.edu/tyre
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list