Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 255
Yannick Wurm Avatar asked Feb 01 '10 09:02

Yannick Wurm


People also ask

Is null () in R?

The R function is. null indicates whether a data object is of the data type NULL (i.e. a missing value). The function returns TRUE in case of a NULL object and FALSE in case that the data object is not NULL.

How do you check if it is null in R?

You can use the is. null function in R to test whether a data object is NULL.

Is null the same as Na in R?

Your answer In simple words, NULL represents the null or an empty object in R. NA represents a missing value in R. NA can be updated in R by vectors, list and other R objects whereas NULL cannot be coerced. NA elements can be accessed and be managed using various na functions such as is.na(), na.

IS null a null R?

NULL represents the null object in R. NULL is used mainly to represent the lists with zero length, and is often returned by expressions and functions whose value is undefined.


1 Answers

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) 
like image 194
Alex Brown Avatar answered Sep 19 '22 17:09

Alex Brown