Search code examples
rp-valueproportions

How to properly compare numbers in scientific notation using R?


I was reading the following tutorial for testing proportions in two populations. After running

prop.test(x=c(342,290), n=c(400,400))

I received a p-value of 9.558674e-06, which the tutorial says is greater than the alpha level of .05. I assumed this was a typo, and was just comparing the p-value to its value in decimal notation, 0.000009558674, but received "False". I even turned off scientific notation using

options(scipen=999)

and when printing out the p-value from the object returned by prop.test, I still receive "False" when comparing the p-value to 0.000009558674 for equality, it recognizes the p-value as lesser than. Why is this the case?


Solution

  • You may want to consider using the all.equal() function. The tolerance between the values can be set with the tolerance argument.

    isTRUE(all.equal(2, 2.00000001))
    ##  [1] TRUE
    isTRUE(all.equal(2, 2.00000001, tolerance = 0.0000000001))
    ##  [1] FALSE