[R] match() & seq()

Roger Bivand Roger.Bivand at nhh.no
Mon Jan 30 15:44:10 CET 2006


On Mon, 30 Jan 2006, Jacques VESLOT wrote:

> sorry if it has already been discussed but i can't understand this:
> 
>  > seq(0.1,1,by=0.1)
>  [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
>  > match(0.1,seq(0.1,1,by=0.1))
> [1] 1
>  > match(0.2,seq(0.1,1,by=0.1))
> [1] 2
>  > match(0.3,seq(0.1,1,by=0.1))
> [1] NA
>  > match(0.4,seq(0.1,1,by=0.1))
> [1] 4

FAQ 7.31
http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

> print(seq(0.1,1,by=0.1), digits=20)
 [1] 0.10000000000000000555 0.20000000000000001110 0.30000000000000004441
 [4] 0.40000000000000002220 0.50000000000000000000 0.59999999999999997780
 [7] 0.70000000000000006661 0.80000000000000004441 0.90000000000000002220
[10] 1.00000000000000000000
> match(c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9), seq(0.1,1,by=0.1))
[1]  1  2 NA  4  5  6 NA  8  9
> all.equal(c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0), seq(0.1,1,by=0.1), 
+ tolerance = .Machine$double.eps ^ 2)
[1] "Mean relative  difference: 1.665335e-16"
> all.equal(c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0), seq(0.1,1,by=0.1))
[1] TRUE


> 
>  > R.version
>          _             
> platform i386-pc-mingw32
> arch     i386          
> os       mingw32       
> system   i386, mingw32 
> status                 
> major    2             
> minor    2.1           
> year     2005          
> month    12            
> day      20            
> svn rev  36812         
> language R
> 
> ______________________________________________
> 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
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list