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.
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.
is.null(object)
Name | Value |
object | It is an R object that can be a vector, string, boolean, or any object |
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
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 NULL, and 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.
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.
This function returns FALSE against empty and character strings.
empty_str <- ""
char_str <- "KRUNAL"
is.null(empty_str) # FALSE
is.null(char_str) # FALSE
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
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
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.
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!
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
R consists of various data types, and "factor" is one of them. You can use…
The most efficient and idiomatic way to check if a vector is empty in R…
What criteria are being evaluated to determine if a data frame is empty? There is…
What do we mean when we say an empty list? An empty list does not…
Whether you want to do membership testing, filter data, identify missing values, check for duplicates,…
Whether you want to perform calculations efficiently or derive accurate analysis, you need a double-precision…