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
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.
You can use the is. null function in R to test whether a data object is NULL.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With