[R] converting character strings to numbers

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jun 15 04:26:34 CEST 2007


See:

https://stat.ethz.ch/pipermail/r-help/2007-April/130912.html

On 6/14/07, Andrew J Tyre <atyre2 at unlnotes.unl.edu> 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.
>



More information about the R-help mailing list