is.null Function in R (Example)
This tutorial illustrates how to check whether an object is NULL with the is.null() function in the R programming language.
Example 1: Applying is.null Function
Example data:
x1 <- "some text" # Create a data object |
x1 <- "some text" # Create a data object
Check if example data is a NULL object:
is.null(x1) # Apply is.null function # FALSE |
is.null(x1) # Apply is.null function # FALSE
The RStudio console returns the logical value FALSE, i.e. our data object is not a NULL object.
Create exemplifying NULL object:
x2 <- NULL # Create a NULL object |
x2 <- NULL # Create a NULL object
Application of is.null function to real NULL object:
is.null(x2) # Apply is.null function # TRUE |
is.null(x2) # Apply is.null function # TRUE
The is.null function returns TRUE, since our second example data x2 is actually a NULL object.
Example 2: Check if Object is NOT NULL
Similar to other is… functions such as is.na or is.nan, we can also check if a data object is NOT NULL.
We simply have to add a bang (i.e. !) in front of is.null:
!is.null(x1) # Apply !is.null function # TRUE |
!is.null(x1) # Apply !is.null function # TRUE
x1 is not NULL and therefore !is.null returns TRUE.
!is.null(x2) # Apply !is.null function # FALSE |
!is.null(x2) # Apply !is.null function # FALSE
x2 is NULL and therefore !is.null returns FALSE.