How can I make a list of lists in R?

后端 未结 5 686
逝去的感伤
逝去的感伤 2020-12-24 00:58

I don\'t know how to make a list of lists in R. I have several lists, I want to store them in one data structure to make accessing them easier. However, it looks like you ca

5条回答
  •  醉酒成梦
    2020-12-24 01:49

    You can easily make lists of lists

    list1 <- list(a = 2, b = 3)
    list2 <- list(c = "a", d = "b")
    mylist <- list(list1, list2)
    

    mylist is now a list that contains two lists. To access list1 you can use mylist[[1]]. If you want to be able to something like mylist$list1 then you need to do somethingl like

    mylist <- list(list1 = list1, list2 = list2)
    # Now you can do the following
    mylist$list1
    

    Edit: To reply to your edit. Just use double bracket indexing

    a <- list_all[[1]]
    a[[1]]
    #[1] 1
    a[[2]]
    #[1] 2
    

提交回复
热议问题