如何从列表中删除一个元素?

364

我有一个列表,想从中删除单个元素。如何做到这一点?

我试图在参考手册中查找我认为显而易见的函数名称,但没有找到合适的内容。


你想按值删除它吗,例如“值为5”,还是按索引/索引删除,例如“在索引5处的元素”或“在索引c(5:6,10)处的元素”?如果您想按值删除并且存在重复项,则您想要仅删除重复项、第一次出现、最后一次出现还是全部删除?列表中是否保证包含您的元素/索引?我们需要处理列表为空的情况吗?我们需要确保NA被传递(/排除)吗?列表保证是平面的还是可以嵌套的?有多少层深度? - smci
5
setdiff(myList,elementToRemove) - JStrahl
20个回答

3
在命名列表的情况下,我发现这些辅助函数非常有用。
member <- function(list,names){
    ## return the elements of the list with the input names
    member..names <- names(list)
    index <- which(member..names %in% names)
    list[index]    
}


exclude <- function(list,names){
     ## return the elements of the list not belonging to names
     member..names <- names(list)
     index <- which(!(member..names %in% names))
    list[index]    
}  
aa <- structure(list(a = 1:10, b = 4:5, fruits = c("apple", "orange"
)), .Names = c("a", "b", "fruits"))

> aa
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10

## $b
## [1] 4 5

## $fruits
## [1] "apple"  "orange"


> member(aa,"fruits")
## $fruits
## [1] "apple"  "orange"


> exclude(aa,"fruits")
## $a
##  [1]  1  2  3  4  5  6  7  8  9 10

## $b
## [1] 4 5

2
使用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))]
# or
lst %>% purrr::discard(names(.) == "a") # use %in% for a set

感谢您使用tidyverse提供的解决方案! - robertspierre

2

如果您只想删除元素"b"的第一个出现位置并保留其余部分

x <- c("a", "b", "b", "c", "d", "e")

which(x == "b")
# [1] 2 3

which(x == "b")[1]
# [1] 2

x[-which(x == "b")[1]]
# [1] "a" "b" "c" "d" "e"

只有当列表中有一个“b”项时,这才有效。 - undefined

2
您可以使用magrittr包的extract函数从列表中进行反向索引,以移除一个列表项。
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

1

purrr包中有一些未被提及的选项:

pluckassign_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

您可以使用modify_list通过赋值zap()NULL来删除值:

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

您可以使用谓词函数与 discardkeep 来删除或保留元素:

# 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

pluck(l, "d", "e") <- NULL 对我很有用。我试过像这样的语句 names(list(a = 1, b = 2, c = 3)[c("b", "c")]) 来除去 a, 但在我的 Shiny 应用中得到了 NA, b, c。你的 pluck 语句实际上删除了 a 值。谢谢! - RAFisherman
@captinbo,我不清楚你想做什么。names(list(a = 1, b = 2, c = 3)[c("b", "c")]) 返回 "b" "c"。如果你想删除 a,那么可以将其赋值为 NULL,或选择其他不是 a 的元素。 - LMc
是的,它返回了“b”、“c”,这正是我想要的。但奇怪的是,在我的R Shiny应用程序中,在运行时它返回了“NA”、“b”、“c”。我花了一个小时在那个错误上,尝试不同的方法来将我的列表子集到我想要的元素,但只有你的“pluck”方法有效。很奇怪,我得到了一个名为“NA”的列表。 - RAFisherman
@captinbo 如果您能制作一个小的可重现的示例并发布到SO上,那么这将是一个很好的问题。 - LMc

0
这里有一个可以使用基础 R 完成的简单解决方案。它从原始数字列表中删除数字 5。您可以使用相同的方法从列表中删除任何元素。
#the original list
original_list = c(1:10)

#the list element to remove
remove = 5

#the new list (which will not contain whatever the `remove` variable equals)
new_list = c()

#go through all the elements in the list and add them to the new list if they don't equal the `remove` variable
counter = 1
for (n in original_list){
  if (n != ){
    new_list[[counter]] = n
    counter = counter + 1
  }
}

new_list变量不再包含5。

new_list
# [1]  1  2  3  4  6  7  8  9 10

0
假设'remove'的意思是将其设为'NULL',而不改变整体顺序。
x <- list("a", "b", "c", "d", "e", "f", "g")

# remove rule (or)
# 1. x %in% c("e", "f")
# 2. remove the 2nd value

mapply(
  \(x, y) if (x %in% c("e", "f") | y == 2) { NULL } else { x },
  x,
  1:length(x)
)

# output:
# [[1]]
# [1] "a"
# 
# [[2]]
# NULL
# 
# [[3]]
# [1] "c"
# 
# [[4]]
# [1] "d"
# 
# [[5]]
# NULL
# 
# [[6]]
# NULL
# 
# [[7]]
# [1] "g"


-1

如果您想避免使用数字索引,可以使用

a <- setdiff(names(a),c("name1", ..., "namen"))

从a中删除名称namea...namen。这适用于列表。

> l <- list(a=1,b=2)
> l[setdiff(names(l),"a")]
$b
[1] 2

以及向量相关

> v <- c(a=1,b=2)
> v[setdiff(names(v),"a")]
b 
2

-1
你可以使用 which
x<-c(1:5)
x
#[1] 1 2 3 4 5
x<-x[-which(x==4)]
x
#[1] 1 2 3 5

-1

这样怎么样?再次使用索引

> m <- c(1:5)
> m
[1] 1 2 3 4 5

> m[1:length(m)-1]
[1] 1 2 3 4

或者

> m[-(length(m))]
[1] 1 2 3 4

1
m是一个向量,而不是一个列表。 - C8H10N4O2
1
该方法适用于列表,但是OP很幸运,可能想要更多的括号:m[1:(length(m) - 1)] - Gregor Thomas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接