To check if a list is not empty in R, you can use the length()
function to check if the length of the list is greater than zero.
In this example,
my_list
with some elements.my_list
is greater than zero using the length()
function.my_list <- list(1, 2, 3)
if (length(my_list) > 0) {
print('The list is not empty')
} else {
print('The list is empty')
}
[1] "The list is not empty"
In this example,
empty_list
without any elements.empty_list
is greater than zero using the length()
function.empty_list <- list()
if (length(empty_list) > 0) {
print('The list is not empty')
} else {
print('The list is empty')
}
[1] "The list is empty"
In this tutorial, we learned How to check if a List is Not Empty in R language with well detailed examples.