How can I delete component from list in R?

前端 未结 4 1669
面向向阳花
面向向阳花 2021-01-21 08:23

I am trying to remove a component from list in R, but it is not working.

I have list like this:

> myList
[[1]]
[[1]][[1]]
[1] \"Sunny\"  \"Cloudy\" \"         


        
4条回答
  •  梦谈多话
    2021-01-21 08:32

    I think you came very close to the right answer:

    > x <- list(list(1:3, 4:6), list(4:7, 2:3), list(4:6,1:2))
    > x[-2]
    [[1]]
    [[1]][[1]]
    [1] 1 2 3
    
    [[1]][[2]]
    [1] 4 5 6
    
    
    [[2]]
    [[2]][[1]]
    [1] 4 5 6
    
    [[2]][[2]]
    [1] 1 2
    

    The above works to get rid of the original second component. Note single square brackets, and compare with:

    x[[-2]]

    Error in x[[-2]] : attempt to select more than one element
    

    Double squrare brackets do not work. (Actually that does work if there are only two compoennts in the list, but do not depend on that.)

    There are numerous places that explain single versus double square brackets. One of them is Circle 8.1.54 of 'The R Inferno' http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

提交回复
热议问题