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

What is is.factor() Function in R

R consists of various data types, and "factor" is one of them. You can use…

19 hours ago

Efficiently Check If a Vector is Empty in R

The most efficient and idiomatic way to check if a vector is empty in R…

2 days ago

Checking If a Data Frame is Empty in R

What criteria are being evaluated to determine if a data frame is empty? There is…

3 days ago

How to Check If a List is Empty in R

What do we mean when we say an empty list? An empty list does not…

4 days ago

is.element() Function: Check Presence of Elements in R

Whether you want to do membership testing, filter data, identify missing values, check for duplicates,…

5 days ago

as.double() and is.double() Functions in R Language

Whether you want to perform calculations efficiently or derive accurate analysis, you need a double-precision…

1 week ago