R – Check if List is Empty
To check if list is empty in R programming, we have to evaluate the condition that the length of list is zero. Call length()
function with this list passed as argument and if the return value is 0 using equal to operator.
In this tutorial, we will learn how to check if a list is empty in R, using length()
function and equal to operator, with the help of example programs.
Syntax
The syntax of condition to check if the list x
is empty in R
</>
Copy
length(x) == 0
The above expression returns a logical value of TRUE or FALSE.
Example
In the following program, we create an empty list and programmatically check if this list is empty.
example.R
</>
Copy
x <- list()
if (length(x) == 0) {
print("List is empty.")
} else {
print("List is not empty.")
}
Output
[1] "List is empty."
Conclusion
In this R Tutorial, we learned how to check if a list is empty or not in R programming, with the help of examples.