67

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 cannot use a list of lists in R, so if I get list l1 from another list, say, l2 then I cannot access elements l1. How can I implement it?

EDIT- I will show an example of what does not work for me:

list1 <- list() list1[1] = 1 list1[2] = 2 list2 <- list() list2[1] = 'a' list2[2] = 'b' list_all <- list(list1, list2) a = list_all[1] a[2] #[[1]] #NULL 

but a should be a list!

0

5 Answers 5

72

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 
Sign up to request clarification or add additional context in comments.

5 Comments

@capoluca To expand on Dason's explanation, list_all[1] does not select the first element of list_all. For lists, [ retrieves sublists while [[ retrieves elements.
If you create a list of duplicate lists like mylist <- list(list1, list1), is list1 copied or referenced twice?
What if I am creating a list of lists in a loop and so I do not have the individual sub-lists computed and stored in variables. Rather, I wish to add them to the list of lists one-by-one as I compute them.
@Leonid It sounds like you should post your own question but is there a reason you aren't using something like lapply?
@Leonid mylist = append(mylist, list(list3))
11

Using your example::

list1 <- list() list1[1] = 1 list1[2] = 2 list2 <- list() list2[1] = 'a' list2[2] = 'b' list_all <- list(list1, list2) 

Use '[[' to retrieve an element of a list:

b = list_all[[1]] b [[1]] [1] 1 [[2]] [1] 2 class(b) [1] "list" 

Comments

10

If you are trying to keep a list of lists (similar to python's list.append()) then this might work:

a <- list(1,2,3) b <- list(4,5,6) c <- append(list(a), list(b)) > c [[1]] [[1]][[1]] [1] 1 [[1]][[2]] [1] 2 [[1]][[3]] [1] 3 [[2]] [[2]][[1]] [1] 4 [[2]][[2]] [1] 5 [[2]][[3]] [1] 6 

Comments

3

The example creates a list of named lists in a loop.

 MyList <- list() for (aName in c("name1", "name2")){ MyList[[aName]] <- list(aName) } MyList[["name1"]] MyList[["name2"]] 

To add another list named "name3" do write:

 MyList$name3 <- list(1, 2, 3) 

Comments

0

As other answers pointed out in a more complicated way already, you did already create a list of lists! It's just the odd output of R that confuses (everybody?). Try this:

> str(list_all) List of 2 $ :List of 2 ..$ : num 1 ..$ : num 2 $ :List of 2 ..$ : chr "a" ..$ : chr "b" 

And the most simple construction would be this:

> str(list(list(1, 2), list("a", "b"))) List of 2 $ :List of 2 ..$ : num 1 ..$ : num 2 $ :List of 2 ..$ : chr "a" ..$ : chr "b" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.