[R] Error: if statement: missing value where TRUE/FALSE needed
murdoch@dunc@n @end|ng |rom gm@||@com
Wed Jan 5 19:57:15 CET 2022
On 05/01/2022 10:47 a.m., Chuck Coleman via R-help wrote:
> 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.
The error message is misleading: you don't get this message from
missing values, it comes from using NA in the condition of the if().
Missing values would lead to an error message like 'argument "x" is
missing, with no default'.
Then, executing the if/else caused a crash,
Usually the word crash is a serious problem where R shuts down. From
your description, I think you just have an error.
Errors are much easier to diagnose. As Ivan said, you can use
options(recover) to stop when the error is raised, find the right
evaluation frame, and print all the values involved in the condition.
You can also just use cat() to print everything just before the bad
cat("xseg[w1]=", xseg[w1], " ub=", ub, " m=", m, " lb=", lb, "\n")
and then see if the last message printed contains any NA values.
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,
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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