代码之家  ›  专栏  ›  技术社区  ›  David Locke

如何从列表中删除元素?

  •  220
  • David Locke  · 技术社区  · 16 年前

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

    15 回复  |  直到 4 年前
        1
  •  253
  •   Florian Jenn    16 年前

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

    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
  •  240
  •   Chad Birch    16 年前

    我一点也不知道R,但一点创造性的谷歌搜索让我来到了这里: http://tolstoy.newcastle.edu.au/R/help/05/04/1919.html

    这里的关键引语是:

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

    myList[[5]]<-无效的

    将移除第5个元件,然后“封闭”因删除该元件而导致的孔。这会淹没索引值,因此在删除元素时必须小心。我必须从头到尾地工作。

    A. response to that post later in the thread 国家:

    relevant section of the R FAQ 说:

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

    它似乎告诉您(以一种有点倒退的方式)如何删除元素。

    希望这能帮助你,或者至少能引导你朝着正确的方向前进。

        3
  •  37
  •   Aleksandr Levchuk Wes    13 年前

    列表 在R中:

    x <- list("a", "b", "c", "d", "e")
    x[length(x)] <- NULL
    

    x <- c("a", "b", "c", "d", "e")
    x <- x[-length(x)]
    
    • 为…工作 列表 载体
        4
  •  33
  •   Kim    7 年前

    我想补充一点,如果是 名单 你可以简单地使用 within .

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

    因此,您可以覆盖原始列表

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

    a 来自列表 l .

        5
  •  22
  •   Sukhi    12 年前

    从单行列表中删除空元素:

    x=x[-(which(sapply(x,is.null),arr.ind=TRUE))]

        6
  •  17
  •   alko989    11 年前

    如果您有一个命名列表,并且希望删除特定元素,则可以尝试:

    lst <- list(a = 1:4, b = 4:8, c = 8:10)
    
    if("b" %in% names(lst)) lst <- lst[ - which(names(lst) == "b")]
    

    这将列出一个清单 lst a , b , c . 第二行删除元素 在检查它是否存在之后(为了避免@hjv提到的问题)。

    或者更好:

    lst$b <- NULL
    

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

        7
  •  10
  •   user2030503    10 年前

    这是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
    
        8
  •  9
  •   tuomastik zoubir REMILA    7 年前

    我不知道你是否还需要一个答案,但我从我有限的(3周的自学R)经验中发现,使用 NULL

    更准确地说,使用

    myList[[5]] <- NULL
    

    将抛出错误

    myList[[5]]<-NULL:替换的长度为零

    我发现工作更稳定的是

    myList <- myList[[-5]]
    
        9
  •  9
  •   Sowmya S. Manian    6 年前

    使用 - (负号)以及元件的位置,例如,如果要移除第三个元件,则将其用作 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
    
        10
  •  5
  •   Alexey Shiklomanov    8 年前

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

    l <- list(a = 1, b = 2, cc = 3)
    l['b'] <- NULL
    
        11
  •  3
  •   DJJ    7 年前

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

    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
    
        12
  •  1
  •   Ferroao    7 年前

    使用lappy和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))]
    
        13
  •  0
  •   Roasty247    4 年前

    您还可以使用 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
    
        14
  •  0
  •   arranjdavis    3 年前

    下面是一个简单的解决方案,可以使用base 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
    
        15
  •  -1
  •   C8H10N4O2    9 年前

    这个怎么样?同样,使用索引

    > 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
  •  -1
  •   user2100721    8 年前

    你可以用 which

    x<-c(1:5)
    x
    #[1] 1 2 3 4 5
    x<-x[-which(x==4)]
    x
    #[1] 1 2 3 5
    
        17
  •  -1
  •   Greg Minshall    6 年前

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

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

    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