Home » How to Use is.null in R (With Examples)

How to Use is.null in R (With Examples)

by Tutor Aspire

You can use the is.null function in R to test whether a data object is NULL.

This function uses the following basic syntax:

is.null(x)

where:

  • x: An R object to be tested

The following examples show how to use this function in different scenarios.

Example 1: Use is.null to Check if Object is NULL

The following code shows how to use is.null to test whether two different vectors are equal to NULL:

#create non-null vector
x #test if x is NULL
is.null(x)

[1] FALSE

#create null vector
y #test if y is NULL
is.null(y)

[1] TRUE

The is.null function returns FALSE for the first vector and TRUE for the second vector.

Also note that is.null will return TRUE if a vector exists but is empty:

#create empty vector
x #test if x is NULL
is.null(x)

[1] TRUE

Example 2: Use !is.null to Check if Object is Not NULL

The following code shows how to use !is.null to test whether two different vectors are not equal to NULL:

#create non-null vector
x #test if x is not NULL
!is.null(x)

[1] TRUE

#create non-null vector
y #test if y is not NULL
!is.null(y)

[1] FALSE

The !is.null function returns TRUE for the first vector and FALSE for the second vector.

Additional Resources

The following tutorials explain how to perform other common operations with missing values in R:

How to Use is.na in R
How to Use na.rm in R
How to Use na.omit in R

You may also like