-1

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#
  }
Gotmadstacks
  • 359
  • 6
  • 20
  • The error message means that your variable exists but contains a missing value, i.e., an `NA` value. Maybe you should use `if (!is.na(count_1))` or `if (is.finite(count_1))`. However, if you have many loops you are writing inefficient R code. – Roland Aug 22 '16 at 11:35
  • Maybe this requires a double check - `if (exists("count_1") & !is.na(count_1))` – Ronak Shah Aug 22 '16 at 11:36
  • @Roland `count_1 <- NA; if (exists("count_1")){ "yes" } else { "no" }` works... not sure how to reproduce above error. – zx8754 Aug 22 '16 at 11:38
  • 1
    @RonakShah Hmm I think that would fail if there'd be not `count_1`. Either `if (exists("count_1") && !is.na(count_1))` or two nested `if`s. – lukeA Aug 22 '16 at 11:38
  • 1
    Actually the above code you had mentioned should work unless its `na`. If in loop just use `else { 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') } ` – Shiva Prakash Aug 22 '16 at 11:40
  • Related post: http://stackoverflow.com/questions/9368900 – zx8754 Aug 22 '16 at 11:41
  • 1
    @zx8754 I believe their `#code#` contains some subsetting with a logical comparison or something similar. – Roland Aug 22 '16 at 11:41
  • @Roland so error is coming from `#code#` bit we know nothing about... – zx8754 Aug 22 '16 at 11:42
  • 1
    @zx8754 Yes, and they are trying to avoid it with the `if` condition. – Roland Aug 22 '16 at 11:43
  • @zx8754 There is also the possibility of an empty value, like `x <- integer(length=0)`. In that case `exists("x")` returns `TRUE` and `isTRUE(is.na(x))` yields `FALSE`, but there is still no value stored in `x`... – RHertel Aug 22 '16 at 11:49
  • 1
    If I understand the discussion correctly: `if(length(count_1)==1)` should check for something with a value. – nya Aug 22 '16 at 11:49
  • 1
    @nya I believe that this would be a good answer. Or maybe a more general comparison, like `if(length(count_1) > 0)` or so... – RHertel Aug 22 '16 at 11:51
  • 2
    Never use `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. – Konrad Rudolph Aug 22 '16 at 11:56

2 Answers2

2

Use if(length(count_1) == 1) { next } to check if there is a value in count_1.

However, this will only work if your code does something like this:

dat <- 1:5
count_1 <- which(dat > 10)
count_1
# integer(0)
length(count_1) == 1
# [1] FALSE

It will not work with another way of filling the variable, e.g.:

count_1 <- ifelse(any(dat > 10), which(dat > 10), NA)
count_1
# [1] NA
length(count_1) == 1
# [1] TRUE
nya
  • 2,138
  • 15
  • 29
  • 2
    Suggestion: Maybe it would be good to combine the two possibilities, empty and `NA` values, like this: `if (length(count_1) && !is.na(count_1))...` – RHertel Aug 22 '16 at 12:07
  • @RHertel Absolutely. If we knew who the OP creates the variable, we could provide a bullet-proof solution. – nya Aug 22 '16 at 12:08
2

For a different reason, I need to check if a variable exists inside a function. I use this:

check=function(x) tryCatch(if(class(x) == 'logical') 1 else 1, error=function(e) 0) 
varX=1 
check(varX)
[1] 1
rm(varX)
check(varX)
[1] 0
f1= function(x) if(check(x)) cat('exists') else cat('not exists')
f1(varX)
not exists
xm1
  • 1,663
  • 1
  • 17
  • 28