How can I remove an element from a list?

前端 未结 16 1440
傲寒
傲寒 2020-11-28 01:21

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

16条回答
  •  清酒与你
    2020-11-28 01:51

    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
    

提交回复
热议问题