csdnceshi73
喵-见缝插针
采纳率0%
2009-03-16 20:59

如何从列表中删除元素?

已采纳

I have a list and I want to remove a single element from it. How can I do this?

I've tried looking up what I think the obvious names for this function would be in the reference manual and I haven't found anything appropriate.

转载于:https://stackoverflow.com/questions/652136/how-can-i-remove-an-element-from-a-list

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

15条回答

  • csdnceshi57 perhaps? 12年前

    I don't know R at all, but a bit of creative googling led me here: http://tolstoy.newcastle.edu.au/R/help/05/04/1919.html

    The key quote from there:

    I do not find explicit documentation for R on how to remove elements from lists, but trial and error tells me

    myList[[5]] <- NULL

    will remove the 5th element and then "close up" the hole caused by deletion of that element. That suffles the index values, So I have to be careful in dropping elements. I must work from the back of the list to the front.

    A response to that post later in the thread states:

    For deleting an element of a list, see R FAQ 7.1

    And the relevant section of the R FAQ says:

    ... Do not set x[i] or x[[i]] to NULL, because this will remove the corresponding component from the list.

    Which seems to tell you (in a somewhat backwards way) how to remove an element.

    Hope that helps, or at least leads you in the right direction.

    点赞 25 评论 复制链接分享
  • csdnceshi60 ℡Wang Yan 12年前

    If you don't want to modify the list in-place (e.g. for passing the list with an element removed to a function), you can use indexing: negative indices mean "don't include this element".

    x <- list("a", "b", "c", "d", "e"); # example list
    
    x[-2];       # without 2nd element
    
    x[-c(2, 3)]; # without 2nd and 3rd
    

    Also, logical index vectors are useful:

    x[x != "b"]; # without elements that are "b"
    

    This works with dataframes, too:

    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
    
    点赞 29 评论 复制链接分享
  • csdnceshi73 喵-见缝插针 11年前

    Removing Null elements from a list in single line :

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

    Cheers

    点赞 10 评论 复制链接分享
  • csdnceshi58 Didn"t forge 6年前

    Don't know if you still need an answer to this but I found from my limited (3 weeks worth of self-teaching R) experience with R that, using the NULL assignment is actually wrong or sub-optimal especially if you're dynamically updating a list in something like a for-loop.

    To be more precise, using

    myList[[5]] <- NULL
    

    will throw the error

    myList[[5]] <- NULL : replacement has length zero

    or

    more elements supplied than there are to replace

    What I found to work more consistently is

    myList <- myList[[-5]]
    
    点赞 10 评论 复制链接分享
  • weixin_41568134 MAO-EYE 3年前

    Use - (Negative sign) along with position of element, example if 3rd element is to be removed use it as your_list[-3]

    Input

    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
    

    Remove single element from list

     my_list[-3]
     # $`a`
     # [1] 3
    
     # $b
     # [1] 3
    
     # $d
     # [1] "Hello"
    
     # $e
     [1] NA
    

    Remove multiple elements from list

     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
    
    点赞 9 评论 复制链接分享
  • csdnceshi64 游.程 10年前

    Here is how the remove the last element of a list in R:

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

    If x might be a vector then you would need to create a new object:

    x <- c("a", "b", "c", "d", "e")
    x <- x[-length(x)]
    
    • Work for lists and vectors
    点赞 7 评论 复制链接分享
  • csdnceshi61 derek5. 7年前

    If you have a named list and want to remove a specific element you can try:

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

    This will make a list lst with elements a, b, c. The second line removes element b after it checks that it exists (to avoid the problem @hjv mentioned).

    or better:

    lst$b <- NULL
    

    This way it is not a problem to try to delete a non-existent element (e.g. lst$g <- NULL)

    点赞 7 评论 复制链接分享
  • csdnceshi71 Memor.の 3年前

    I would like to add that if it's a named list you can simply use within.

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

    So you can overwrite the original list

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

    to remove element named a from list l.

    点赞 7 评论 复制链接分享
  • csdnceshi61 derek5. 3年前

    In the case of named lists I find those helper functions useful

    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
    
    点赞 7 评论 复制链接分享
  • weixin_41568127 ?yb? 7年前

    There's the rlist package (http://cran.r-project.org/web/packages/rlist/index.html) to deal with various kinds of list operations.

    Example (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"))
    

    Results in:

    # $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 评论 复制链接分享
  • csdnceshi68 local-host 4年前

    Just wanted to quickly add (because I didn't see it in any of the answers) that, for a named list, you can also do l["name"] <- NULL. For example:

    l <- list(a = 1, b = 2, cc = 3)
    l['b'] <- NULL
    
    点赞 2 评论 复制链接分享
  • weixin_41568174 from.. 4年前

    Using lapply and 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))]
    
    点赞 评论 复制链接分享
  • csdnceshi68 local-host 3年前

    if you'd like to avoid numeric indices, you can use

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

    to delete names namea...namen from a. this works for lists

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

    as well as for vectors

    > v <- c(a=1,b=2)
    > v[setdiff(names(v),"a")]
    b 
    2
    
    点赞 评论 复制链接分享
  • csdnceshi70 笑故挽风 6年前

    How about this? Again, using indices

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

    or

    > m[-(length(m))]
    [1] 1 2 3 4
    
    点赞 评论 复制链接分享
  • csdnceshi63 elliott.david 9年前

    You can use which.

    x<-c(1:5)
    x
    #[1] 1 2 3 4 5
    x<-x[-which(x==4)]
    x
    #[1] 1 2 3 5
    
    点赞 评论 复制链接分享