1

As a follow-up of my previous question - NaN is removed when using na.rm=TRUE, I am looking for a workaround with minimal changes to my (large) code base.

I tried this, which works, but

my.is.na <- function(x)
{
   ifelse(is.na(x) & !is.nan(x), TRUE, FALSE)
}

But I want to override the built-in is.na function. Renaming my function to is.na obviously results in infinite recursion. What are my options?

PS: In all of the code it is assumed that is.na(NaN) returns FALSE, so I would prefer to override.

Community
  • 1
  • 1
Nishanth
  • 6,932
  • 5
  • 26
  • 38
  • 2
    If you are trying to replace how `na.rm` works within primitive functions like `max` and `min`, It might be better to rewrite these functions) – mnel Apr 16 '13 at 06:51
  • I just read your older question and there I wondered, if you want to keep `NaN` in the `max()` call, what is the maximum then? I would agree with @mnel and rewrite the `max`function instead of the here planned workaround. – Daniel Fischer Apr 16 '13 at 06:56
  • 1
    @mnel - that's probably the more sensible option as I see lots of potential unintended consequences from following answers like mine. – thelatemail Apr 16 '13 at 06:56
  • 1
    @thelatemail, primitive functions won't use is.na anyway, so along with the potential unintended consequences, I don't think any solution modifying is.na will have the intended consequences. – mnel Apr 16 '13 at 07:04
  • @mnel - it appears you are right. I checked my answer for sensitivity, not specificity. I guess that'll learn me. – thelatemail Apr 16 '13 at 07:09
  • @mnel Yes, overriding `min` & `max` is one solution - I have to make sure that this behaviour is required by only these 2 functions in my code. – Nishanth Apr 16 '13 at 07:46

1 Answers1

0

I am not sure if this is a legal way, but what about:

old.na <- is.na
is.na <- function(x)
{
   ifelse(old.na(x) & !is.nan(x), TRUE, FALSE)
}

This might violate some rules in R and might have some other bad consequences, but at least it behaves the way you had in mind...

Daniel Fischer
  • 3,280
  • 1
  • 18
  • 29
  • truly hacky, in a horrifyingly brilliant way. i think that it should work, though, if `is.na` is not called anywhere else in any other context, because it's a generic function and will probably start behaving badly in those cases. but the thing with these things is that you might have to test in production, and by then, it's too late... or is it? :[ – daikonradish Apr 16 '13 at 07:48
  • This will only affect your code, not any functions that call `is.na` from other packages. – hadley Apr 16 '13 at 12:29