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

colSums(): Calculating the Sum of Columns of a Data Frame in R

The colSums() function in R calculates the sums of columns for numeric matrices, data frames,…

4 days ago

rowSums(): Calculating the Sum of Rows of a Matrix or Data Frame in R

The rowSums() function calculates the sum of values in each numeric row of a matrix,…

7 days ago

R View() Function

The View() is a utility function in R that invokes a more intuitive spreadsheet-style data…

2 weeks ago

summary() Function: Producing Summary Statistics in R

The summary() is a generic function that produces the summary statistics for various R objects,…

3 weeks ago

R paste() Function

The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…

4 weeks ago

paste0() Function in R

R paste0() function concatenates strings without any separator between them. It is a shorthand version…

4 weeks ago