The is.null Function in R (4 Examples)


Basic R Syntax:



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


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


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
!is.null(x2)                      # Check if object is not NULL

Do you need more explanations on Examples 1 and 2 of this page? Then check out the following video of my YouTube channel:



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


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


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


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


On a side note:
R provides several other functions that are very similar to is.null (e.g., 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


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

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.