[R] ignoring zeros or converting to NA

Thomas Lumley tlumley at u.washington.edu
Thu Aug 14 16:59:39 CEST 2008


On Wed, 13 Aug 2008, Moshe Olshansky wrote:

> Since 0 can be represented exactly as a floating point number, there is no problem with something like x[x==0].
> What you can not rely on is something like 0.1+0.2 == 0.3 to be TRUE.

As I tried to indicate in my previous email, it is more complicated than that. Certainly 0 is exactly representable, the questions are whether x is exactly zero and whether you want to test for exact zero

If x has been read in directly or computed by eg addition/subtraction/multiplication of integers then x will be exactly zero when it is approximately zero so there is no problem.

   If you want to find the subset of x where 0/x will be NaN then you want to test for exact zero and there is no problem even if x is not exactly zero

If x has been computed some other way and you want to find the values of x that would be zero if the computations were done at infinite precision, then x==0 may not work.

         -thomas



>
> --- On Thu, 14/8/08, Roland Rau <roland.rproject at gmail.com> wrote:
>
>> From: Roland Rau <roland.rproject at gmail.com>
>> Subject: Re: [R] ignoring zeros or converting to NA
>> To: "rcoder" <mpdotbook at gmail.com>
>> Cc: r-help at r-project.org
>> Received: Thursday, 14 August, 2008, 1:23 AM
>> Hi,
>>
>> since many suggestions are following the form of
>> x[x==0] (or similar)
>> I would like to ask if this is really recommended?
>> What I have learned (the hard way) is that one should not
>> test for
>> equality of floating point numbers (which is the default
>> for R's numeric
>> values, right?) since the binary representation of these
>> (decimal)
>> floating point numbers is not necessarily exact (with the
>> classic
>> example of decimal 0.1).
>> Is it okay in this case for the value zero where all binary
>> elements are
>> zero? Or does R somehow recognize that it is an integer?
>>
>> Just some questions out of curiosity.
>>
>> Thank you,
>> Roland
>>
>>
>> rcoder wrote:
>>> Hi everyone,
>>>
>>> I have a matrix that has a combination of zeros and
>> NAs. When I perform
>>> certain calculations on the matrix, the zeros generate
>> "Inf" values. Is
>>> there a way to either convert the zeros in the matrix
>> to NAs, or only
>>> perform the calculations if not zero (i.e. like using
>> something similar to
>>> an !all(is.na() construct)?
>>>
>>> Thanks,
>>>
>>> rcoder
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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.
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list