Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract the non-empty elements of list in R?

Tags:

r

I have very big list, but some of the elements(positions) are NULL, means nothing inside there. I want just extract the part of my list, which is non-empty. Here is my effort, but I faced with error:

ind<-sapply(mylist, function() which(x)!=NULL)
list<-mylist[ind]

#Error in which(x) : argument to 'which' is not logical

Would someone help me to implement it ?

like image 817
user2806363 Avatar asked Jun 12 '14 12:06

user2806363


People also ask

How do I remove null elements from a list in R?

If a list contains NULL then we might want to replace it with another value or remove it from the list if we do not have any replacement for it. To remove the NULL value from a list, we can use the negation of sapply with is. NULL.

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.

How do you remove null elements from a list in Python?

The easiest way to remove none from list in Python is by using the list filter() method. The list filter() method takes two parameters as function and iterator. To remove none values from the list we provide none as the function to filter() method and the list which contains none values.


6 Answers

You can use the logical negation of is.null here. That can be applied over the list with vapply, and we can return the non-null elements with [

(mylist <- list(1:5, NULL, letters[1:5]))
# [[1]]
# [1] 1 2 3 4 5

# [[2]]
# NULL

# [[3]]
# [1] "a" "b" "c" "d" "e"

mylist[vapply(mylist, Negate(is.null), NA)]
# [[1]]
# [1] 1 2 3 4 5

# [[2]]
# [1] "a" "b" "c" "d" "e"
like image 193
Rich Scriven Avatar answered Oct 06 '22 07:10

Rich Scriven


Try:

 myList <- list(NULL, c(5,4,3), NULL, 25)
 Filter(Negate(is.null), myList)
like image 41
akrun Avatar answered Oct 06 '22 05:10

akrun


If you don't care of the result structure , you can just unlist:

unlist(mylist)
like image 26
agstudy Avatar answered Oct 06 '22 05:10

agstudy


What the error means is that your brackets are not correct, the condition you want to test must be in the which function :

which(x != NULL)

like image 32
Math Avatar answered Oct 06 '22 07:10

Math


One can extract the indices of null enteries in the list using "which" function and not include them in the new list by using "-".

new_list=list[-which(is.null(list[]))] 

should do the job :)

like image 23
Amit Avatar answered Oct 06 '22 06:10

Amit


Try this:

list(NULL, 1, 2, 3, NULL, 5) %>% 
     purrr::map_if(is.null, ~ NA_character_) %>% #convert NULL into NA
     is.na() %>% #find NA
     `!` %>%     #Negate
     which()     #get index of Non-NULLs

or even this:

list(NULL, 1, 2, 3, NULL, 5) %>% 
     purrr::map_lgl(is.null) %>% 
     `!` %>% #Negate 
     which()
like image 30
slavakx Avatar answered Oct 06 '22 06:10

slavakx