[R] converting character strings to numbers

From: Andrew J Tyre <atyre2_at_unlnotes.unl.edu>
Date: Thu, 14 Jun 2007 20:59:33 -0500

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

        [[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. Received on Fri 15 Jun 2007 - 02:04:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 15 Jun 2007 - 06:31:58 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.