[R] Numeric data not numeric in .csv file

Jim Lemon jim at bitwrit.com.au
Wed May 2 10:34:35 CEST 2012


On 05/02/2012 10:47 AM, Eve Proper wrote:
> I am a raw novice to R, playing around with a mini .csv dataset created in
> Excel. I can read it in and the data looks OK in Excel and upon initial
> inspection in R:
>
> hikes<- read.csv("/Users/eproper/Desktop/hikes.csv", header=TRUE)
> print(hikes)
>
> does exactly what it is supposed to do.
>
> Two of the variables are genuine strings, but the others ought to be
> numeric, and R will calculate their min, max etc. However, is.numeric
> returns FALSE for all of them; storage.mode returns "language." as.numeric
> returns "Error: 'pairlist' object cannot be coerced to type 'double'." In
> what I suspect is a related problem, any command that calls for a variable
> name requires an initial ~ to work. That is, instead of plot(miles) I have
> to use plot(~miles).
>
> No doubt there is some very elementary mistake I am making, but I can't
> figure it out. Any help would be appreciated.
>
Hi Eve,
Have you tried "as.numeric" on them? As Jeff suggested, you may be 
importing spaces along with the digits or some other character that 
changes the class of the variable. Also note that the default behavior 
of functions like read.csv is to coerce all of the values in a column of 
the resulting data frame to the "lowest common denominator". If you have 
one text value in a column of numbers, you usually get factor values. 
This is due to the restriction that all values in a column must be of 
the same class (data type).

Jim



More information about the R-help mailing list