[R] Matching/checking for occurence when values are double?

ruipbarradas at sapo.pt ruipbarradas at sapo.pt
Fri Sep 9 14:47:39 CEST 2016


Hello,

See FAQ 7.31.
It's irrelevant if you write 100 or 100.0, the values are the same.  
The difference would be between 100 (double) and 100L (integer).
To check for equality between floating-point numbers you can use, for  
instance, the following function.

equal <- function(x, y, eps = .Machine$double.eps^0.5) abs(x - y) < eps

equal(100, 100 + 2e-15)
[1] TRUE

Hope this helps,

Rui Barradas



Citando Matti Viljamaa <mviljamaa at kapsi.fi>:

> I need to pick from a dataset those rows that have a double value set to 100.
> However since the values in this column are like the following:
>
> [1] 121.11750  89.36188 115.44320  99.44964  92.74571 107.90180
> [7] 138.89310 125.14510  81.61953  95.07307  88.57700  94.85971
> [13]  88.96280 114.11430 100.53410 120.41910 114.42690
>>
> Then can I match against 100 or 100.0? Or do I need to match against  
> 100.00000 or something else?
>
> E.g. does
>
> 100.0 %in% kidmomiq$mom_iq
>
> produce a truthful match result with this kind of data (I’m getting  
> 0 occurrences, which might be correct, but I’m not sure)?
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.



More information about the R-help mailing list