NULL in R is a special object that represents the absence of any object. It is often used as a placeholder or to indicate the absence of a value.
NULL is used largely to represent the lists with zero length and is usually returned by expressions and functions whose value is undefined.
Example 1: How to define a NULL in R
To create a NULL, assign it to a variable, and that variable becomes NULL.
rv <- NULL
rv
typeof(rv)
Output
NULL
[1] "NULL"
Example 2: Using NULL as a placeholder
main_function <- function() {
return(NULL)
}
# call the function and assign the result to a variable
null_res <- main_function()
# print the result
print(null_res)
Output
NULL
R provides two functions to deal with NULL.
- is.null()
- as.null()
is.null() function in R
The is.null() is a built-in R function that checks whether the variable has a NULL value. It returns the logical vector, either TRUE or FALSE.
rv <- NULL
rv
is.null(rv)
Output
NULL
[1] TRUE
Let’s define a list of mixed values, including NULL value, and use the is.null() function to see the output.
rl <- list(1, 2, NULL, 4, 5)
rl
cat("Checking the NULL value", "\n")
is.null(rl)
Output
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
NULL
[[4]]
[1] 4
[[5]]
[1] 5
Checking the NULL value
[1] FALSE
You can see from the output that it is.null() function returns FALSE.
Let’s check the same for the vector in R to see if we get the same output.
rv <- c(1, NULL, 2, 4, 3)
rv
cat("Checking the NULL value of Vector", "\n")
is.null(rv)
Output
[1] 1 2 4 3
Checking the NULL value of Vector
[1] FALSE
And we get the FALSE in the output, but let’s fill all the values with NULL and use the is.null() function again!
rv <- c(NULL, NULL, NULL, NULL, NULL)
rv
cat("Checking the NULL value of Vector", "\n")
is.null(rv)
Output
NULL
Checking the NULL value of Vector
[1] TRUE
Surprise! It returns TRUE, meaning if all the vector elements are NULL, it is.null() returns TRUE.
You can check if a variable is NULL by using the is.null() function, which returns TRUE if its argument is NULL and FALSE otherwise.
as.null() function in R
You can convert a variable to NULL by using the as.null() function, which ignores its argument and returns the value NULL. The as.null() function ignores its argument and returns the value NULL.
Syntax
as.null(x, ...)
Parameters
The x is an object to be tested or coerced.
The … will be ignored.
Example
rv <- c(1, 2, 3, 4, 5)
rv
cat("Converting a vector to NULL", "\n")
rv_null <- as.null(rv)
rv_null
is.null(rv_null)
Output
[1] 1 2 3 4 5
Converting a vector to NULL
NULL
[1] TRUE
As we can see that the as.null() function converts R Vector to NULL. This way, you can create any data type to NULL by assigning the value NULL or using the as.null() function to convert it to NULL.
See also

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.