[R] comparison operator, decimals, and signif()

Duncan Murdoch murdoch at stats.uwo.ca
Sun May 22 06:40:22 CEST 2005


Nick Drew wrote:
> Hi, I recently spent quite a bit of time trouble
> shooting a function that I had written only to
> discover that the problem I was having was with the
> comparison operator. I assumed that the following
> would return TRUE:
> 

This is a very common error.  In R 2.1.0, it's FAQ

7.31 Why doesn't R think these numbers are equal?

Duncan Murdoch
> 
>>testMean <- 82.8 + 0.1
>>testMean
> 
> [1] 82.9
> 
>>testMean == 82.9
> 
> [1] FALSE
> 
> 
> Apparently this has to do with deciml places. Look:
> 
> 
>>newTest <- 82.0
>>newTest
> 
> [1] 82
> 
>>newTest == 82
> 
> [1] TRUE
> 
>>newTest == 82.0
> 
> [1] TRUE
> 
> 
> What does signif() do to my object called "testMean"
> so that the comparison now evaluates to TRUE?
> 
> 
>>signif(testMean, 3) == 82.9
> 
> [1] TRUE
> 
> 
> Version info:
> 
> 
>>R.Version()
> 
> $platform
> [1] "i386-pc-mingw32"
> 
> $arch
> [1] "i386"
> 
> $os
> [1] "mingw32"
> 
> $system
> [1] "i386, mingw32"
> 
> $status
> [1] ""
> 
> $major
> [1] "2"
> 
> $minor
> [1] "1.0"
> 
> $year
> [1] "2005"
> 
> $month
> [1] "04"
> 
> $day
> [1] "18"
> 
> $language
> [1] "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