[R] Error: if statement: missing value where TRUE/FALSE needed

Chuck Coleman chuckco|em@n @end|ng |rom y@hoo@com
Wed Jan 5 16:47:07 CET 2022

This is a rather complex error, for which I created https://sourceforge.net/projects/rhelp/files/Metro/ to hold a minimal workspace with all files needed to make everything work.  I'm afraid I can't make a minimal example because the environment may be involved.  I suspect that it is an error with base R itself.  I've verified it in both MacOS and Ubuntu Linux using Rstudio 2021.09.1 Build 372 and R 4.1.2.  I'm trying to run the command

Metro.imf0 <- extractIMF0(Metro.xts,threshold = 1.7,robust=TRUE)$IMF0

This is the last command in MetroTest.R, which also sets up the workspace.  The output is in MetroTest.log.  The error is:

    Error in if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE else if (xseg[w1] <  : 
      missing value where TRUE/FALSE needed
    Called from: detectspikes(x, mindex = mindex, threshold = threshold, robust = robust)

extractIMF0() calls extractoutliers() which iteratively calls detectspikes() using while().  I've put in tracking code to show the beginning and end of each iteration of while() and the call to detectspikes().

The command producing the error is:

      if (xseg[w1] > ub & m == 1) olrmark[wi] = TRUE
      else if (xseg[w1] < lb & m == 2) olrmark[wi] = TRUE

This code used to work before R 4.1.2.  (I forget the exact version.)  The first time I had this error, I inserted a browser() command right before if/else so I could manually check all of the if/else arguments and they were nonmissing.  Then, executing the if/else caused a crash, I believe the first time it was called.  I then made the browser() statement conditional to stop execution if a missing argument were encountered and detectspikes() happily passed it and crashed.  Then, I individually called print() for each variable.  detectspikes() happily ran several times, then crashed in a later call after running for several seconds.  Again, browser() never stopped the program.  It is this version that I have verified on both a Mac and Ubuntu box.

The first step appears to be to run the code using earlier versions of R.  (I don't want to tamper with my installations.)  Afterwards, I have no idea.  Hence, my post.

Thank you for your help,

More information about the R-help mailing list