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

J.delasHeras at ed.ac.uk J.delasHeras at ed.ac.uk
Wed Feb 3 16:37:39 CET 2010

```Concise and perfect answer. Thanks!
I'm surprised I never came across this issue before (4 years playing with R).

Jose

Quoting Erik Iverson <eriki at ccbr.umn.edu>:

> FAQ 7.31
>
> J.delasHeras at ed.ac.uk wrote:
>>
>> 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
>>  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.
>>
>> Thanks!
>>
>> Jose de las Heras
>>
>>
>> code example:
>> #v<-vector(mode="character")
>> v<-c()
>> i<-0
>> while (i <= 0.1)
>>  {
>>  v<-c(v,i)
>>  i<-i+0.005
>>  }
>> v
>> which(v==0.01)
>> which(v==0.02)
>> which(v==0.03)
>>
>>
>>
>>> sessionInfo()
>> R version 2.10.0 (2009-10-26)
>> i386-pc-mingw32
>>
>> locale:
>> [1] LC_COLLATE=English_United Kingdom.1252
>> [2] LC_CTYPE=English_United Kingdom.1252
>> [3] LC_MONETARY=English_United Kingdom.1252
>> [4] LC_NUMERIC=C
>> [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
>>
>>
>>

--
Dr. Jose I. de las Heras                      Email: J.delasHeras at ed.ac.uk
The Wellcome Trust Centre for Cell Biology    Phone: +44 (0)131 6513374
Institute for Cell & Molecular Biology        Fax:   +44 (0)131 6507360
University of Edinburgh
Edinburgh EH9 3JR
UK
*********************************************
NEW EMAIL from July'09: nach.mcnach at gmail.com
*********************************************

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

```