[R] match() & seq()

Jacques VESLOT jacques.veslot at cirad.fr
Tue Jan 31 08:21:20 CET 2006


Thanks Roger, Andy and Dimitris...
though i am familiar with this behaviour **in some cases**, i couldn't 
catch - yesterday evening - why it matched with 0.4, and not with 0.3; 
of course these numbers are not integers ! but i believed match() deals 
with such equalities.
i will have a look at the article mentionned in FAQ 7.31 since 
everything is not clear for me yet .
 > identical(0.4-0.3,0.1)
[1] FALSE
 > all.equal(0.4-0.3,0.1)
[1] TRUE
 
jacques


Roger Bivand a écrit :

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




More information about the R-help mailing list