[R] scan seems to modify the data

Duncan Murdoch dmurdoch at pair.com
Wed Mar 31 19:50:33 CEST 2004


On Wed, 31 Mar 2004 12:24:38 -0500, Stephane DRAY
<stephane.dray at umontreal.ca> wrote :

>Hello list,
>I have used scan function to import data into R. I have done some analysis 
>and find strange results. I have found my problem : when importing data 
>with scan, this can slightly modify the data :
>
> > write(c(0.251,3.399,-0.481,0.266),"essai.txt")
> > scan("essai.txt")
>Read 4 items
>[1]  0.251  3.399 -0.481  0.266
> > print(scan("essai.txt"),17)
>Read 4 items
>[1]  0.25100000000000000  3.39900000000000000 
>-0.48099999999999998  0.26600000000000001
>
>
>
>Is it normal ? Is it a bug ?

I think it's normal.  Floating point formats aren't exact except for
fractions with only powers of 2 in the denominator.  There is no way
to represent any of your values in the formats that R uses without
slight errors.

I do notice one oddity in the print routines in R:

> x<-scan()
1: 0.266
2: 0.251
3: 
Read 2 items
> print(x,17)
[1] 0.26600000000000001 0.25100000000000000
> x<-scan()
1: 0.266
2: 
Read 1 items
> print(x,17)
[1] 0.266

I don't know why the second print() prints 0.266 differently from the
first one.  (This is in the 1.9.0 beta in Windows).

Duncan Murdoch




More information about the R-help mailing list