[R] scan seems to modify the data

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 31 20:05:10 CEST 2004


On Wed, 31 Mar 2004, Duncan Murdoch wrote:

> 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).

Because the precision applies to the smallest of the numbers.

-- 
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