Search code examples
r

alternative to "!is.null()" in R


my R code ends up containing plethora of statements of the form:

if (!is.null(aVariable)) { 
     do whatever 
}

But this kind of statement is hard to read because it contains two negations. I would prefer something like:

 if (is.defined(aVariable)) { 
      do whatever 
 }

Does a is.defined type function that does the opposite of !is.null exist standard in R?

cheers, yannick


Solution

  • You may be better off working out what value type your function or code accepts, and asking for that:

    if (is.integer(aVariable))
    {
      do whatever
    }
    

    This may be an improvement over isnull, because it provides type checking. On the other hand, it may reduce the genericity of your code.

    Alternatively, just make the function you want:

    is.defined = function(x)!is.null(x)