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.
For the sake of completeness, the purrr package from the popular tidyverse has some useful functions for working with lists -
compact
(introduction) does the trick, too, and works fine with magrittr's%>%
pipes:or
Funny enough, none of the many solutions above remove the empty/blank character string:
""
. But the trivial solution is not easily found:L[L != ""]
.To summarize, here are some various ways to remove unwanted items from an array list.
Use
lengths()
to define lengths of the list elements:One possible approach is
where
This works due to the fact that positive integers get coerced to
TRUE
byFilter
and, hence, are kept, while zero doesn't:Another option(I think more efficient) by keeping index where element length > 0 :