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]]
Hi On 31 Oct 2006 at 22:42, tom soyer wrote: Date sent: Tue, 31 Oct 2006 22:42:40 -0600 From: "tom soyer" <tom.soyer at gmail.com> To: r-help <r-help at 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] 805Levels 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 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.Petr Pikal petr.pikal at precheza.cz
Prof Brian Ripley
2006-Nov-01 08:09 UTC
[R] Problem with data type recognition and conversion
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
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
Ricardo Arias Brito
2006-Nov-01 11:47 UTC
[R] Problem with data type recognition and conversion
Hi, The plot function not produces graphics in linux. The msg: Error in X11(): it was not possible to find no source X11 Verifity if the way of sources is correct. Any ideas? Thanks, Ricardo __________________________________________________ [[alternative HTML version deleted]]