How to detect null values ββin a vector
As stated in the comments, NULL
will not appear in length(vx)
. This is a special object in R for undefined values. From CRAN documentation:
NULL represents a null object in R: it is a reserved word. NULL - often returned by expressions and functions whose value is undefined.
But your question may still have learning opportunities regarding lists. He will appear there. How in:
vlist <- list(1, 2, 3, NULL, 5)
Trying to identify a value NULL
in a very large dataset can be daunting for novice R users. There are different methods, but here I work for me when I need it.
!unlist(lapply(vlist, is.numeric))
[1] FALSE FALSE FALSE TRUE FALSE
#or as user Kara Woo added. Much better than my convoluted code
sapply(vlist, is.null)
[1] FALSE FALSE FALSE TRUE FALSE
#suggestion by @Roland
vapply(vlist, is.null, TRUE)
[1] FALSE FALSE FALSE TRUE FALSE
If there are symbols, then substitute in is.character
or in relation to any class.
source to share
Perhaps someone (like me) can find someone who is.null()
really needs is.na()
. If so, remind yourself (as I did) that R has both an object NULL
and NA
. NULL
means that there is no value, whereas NA
means that the value is unknown. If you are looking for missing values ββin a value vector, then what you are probably after is is.na()
.
source to share