Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty elements from list with character(0)

Tags:

list

r

How can I remove empty elements from a list that contain zero length pairlist as character(0), integer(0) etc...

list2 # $`hsa:7476` # [1] "1","2","3" #  # $`hsa:656` # character(0) # # $`hsa:7475` # character(0) # # $`hsa:7472` # character(0) 

I don't know how to deal with them. I mean if NULL it is much simpler. How can I remove these elements such that just hsa:7476 remains in the list.

like image 223
Richard A. Schäfer Avatar asked Sep 26 '13 08:09

Richard A. Schäfer


People also ask

How do I remove an empty element from a list?

The easiest way is list comprehension to remove empty elements from a list in Python. And another way is to use the filter() method. The empty string "" contains no characters and empty elements could be None or [ ], etc.

How do I remove empty entries from a list in Python?

Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list.


1 Answers

Another option(I think more efficient) by keeping index where element length > 0 :

l[lapply(l,length)>0] ## you can use sapply,rapply  [[1]] [1] 1 2 3  [[2]] [1] "foo" 
like image 83
agstudy Avatar answered Sep 24 '22 01:09

agstudy