[R] "strange" behaviour: recognition of decimal numbers by 'which'

J.delasHeras at ed.ac.uk J.delasHeras at ed.ac.uk
Tue Feb 2 15:49:37 CET 2010

It is a strange behaviour in that I did not expect it... but I am sure  
there is a simple explanation for it and it'll have to do with the way  
numbers are stored in R, but it's caught me by surprise and I don't  
find it obvious.

Here's a simplified example reproducing the behaviour I encountered:

I create an empty vector, and I fill it with a sequence of numbers: 0,  
0.005, 0.01, 0.015, 0.02, etc... until 0.1.
(In my real code this is just a way to store certain values when  
certain conditions are met etc etc)
Then I ask which element contains teh value 0.01. Ok. Same with 0.02.  
But when I ask about 0.03 or above... the answer is none. But I can  
see "0.03" in my vector, it is there!

This must be because of the value stored internally. Indeed, if I do:

v-0.03, when I reach the value that appeared to contain 0.03, I don't  
obtain zero, but "3.47e-18"

why is this?
what is the recommended way to overcome this?
Interestingly, if I store the values as "character", then I can match  
them fine...

Below is the code, and my sessionInfo() output.


Jose de las Heras

code example:
while (i <= 0.1)

> sessionInfo()
R version 2.10.0 (2009-10-26)

[1] LC_COLLATE=English_United Kingdom.1252
[2] LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] biomaRt_2.2.0

loaded via a namespace (and not attached):
[1] RCurl_1.3-1  tools_2.10.0 XML_2.6-0

The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.

More information about the R-help mailing list