Re: [R] Problem with data type recognition and conversion

2006-11-01 Thread Petr Pikal
Hi

On 31 Oct 2006 at 22:42, tom soyer wrote:

Date sent:  Tue, 31 Oct 2006 22:42:40 -0600
From:   tom soyer [EMAIL PROTECTED]
To: r-help r-help@stat.math.ethz.ch
Subject:[R] Problem with data type recognition and conversion

 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
Levels always tell you it is a factor. Your csv file has an extra 
decimal point or something odd that forces R to convert second column 
to factor. You can transfer factor variable to numeric one by 

as.numeric(as.character(temp[,2]))

however I recommend you to go through your csv file (if it is not too 
big) and find the problematic item.

 
 If I leave the numbers as integer, then I can't do arithmetic
 operations on them. Does anyone know what's going on?

see ?factor

HTH
Petr
 
 Thanks,
 
 Tom
 
  [[alternative HTML version deleted]]
 
 __
 R-help@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.

Petr Pikal
[EMAIL PROTECTED]

__
R-help@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.


Re: [R] Problem with data type recognition and conversion

2006-11-01 Thread Prof Brian Ripley
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@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,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-help@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.


Re: [R] Problem with data type recognition and conversion

2006-11-01 Thread Jim Lemon
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).
 

The data seem to have been read as a factor, probably due to the wrong 
delimiter being supplied or assumed. Make sure that the delimiter in the 
file is the one specified on the read.* function. Another thing that 
might mess up the input is quote characters.

Jim

__
R-help@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.


Re: [R] Problem with data type recognition and conversion

2006-11-01 Thread tom soyer
Thanks Jim, Brian, and Petr. I found a non-number, ., in my CSV file that
prevented R from reading the data correctly. Once I got rid of it, R works
fine. Thanks!

On 11/1/06, Jim Lemon [EMAIL PROTECTED] wrote:

 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).
 
 
 The data seem to have been read as a factor, probably due to the wrong
 delimiter being supplied or assumed. Make sure that the delimiter in the
 file is the one specified on the read.* function. Another thing that
 might mess up the input is quote characters.

 Jim


[[alternative HTML version deleted]]

__
R-help@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.