Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a list is empty in R?

Tags:

r

Essentially, I have a function that creates outputs of list(). I want my function to not create list(), but instead replace it with another value such as NULL or string. Is there a way to determine if a variable is assigned the empty list, list()?

like image 858
mtber75 Avatar asked Jun 19 '13 05:06

mtber75


People also ask

IS NULL For list in R?

Description. NULL represents the null object in R. NULL is used mainly to represent the lists with zero length, and is often returned by expressions and functions whose value is undefined.

Is empty string in R?

A string in R can be created using single quotes or double quotes. We can create an empty string with empty_str = "" or an empty character vector with empty_chr = character(0) . Both have class “character” but the empty string has length equal to 1 while the empty character vector has length equal to zero.

How do I check if a vector is empty in R?

If you check length(vec) , we know the length of our vector is also 0, meaning that our vector, vec , is empty.

Is Empty function for list?

The list::empty() is a built-in function in C++ STL is used to check whether a particular list container is empty or not. This function does not modifies the list, it simply checks whether a list is empty or not, i.e. the size of list is zero or not.


2 Answers

Empty list is empty. It has no elements, but is still a list. List with no elements has length 0.

a = list() if(length(a) == 0) {     .... } 
like image 88
Uros K Avatar answered Oct 11 '22 20:10

Uros K


!is.null(listname) & class(listname) != "NULL" & class(listname) != "logical" & length(listname) != 0 
like image 40
meiriweixin Avatar answered Oct 11 '22 22:10

meiriweixin