[R] Numeric data not numeric in .csv file

R. Michael Weylandt michael.weylandt at gmail.com
Wed May 2 15:44:04 CEST 2012


On Wed, May 2, 2012 at 4:34 AM, Jim Lemon <jim at bitwrit.com.au> wrote:
> 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
>

If accidentally coerced to factors, the OP might prefer

as.numeric(as.character(x))

to get the apparent numeric values rather than the internal ones.

Also seconded Jeff's advice about str()

Michael



More information about the R-help mailing list