我有一个列表,想从中删除单个元素。如何做到这一点?
我试图在参考手册中查找我认为显而易见的函数名称,但没有找到合适的内容。
我有一个列表,想从中删除单个元素。如何做到这一点?
我试图在参考手册中查找我认为显而易见的函数名称,但没有找到合适的内容。
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
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
如果您只想删除元素"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"
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
在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
, 但在我的 Shiny 应用中得到了 NA, b, c
。你的 pluck
语句实际上删除了 a
值。谢谢! - RAFishermannames(list(a = 1, b = 2, c = 3)[c("b", "c")])
返回 "b" "c"
。如果你想删除 a
,那么可以将其赋值为 NULL
,或选择其他不是 a
的元素。 - LMc#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
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"
如果您想避免使用数字索引,可以使用
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
which
。x<-c(1:5)
x
#[1] 1 2 3 4 5
x<-x[-which(x==4)]
x
#[1] 1 2 3 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)]
。 - Gregor Thomas