list 从带有字符(0)的列表中删除空元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19023446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 02:05:05  来源:igfitidea点击:

Remove empty elements from list with character(0)

rlist

提问by Richard A. Sch?fer

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

我怎样才能把包含零长度的成对列表为空表元素 character(0)integer(0)等等......

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 NULLit is much simpler. How can I remove these elements such that just hsa:7476remains in the list.

我不知道怎么对付他们。我的意思是如果NULL它更简单。如何删除这些元素,使其仅hsa:7476保留在列表中。

回答by agstudy

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

另一种选择(我认为更有效)通过保持元素长度 > 0 的索引:

l[lapply(l,length)>0] ## you can use sapply,rapply

[[1]]
[1] 1 2 3

[[2]]
[1] "foo"

回答by Julius Vainora

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 TRUEby Filterand, hence, are kept, while zero doesn't:

该工程由于这样的事实,正整数得到强制为TRUE通过Filter,因此,被保留,而零不:

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

回答by lukeA

For the sake of completeness, the purrrpackage from the popular tidyversehas some useful functions for working with lists - compact(introduction) does the trick, too, and works fine with magrittr's %>%pipes:

为了完整起见,流行的tidyverse 中purrr包有一些有用的函数来处理列表 - (介绍)也可以解决这个问题,并且可以很好地与 magrittr 的管道配合使用:compact%>%

l <- list(1:3, "foo", character(0), integer(0))
library(purrr)
compact(l)
# [[1]]
# [1] 1 2 3
#
# [[2]]
# [1] "foo"

or

或者

list(1:3, "foo", character(0), integer(0)) %>% compact

回答by Artem Klevtsov

Use lengths()to define lengths of the list elements:

使用lengths()定义列表元素的长度:

l <- list(1:3, "foo", character(0), integer(0))
l[lengths(l) > 0L]
#> [[1]]
#> [1] 1 2 3
#> 
#> [[2]]
#> [1] "foo"
#> 

回答by not2qubit

Funny enough, none of the many solutions above remove the empty/blank character string: "". But the trivial solution is not easily found: L[L != ""].

有趣的是,上面的许多解决方案都没有删除空/空白字符串:"". 但并不容易找到微不足道的解决方案:L[L != ""].

To summarize, here are some various ways to remove unwanted items from an array list.

总而言之,这里有一些从数组列表中删除不需要的项目的各种方法。

# Our Example List:
L <- list(1:3, "foo", "", character(0), integer(0))

# 1. Using the *purrr* package:
library(purrr)
compact(L)

# 2. Using the *Filter* function:
Filter(length, L)

# 3. Using *lengths* in a sub-array specification:
L[lengths(L) > 0]

# 4. Using *lapply* (with *length*) in a sub-array specification:
L[lapply(L,length)>0]

# 5. Using a sub-array specification:
L[L != ""]

# 6. Combine (3) & (5)
L[lengths(L) > 0 & L != ""]