R Basic

What is is.null() Function in R

Overview

Whether working on a small program or project, if a function receives a “NULL” value when it is not expected, it can lead to an error or unexpected results.

To avoid this type of unexpected error, we have to put a mechanism in place that checks whether the input is “NULL” or not. This is where this function comes into play.

is.null()

The is.null() is a built-in function that checks whether an object is NULL. It returns TRUE if the object is NULL and FALSE if not.

Syntax

is.null(object)

Parameters

Name Value
object It is an R object that can be a vector, string, boolean, or any object

Checking against an explicitly NULL and not NULL objects

Let’s create a NULL vector, pass it to this function, and observe the output.

obj <- NULL

is.null(obj)

# TRUE

As expected, it returns TRUE.

Instead of assigning NULL, let’s assign a positive value and check against a non-null value.

obj <- 1921

is.null(obj)

# FALSE

Checking against NULL and non-NULL vectors

Let’s create a vector of five NULL values.

obj_vec <- c(NULL, NULL, NULL, NULL, NULL)

is.null(obj_vec)

# TRUE

Since all the five values of a vector are NULL, it returns TRUE.

Changing the single value of a vector to the proper value will return FALSE because now the vector is non-null.

The is.null() function checks if an object itself is NULL and not an individual element. For example, if you are working on a vector, the vector itself should be NULLand all its individual components.

obj_vec <- c(NULL, NULL, 19, NULL, NULL)

is.null(obj_vec)

# FALSE

A vector’s individual elements must be strictly NULL to be counted as a NULL vector.

Checking against an empty vector

Initialize an empty vector and see the outcome.

obj_vec_empty <- c()

is.null(obj_vec_empty)

# TRUE

If you check for an empty vector, this function returns TRUE.

Empty and character strings

This function returns FALSE against empty and character strings.

empty_str <- ""
char_str <- "KRUNAL"

is.null(empty_str) # FALSE
is.null(char_str) # FALSE

Logical values

The boolean (logical) values TRUE and FALSE are not NULL values and hence it returns FALSE.

logi_true <- TRUE
logi_false <- FALSE

is.null(logi_true) # FALSE
is.null(logi_false) # FALSE

Missing Values (NA)

In R, NA represents (Not Available), meaning it is a missing value. One thing to remember is that the NA value is not the same as the NULL value.

The is.null() function does not check for missing values. To check for missing values, use the is.na() function. Hence, it returns FALSE when you check for missing values.

missing_value <- NA

is.null(missing_value) # FALSE

Data Frames and Lists

Even if you check for an empty data frame and list objects, it is not NULL. That’s why it returns FALSE.

main_df <- data.frame()
main_list <- list()

is.null(main_df) # FALSE
is.null(main_list) #FALSE

The empty or non-empty data frame and list objects, it returns FALSE.

Using !is.null()

Until now, we checked for a NULL object. If you put a (!) negation sign at the start of is.null() will check whether the value is not NULL instead of NULL.

In short, If is.null() returns TRUE, the !is.null() returns FALSE for the same value.

r_obj <- NULL

!is.null(r_obj) # FALSE

The negation operator is often used in conditional statements such as “if” to check whether the value is not NULL before proceeding further.

if (!is.null(obj)) {
  # Performing some operation on obj
  print(obj)
}

That’s all, mates!

Recent Posts

How to Check If File and Folder Already Exists in R

Whether you are reading or writing files via programs in the file system, it is…

2 days ago

How to Check Data type of a Variable in R

When it comes to checking the data type of a variable, it depends on what…

3 days ago

Mastering grepl() Function in R

The grepl() function (stands for "grep logical") in R searches for patterns within each element…

4 days ago

zip(), unzip() and tar(), untar() Functions in R

The zip() function creates a new zip archive file. You must ensure that the zip tool…

5 days ago

How to Create Directory and File If It doesn’t Exist in R

When working with file systems, checking the directory or file existence is always better before…

6 days ago

How to Create a Grouped Boxplot in R

To create a grouped boxplot in R, we can use the ggplot2 library's aes() and…

1 week ago