The is.null Function in R (4 Examples)

 

Basic R Syntax:

is.null(x)

 

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. The code above illustrates how to use is.null in R.

In the following article, I’ll provide you with 4 examples for the application of the is.null function in R. Let’s dive in…

 

Example 1: Check if Object is NULL in R

Consider the following example vector in R:

x1 <- c(3, 7, 1, 5, 2, 8)        # Create vector in R

By applying the is.null function we can check whether this data object is NULL. As we know, it is not, and therefore the is.null function returns FALSE:

is.null(x1)                      # Check if vector is NULL
# FALSE

 

Now, consider the following NULL object in R:

x2 <- NULL                       # Create NULL object in R

In this case, the is.null function returns TRUE, indicating that the object x2 is NULL:

is.null(x2)                      # Check if object is NULL
# TRUE

 

Example 2: Check if Object is not NULL

The is.null function can be used the other way around in order to check whether a data object is not NULL. You simply have to put an explanation mark in front of is.null (i.e. !is.null):

!is.null(x1)                      # Check if vector is not NULL
# TRUE
 
!is.null(x2)                      # Check if object is not NULL
# FALSE

 

Example 3: Check if Data Frame is NULL

The same principle can be applied to a data frame. Let’s load some example data to RStudio:

data("mtcars")                   # Load mtcars data set
head(mtcars)                     # First 6 rows of mtcars data

 

Data Frame Example for Applying the is.null R Function

Table 1: First 6 Rows of the Example Data Set mtcars.

 

Now, let’s check whether the mtcars data is a NULL object (obviously it’s not):

is.null(mtcars)                  # Check if data frame is NULL
# FALSE

 

However, if we convert this data matrix to a NULL object, the is.null function returns TRUE:

mtcars2 <- mtcars                # Replicate mtcars data frame
mtcars2 <- NULL                  # Convert mtcars2 data to NULL object
is.null(mtcars2)
# TRUE

 

Example 4: Check if List is NULL

As you have seen, the is.null function can be applied to a variety of data classes and formats. This also includes list objects in R. Consider the following example list:

mylist <- list()                 # Empty list object
mylist[[1]] <- x1                # Assign x1 example vector to first list entry
mylist[[2]] <- mtcars[1:3, 3:5]  # Assign mtcars subset to second list entry

As before, we can apply is.null to the whole list:

is.null(mylist)                  # Check if list is NULL
# FALSE

 

Again, after transforming the list to NULL, the is.null function returns TRUE:

mylist <- NULL                   # Convert mylist to NULL
is.null(mylist)                  # Check if list is NULL
# TRUE

 
 

On a side note:
R provides several other is.xxx functions that are very similar to is.null (e.g. is.na, is.nan, or is.finite). All I’ve show you here is applicable to many other R programming scenarios!

 
 

Video Explanation: Logical Values in R

The is.null function returns a logical vector. Do you need more practice with logical vectors in R? Then I can recommend the following tutorial video of the DataCamp YouTube channel:

 

 

Further Reading

 



 

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu