[R] ignoring zeros or converting to NA

Henrik Bengtsson hb at stat.berkeley.edu
Wed Aug 13 20:21:00 CEST 2008


FYI,

there is an isZero() in the R.utils package that allows you to specify
the precision.  It looks like this:

isZero <- function (x, neps=1, eps=.Machine$double.eps, ...) {
  (abs(x) < neps*eps);
}

/Henrik

On Wed, Aug 13, 2008 at 8:23 AM, Roland Rau <roland.rproject at gmail.com> wrote:
> 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.
>



More information about the R-help mailing list