0

I'm running through someone else's R code while they are on vacation. To my knowledge, this code has always worked before, and I've seen them run it successfully. When I run it, I have an unexpected error with this line:

AllZero <- if ( length(unique(x==0)) > 1 ) {FALSE} else if (unique(x==0) == TRUE) {TRUE} else {FALSE}

The error returned is:

Error in if (unique(x == 0) == TRUE) { : missing value where TRUE/FALSE needed

My cursory search indicates this syntax is okay. This is part of a function which is in turn part of a very, very large set of procedures. I'm not sure if this function is now being called in an unexpected way (or perhaps has not been called previously) because the underlying data changed.

Am I missing something very obvious?

Quicksilver
  • 295
  • 4
  • 16
  • 3
    How is `x` defined? I ask because the syntax isn't necessarily wrong, this appears to be a runtime error. – MrFlick May 22 '15 at 18:42
  • It seems like what they really want is something like `AllZero<-if(all(x==0)) TRUE else FALSE` – John Paul May 22 '15 at 18:47
  • 1
    @JohnPaul: Or just `AllZero<-all(x==0)`. ;) – Alex A. May 22 '15 at 18:58
  • 1
    The error you see commonly arrises when the condition does not evaluate to purely `TRUE` or `FALSE`. There are two most likely culprits. Either you have an `NA` value, or there is no value (ie, length of `0`) – Ricardo Saporta May 22 '15 at 19:11

0 Answers0