Matching/checking for occurence when values are double?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Matching/checking for occurence when values are double?

mavavilj
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)?
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Matching/checking for occurence when values are double?

Rui Barradas
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 <[hidden email]>:

> 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)?
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Matching/checking for occurence when values are double?

Jorge Cimentada
In reply to this post by mavavilj
Matching 100 to 100.0 or 100.00 or whatever N number of decimales will
always return a TRUE.

The expression your using is correct. A more complete expression would be
kidmomiq[100 == kidmomiq$mom_iq, ].



On Fri, Sep 9, 2016 at 2:01 PM, Matti Viljamaa <[hidden email]> wrote:

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)?
> ______________________________________________
> [hidden email] 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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Matching/checking for occurence when values are double?

Ivan Calandra-4
In reply to this post by Rui Barradas
Hi,

Not sure, but it seems that your function equal() is exactly what
all.equal() does, isn't it?

Ivan

--
Ivan Calandra, PhD
Scientific Mediator
University of Reims Champagne-Ardenne
GEGENAA - EA 3795
CREA - 2 esplanade Roland Garros
51100 Reims, France
+33(0)3 26 77 36 89
[hidden email]
--
https://www.researchgate.net/profile/Ivan_Calandra
https://publons.com/author/705639/

Le 09/09/2016 à 14:47, [hidden email] a écrit :

> 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 <[hidden email]>:
>
>> 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)?
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Matching/checking for occurence when values are double?

Rui Barradas
Not exactly, all.equal is much more complete.
It accepts all kinds of objects, not just vectors.


Rui Barradas


Citando Ivan Calandra <[hidden email]>:

> Hi,
>
> Not sure, but it seems that your function equal() is exactly what  
> all.equal() does, isn't it?
>
> Ivan
>
> --
> Ivan Calandra, PhD
> Scientific Mediator
> University of Reims Champagne-Ardenne
> GEGENAA - EA 3795
> CREA - 2 esplanade Roland Garros
> 51100 Reims, France
> +33(0)3 26 77 36 89
> [hidden email]
> --
> https://www.researchgate.net/profile/Ivan_Calandra
> https://publons.com/author/705639/
>
> Le 09/09/2016 à 14:47, [hidden email] a écrit :
>> 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 <[hidden email]>:
>>
>>> 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)?
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Matching/checking for occurence when values are double?

Rui Barradas
Actually, there was another reason for the function equal() but I  
wasn't remembering what.
all.equal doesn't recycle its arguments, just see this example.

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

x <- seq(0, 1, by = 0.2)
x == 0.6
all.equal(x, 0.6)
equal(x, 0.6)

Rui Barradas



Citando [hidden email]:

> Not exactly, all.equal is much more complete.
> It accepts all kinds of objects, not just vectors.
>
>
> Rui Barradas
>
>
> Citando Ivan Calandra <[hidden email]>:
>
>> Hi,
>>
>> Not sure, but it seems that your function equal() is exactly what  
>> all.equal() does, isn't it?
>>
>> Ivan
>>
>> --
>> Ivan Calandra, PhD
>> Scientific Mediator
>> University of Reims Champagne-Ardenne
>> GEGENAA - EA 3795
>> CREA - 2 esplanade Roland Garros
>> 51100 Reims, France
>> +33(0)3 26 77 36 89
>> [hidden email]
>> --
>> https://www.researchgate.net/profile/Ivan_Calandra
>> https://publons.com/author/705639/
>>
>> Le 09/09/2016 à 14:47, [hidden email] a écrit :
>>> 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 <[hidden email]>:
>>>
>>>> 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)?
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.