我有一个列表,我想从其中删除一个元素。我该怎么做呢?

我试着在参考手册中查找我认为这个函数的明显名称,但我没有找到任何合适的名称。


当前回答

purrr包中有几个选项没有被提到:

Pluck和assign_in可以很好地使用嵌套值,你可以使用名称和/或索引的组合来访问它:

library(purrr)

l <- list("a" = 1:2, "b" = 3:4, "d" = list("e" = 5:6, "f" = 7:8))

# select values (by name and/or index)
all.equal(pluck(l, "d", "e"), pluck(l, 3, "e"), pluck(l, 3, 1))
[1] TRUE

# or if element location stored in a vector use !!!
pluck(l, !!! as.list(c("d", "e")))
[1] 5 6

# remove values (modifies in place)
pluck(l, "d", "e") <- NULL

# assign_in to remove values with name and/or index (does not modify in place)
assign_in(l, list("d", 1), NULL)
$a
[1] 1 2

$b
[1] 3 4

$d
$d$f
[1] 7 8

或者你可以通过分配zap()或NULL使用modify_list删除值:

all.equal(list_modify(l, a = zap()), list_modify(l, a = NULL))
[1] TRUE

你可以使用带有discard和keep的谓词函数来删除或保留元素:

# remove numeric elements
discard(l, is.numeric)
$d
$d$e
[1] 5 6

$d$f
[1] 7 8

# keep numeric elements
keep(l, is.numeric)
$a
[1] 1 2

$b
[1] 3 4

其他回答

rlist包(http://cran.r-project.org/web/packages/rlist/index.html)用于处理各种列表操作。

示例(http://cran.r-project.org/web/packages/rlist/vignettes/Filtering.html):

library(rlist)
devs <- 
  list(
    p1=list(name="Ken",age=24,
      interest=c("reading","music","movies"),
      lang=list(r=2,csharp=4,python=3)),
    p2=list(name="James",age=25,
      interest=c("sports","music"),
      lang=list(r=3,java=2,cpp=5)),
    p3=list(name="Penny",age=24,
      interest=c("movies","reading"),
      lang=list(r=1,cpp=4,python=2)))

list.remove(devs, c("p1","p2"))

结果:

# $p3
# $p3$name
# [1] "Penny"
# 
# $p3$age
# [1] 24
# 
# $p3$interest
# [1] "movies"  "reading"
# 
# $p3$lang
# $p3$lang$r
# [1] 1
# 
# $p3$lang$cpp
# [1] 4
# 
# $p3$lang$python
# [1] 2

我想补充的是,如果它是一个命名列表,你可以简单地使用。

l <- list(a = 1, b = 2)    
> within(l, rm(a))
$b
[1] 2

所以你可以覆盖原来的列表

l <- within(l, rm(a)) 

从列表l中删除名为a的元素。

还可以使用magrittr包的提取函数从列表中进行负索引以删除列表项。

a <- seq(1,5)
b <- seq(2,6)
c <- seq(3,7)
l <- list(a,b,c)

library(magrittr)

extract(l,-1) #simple one-function method
[[1]]
[1] 2 3 4 5 6

[[2]]
[1] 3 4 5 6 7

使用lapply和grep:

lst <- list(a = 1:4, b = 4:8, c = 8:10)
# say you want to remove a and c
toremove<-c("a","c")
lstnew<-lst[-unlist(lapply(toremove, function(x) grep(x, names(lst)) ) ) ]
#or
pattern<-"a|c"
lstnew<-lst[-grep(pattern, names(lst))]

如果你不想就地修改列表(例如,将一个元素传递给一个函数),你可以使用索引:负索引表示“不包括这个元素”。

x <- list("a", "b", "c", "d", "e"); # example list

x[-2];       # without 2nd element

x[-c(2, 3)]; # without 2nd and 3rd

同样,逻辑索引向量也很有用:

x[x != "b"]; # without elements that are "b"

这也适用于数据框架:

df <- data.frame(number = 1:5, name = letters[1:5])

df[df$name != "b", ];     # rows without "b"

df[df$number %% 2 == 1, ] # rows with odd numbers only