[R] That dreaded floating point trap

From: Alexander Engelhardt <alex_at_chaotic-neutral.de>
Date: Thu, 31 Mar 2011 13:24:01 +0200

I had a piece of code which looped over a decimal vector like this:

for( i in where ){

   thisdata <- subset(herde, herde$mlr >= i)    # do stuff with thisdata..

'where' is a vector like seq(-1, 1, by=0.1)

My problem was: 'nrow(thisdata)' in loop repetition 0.4 was different if
'where' was seq(-1, 1, by=0.1) than when 'where' was seq(-0.8, 1, by=0.1)
It went away after I changed the first line to:

   thisdata <- subset(herde, herde$mlr >= round(i, digits=1))

This is that "floating point trap" the R inferno pdf talked about, right? That file talked about the problem, but didn't offer a solution.

Similar things happened when I created a table() from a vector with values in seq(-1, 1, by=0.1)

Do I really have to round every float at every occurence from now on, or is there another solution? I only found all.equal() and identical(), but I want to subset for observations with a value /greater/ than something.

Thanks in advance,

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 31 Mar 2011 - 11:24:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 31 Mar 2011 - 13:50:25 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive