# [R] lists of lists

Tamas Papp tpapp at axelero.hu
Sun Mar 9 10:33:25 CET 2003

```I have a function that returns multiple elements in a list (for the
sake of the example here, this will simply be a list of two elements,
a and b, but it is actually five named elements).

This functions computes these values in an iterative manner, and I
would like to start with an empty list and append the list of return
values to this list (so that they would remain lists). The Scheme code
for the resulting list would look something like

(list (list 1 2) (list 3 4) (list 4 5))

I tried to do the same thing in R:

> l <- list()
> l <- list(unlist(l, recursive=FALSE), list(a=1, b=2)) # OK
> l <- list(unlist(l, recursive=FALSE), list(a=3, b=4)) # OK
> l <- list(unlist(l, recursive=FALSE), list(a=5, b=6)) # fails here

I would like to access the elements with eg l[]\$a.

Strangely enough (at least to me), I find that the following works:

l <- NULL
l <- c(l, list(list(a=1, b=2))) # for any values of 1 and 2, of course ;)

Could somebody please explain why the first way doesn't work, and why
the second does? There seems to be an implicit assumption I am making
about lists in R which is clearly wrong. Comparisons with Scheme would
be welcome (does c() "unquote" the lists it is given?)

Is there a better way to solve this problem (using another data
structure)?

Regards,

Tamas Papp

--
Tamás K. Papp
E-mail: tpapp at axelero.hu (preferred, especially for large messages)
tpapp at westel900.net
Please try to send only (latin-2) plain text, not HTML or other garbage.

```