# The is.null Function in R (4 Examples)

**Basic R Syntax:**

is.null(x) |

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 |

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 |

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

Now, consider the following NULL object in R:

x2 <- NULL # Create 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 |

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 |

!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("mtcars") # Load mtcars data set head(mtcars) # First 6 rows of mtcars data

**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 |

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 |

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 |

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 |

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 |

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

### Subscribe to my free statistics newsletter:

### R Tutorials

abs Function in R

all & any R Functions

Set Aspect Ratio of Plot

attach & detach R Functions

attr, attributes & structure in R

cbind R Command

Change ggplot2 Legend Title

Character to Numeric in R

Check if Object is Defined

col & row sums, means & medians

Complete Cases in R

Concatenate Vector of Strings

Convert Date to Weekday

cumsum R Function

Data Frame Column to Numeric

diff Command in R

difftime R Function

dim Function in R

dir R Function

Disable Scientific Notation

Draw Segments in R

droplevels R Example

Evaluate an Expression

Extract Characters from String

Factor to Numeric in R

Format Decimal Places

get, get0 & mget in R

is.na R Function

is.null Function in R

jitter R Function

Join Data with dplyr Package

length Function in R

lowess R Smoothing Function

max and min Functions in R

NA Omit in R

nchar R Function

ncol Function in R

nrow Function in R

outer Function in R

pairs & ggpairs Plot

parse, deparse & R expression

paste & paste0 Functions in R

pmax and pmin R Functions

polygon Plots in R

pretty R Function

R Find Missing Values

R Functions List (+ Examples)

R NA – Values

R Replace NA with 0

rbind & rbind.fill in R

Read Excel Files in R

readLines, n.readLines & readline

Remove Element from List

Remove Legend in ggplot2

Rename Column Name in R

Replace Last Comma of String

rev R Command

Round Numeric Data in R

Save & Load RData Workspace

scan R Function

setdiff R Function

setNames vs. setnames in R

sink Command in R

Sort, Order & Rank Data in R

sprintf Function in R

Square Root in R

str_c Function of stringr Package

str_sub Function of stringr Package

strptime & strftime Functions

substr & substring R Commands

sweep R Function

Transform Data Frames

union Function in R

unlist in R

weekdays, months, quarters & julian in R

with & within R Functions

Write Excel File in R