In computer programming, null or NULL is both a value and a pointer. Null is generally an inbuilt constant in the programming languages with a value of zero or close to zero like nothing. If a reference points to null, it simply means that there is no value associated with it. Let’s cut the chase and coming to R Programming.
To convert any R Object to NULL, just assign the NULL value to that R Object or use the as.null() function and pass the R Object to that function and it will return NULL.
NULL in R
The NULL represents the NULL object in R. NULL is used largely to represent the lists with zero length and is usually returned by expressions and functions whose value is undefined.
To define a NULL, just assign it to a variable and that variable becomes NULL.
rv <- NULL rv typeof(rv)
NULL  "NULL"
To create a NULL variable, assign the value NULL to any variable, and that variable has a NULL value.
R provides two functions to deal with NULL.
is.null() function in R
The is.null() is an inbuilt R function that checks if the variable has NULL value or not. It returns the logical vector, either TRUE or FALSE.
rv <- NULL rv is.null(rv)
NULL  TRUE
Let’s define a list of mix 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)
[]  1 []  2 [] NULL []  4 []  5 Checking the NULL value  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)
 1 2 4 3 Checking the NULL value of Vector  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)
NULL Checking the NULL value of Vector  TRUE
Surprise! It returns TRUE that means if all the vector elements are NULL then is.null() returns TRUE.
as.null() function in R
The as.null() function ignores its argument and returns the value NULL.
The x an object to be tested or coerced.
The … will be ignored.
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)
 1 2 3 4 5 Converting a vector to NULL NULL  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 just assigning the value NULL or use the as.null() function to convert it to NULL.
That is it for a NULL value in the R language.