Remove empty elements from list with character(0)

后端 未结 5 1866
渐次进展
渐次进展 2020-12-13 08:33

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

list2
# $`hsa:7476         


        
5条回答
  •  Happy的楠姐
    2020-12-13 09:00

    One possible approach is

    Filter(length, l)
    # [[1]]
    # [1] 1 2 3
    # 
    # [[2]]
    # [1] "foo"
    

    where

    l <- list(1:3, "foo", character(0), integer(0))
    

    This works due to the fact that positive integers get coerced to TRUE by Filter and, hence, are kept, while zero doesn't:

    as.logical(0:2)
    # [1] FALSE  TRUE  TRUE
    

提交回复
热议问题