[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.
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
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
More information about the R-help
mailing list