如何从列表中删除元素?

时间:2009-03-16 20:59:36

标签: r list indexing

我有一个列表,我想从中删除一个元素。我怎么能这样做?

我已经尝试查找我认为此函数的明显名称将出现在参考手册中,而我找不到合适的内容。

17 个答案:

答案 0 :(得分:185)

我根本不认识R,但是一些有创意的谷歌搜索让我在这里: http://tolstoy.newcastle.edu.au/R/help/05/04/1919.html

那里的关键引用:

  

我没有找到关于如何从列表中删除元素的R的明确文档,但是试验和错误告诉我

     

myList [[5]]< - NULL

     

将删除第5个元素,然后“关闭”由删除该元素引起的漏洞。这就是索引值的结果,所以我必须小心删除元素。我必须从名单的后面到前线工作。

A response to that post later in the thread声明:

  

要删除列表中的元素,请参阅R FAQ 7.1

relevant section of the R FAQ说:

  

...不要将x [i]或x [[i]]设置为NULL,因为这将从列表中删除相应的组件。

这似乎告诉你(以某种向后的方式)如何删除元素。

希望这有助于,或至少引导您朝着正确的方向前进。

答案 1 :(得分:181)

如果您不想就地修改列表(例如,为了将带有元素的列表传递给函数),您可以使用索引:负索引表示“不包含此元素”。

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

答案 2 :(得分:29)

以下是删除R:

列表的最后一个元素的方法
x <- list("a", "b", "c", "d", "e")
x[length(x)] <- NULL

如果x可能是矢量,那么您需要创建一个新对象:

x <- c("a", "b", "c", "d", "e")
x <- x[-length(x)]
  • 适用于列表向量

答案 3 :(得分:17)

答案 4 :(得分:14)

如果您有一个命名列表并想要删除特定元素,您可以尝试:

lst <- list(a = 1:4, b = 4:8, c = 8:10)

if("b" %in% names(lst)) lst <- lst[ - which(names(lst) == "b")]

这将生成包含lstab元素的列表c。第二行在检查到它存在后删除元素b(以避免提到@hjv的问题)。

或更好:

lst$b <- NULL

这样尝试删除不存在的元素(例如lst$g <- NULL

就不成问题

答案 5 :(得分:7)

有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

答案 6 :(得分:7)

不知道你是否还需要答案,但我从R(我有价值的3周自学R)经验中发现,使用NULL任务实际上是错误的或是特别是如果你动态更新像for-loop这样的列表。

更准确地说,使用

myList[[5]] <- NULL

会抛出错误

  

myList [[5]]&lt; - NULL:替换长度为零

  

提供的元素多于要替换的元素

我发现更加一致的工作是

myList <- myList[[-5]]

答案 7 :(得分:5)

我想补充说,如果它是命名列表,您只需使用within即可。

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

所以你可以覆盖原始列表

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

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

答案 8 :(得分:2)

只是想快速添加(因为我没有在任何答案中看到它),对于命名列表,您也可以l["name"] <- NULL。例如:

l <- list(a = 1, b = 2, cc = 3)
l['b'] <- NULL

答案 9 :(得分:2)

使用sc.textFile("/my/dir1") (负号)以及元素的位置,例如,如果要删除第三个元素,请将其用作-

输入

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

答案 10 :(得分:1)

对于命名列表,我发现这些辅助函数很有用

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

答案 11 :(得分: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

答案 12 :(得分:0)

您还可以使用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

答案 13 :(得分:0)

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

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

答案 14 :(得分:0)

使用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))]

答案 15 :(得分:-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

答案 16 :(得分:-2)

您可以使用which

x<-c(1:5)
x
#[1] 1 2 3 4 5
x<-x[-which(x==4)]
x
#[1] 1 2 3 5
相关问题