[R] Problem with data type recognition and conversion
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Nov 1 09:09:38 CET 2006
Your 'numbers' are in fact a factor: note what it says about 'levels',
and that one level is '.'. So very likely there was a non-number ('.') in
that column of your input file.
Please study 'An Introduction to R' and familiarize yourself with factors.
typeof() is useful for basic types, but not for classed objects such as
factors.
On Tue, 31 Oct 2006, tom soyer wrote:
> Hi,
>
> I have a CSV file with two columns; the first column is date, second column
> is numbers. I used read.csv() to load the file into the variable temp.
> Somehow, R could not recognize my numbers as double. Instead, it thinks
> these numbers are integer even though they all have decimal points (isn't
> that strange?). The problem I ran into is that if I tried to convert the
> numbers to double using as.double, R doesn't give me the original value; e.g.
> 9.92 becomes 805 (see below).
>
>> temp[1,2]
> [1] 9.92
> 812 Levels: . 10.00 10.01 10.02 10.03 10.04 10.05 10.06 10.07 10.08
> 10.09 10.10 10.11 10.12 ... 9.99
>> typeof(temp[1,2])
> [1] "integer"
>> as.double(temp[1,2])
> [1] 805
>
> If I leave the numbers as integer, then I can't do arithmetic operations on
> them. Does anyone know what's going on?
>
> Thanks,
>
> Tom
>
> [[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