I thought I had figured this one out. I have a variable called:
count_1
It's just a vector with one element. As I go through my loops, sometimes it has a value, and at other times it doesn't. All I want to do is ask if it contains anything, and if not, merely loop back around. Right now my script fails because when count_1 is empty, I get this:
missing value where TRUE/FALSE needed Execution halted
Here is my attempt so far:
if (exists("count_1"))
{
#code#
}
count_1
. Eitherif (exists("count_1") && !is.na(count_1))
or two nestedif
s.na
. If in loop just useelse { next }
that should work I guess. In fact I tried something similar and it worked-a <- 5 rm(b) if(a%%2 == 0 ) { b <- 1 } if(exists("b")) { print('hi') }else { print('bye') }
#code#
contains some subsetting with a logical comparison or something similar.if
condition.exists
for general code. This is a function to support infrastructure programming, it should simply never appear in normal analysis (or general user-level) code: if you write the code, you should know whether a variable exists. Asking the question inside the code is a sure sign that there’s a logic error in the code.