To check if a list is empty in R, you can use the length() function or directly check the list as a boolean expression.
In this example,
my_list
.length()
function to get the length of my_list
.my_list <- list()
if (length(my_list) == 0) {
print('List is empty')
} else {
print('List is not empty')
}
List is empty
In this example,
my_list
.my_list
is empty as a boolean expression.my_list
is empty, the boolean expression evaluates to TRUE
; otherwise, it evaluates to FALSE
.my_list <- list()
if (is.null(my_list)) {
print('List is empty')
} else {
print('List is not empty')
}
List is empty
In this tutorial, we learned How to check if a List is Empty in R language with well detailed examples.