提问人:David Locke 提问时间:3/17/2009 最后编辑:KimDavid Locke 更新时间:11/3/2023 访问量:667093
如何从列表中删除元素?
How can I remove an element from a list?
答:
我根本不了解 R,但一些创造性的谷歌搜索将我带到了这里:http://tolstoy.newcastle.edu.au/R/help/05/04/1919.html
那里的关键引述:
我没有找到关于如何从列表中删除元素的 R 明确文档,但反复试验告诉我
myList[[5]] <- NULL
将删除第 5 个元素,然后“关闭”因删除该元素而导致的孔。这充斥着索引值,所以我在删除元素时必须小心。我必须从列表的后面工作到前面。
若要删除列表的元素,请参阅 R 常见问题解答 7.1
...不要将 x[i] 或 x[[i]] 设置为 NULL,因为这会从列表中删除相应的组件。
这似乎告诉您(以一种有点倒退的方式)如何删除元素。
评论
Error in list[length(list)] <- NULL : replacement has length zero
within
如果您不想就地修改列表(例如,将删除元素的列表传递给函数),则可以使用索引:负索引表示“不包含此元素”。
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
评论
x$b
x[[2]] = c("b","k")
%in%
b
从单行列表中删除 Null 元素:
x=x[-(which(sapply(x,is.null),arr.ind=TRUE))]
干杯
评论
x
compact
plyr
-(which(sapply(x,is.null),arr.ind=TRUE))
named integer(0)
以下是在 R 中删除列表的最后一个元素的方法:
x <- list("a", "b", "c", "d", "e")
x[length(x)] <- NULL
如果 x 可能是一个向量,那么你需要创建一个新对象:
x <- c("a", "b", "c", "d", "e")
x <- x[-length(x)]
- 处理列表和向量
评论
您可以使用 .which
x<-c(1:5)
x
#[1] 1 2 3 4 5
x<-x[-which(x==4)]
x
#[1] 1 2 3 5
如果您有一个命名列表,并且想要删除特定元素,您可以尝试:
lst <- list(a = 1:4, b = 4:8, c = 8:10)
if("b" %in% names(lst)) lst <- lst[ - which(names(lst) == "b")]
这将创建一个包含元素 , , .第二行在检查元素是否存在后删除元素(以避免@hjv提到的问题)。lst
a
b
c
b
或更好:
lst$b <- NULL
这样,尝试删除不存在的元素(例如lst$g <- NULL
)
有 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
评论
不知道你是否还需要这个问题的答案,但我从我有限的(3 周的自学 R)经验中发现,使用赋值实际上是错误的或次优的,特别是如果你在类似 for 循环中动态更新列表。NULL
更准确地说,使用
myList[[5]] <- NULL
将抛出错误
myList[[5]] <- NULL : 替换的长度为零
或
供应的元件多于要更换的元件
我发现更一致的工作是
myList <- myList[[-5]]
评论
[[-5]]
myList <- myList[-5]
这个怎么样?同样,使用索引
> 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
评论
m[1:(length(m) - 1)]
只是想快速添加(因为我没有在任何答案中看到它)对于命名列表,您也可以这样做.例如:l["name"] <- NULL
l <- list(a = 1, b = 2, cc = 3)
l['b'] <- NULL
使用 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
评论
对于命名列表,我发现这些辅助函数很有用
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
我想补充一点,如果它是一个命名列表,您可以简单地使用 .within
l <- list(a = 1, b = 2)
> within(l, rm(a))
$b
[1] 2
因此,您可以覆盖原始列表
l <- within(l, rm(a))
从列表中删除名为 的元素。a
l
评论
within(l, rm(a, b))
x <- c("a","b"); within(l,rm(list=x))
使用(负号)和元素的位置,例如,如果要删除第三个元素,请将其用作-
your_list[-3]
输入
my_list <- list(a = 3, b = 3, c = 4, d = "Hello", e = NA)
my_list
# $`a`
# [1] 3
# $b
# [1] 3
# $c
# [1] 4
# $d
# [1] "Hello"
# $e
# [1] NA
从列表中删除单个元素
my_list[-3]
# $`a`
# [1] 3
# $b
# [1] 3
# $d
# [1] "Hello"
# $e
[1] NA
从列表中删除多个元素
my_list[c(-1,-3,-2)]
# $`d`
# [1] "Hello"
# $e
# [1] NA
my_list[c(-3:-5)]
# $`a`
# [1] 3
# $b
# [1] 3
my_list[-seq(1:2)]
# $`c`
# [1] 4
# $d
# [1] "Hello"
# $e
# [1] NA
如果您想避免使用数字索引,可以使用
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
还可以使用包的功能从列表中负索引以删除列表项。extract
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
这是一个可以使用基础 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
}
}
该变量不再包含 5。new_list
new_list
# [1] 1 2 3 4 6 7 8 9 10
包中有几个选项未提及: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
或者,您可以使用赋值 或 :modify_list
zap()
NULL
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
评论
pluck(l, "d", "e") <- NULL
为我工作。我尝试过摆脱类似的东西,但在我闪亮的应用程序中,我得到了.您的语句实际上删除了该值。谢谢!names(list(a = 1, b = 2, c = 3)[c("b", "c")])
a
NA, b, c
pluck
a
names(list(a = 1, b = 2, c = 3)[c("b", "c")])
"b" "c"
a
NULL
a
"NA", "b", "c"
pluck
NA
如果您只想删除元素的第一个匹配项并保留其余元素"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"
评论
假设“remove”意味着在不更改整体顺序的情况下将其设置为“NULL”。
mylist <- list("a", "b", "c", "d", "e", "f", "g")
# remove rule (or)
# 1. remove letter "e" & "f"
# 2. remove the 2nd value
mapply(
\(x, y) if (x %in% c("e", "f") | y == 2) { NULL } else { x },
mylist,
1:length(mylist)
)
# output:
# [[1]]
# [1] "a"
#
# [[2]]
# NULL
#
# [[3]]
# [1] "c"
#
# [[4]]
# [1] "d"
#
# [[5]]
# NULL
#
# [[6]]
# NULL
#
# [[7]]
# [1] "g"
上一个:如何从列表中删除元素?
下一个:如何将绘图保存为磁盘上的图像?
评论