# [R] lists within a list / data-structure problem

james.holtman@convergys.com james.holtman at convergys.com
Mon Dec 13 17:46:19 CET 2004

```

construct you list in the loop:

x.all <- list()  # initialize
for (i in 1:limit){
.......
x.all[[i]] <- result.list
}

now you want to name them, e.g., run1 ....

names(x.all) <- paste('run', seq(length(x.all)), sep='')

To access, you can do    x.all\$run1\$Dom

To extract all the 'Dom's

lapply(x.all, function(x) x\$Dom)

HTH
__________________________________________________________
James Holtman        "What is the problem you are trying to solve?"
Executive Technical Consultant  --  Office of Technology, Convergys
james.holtman at convergys.com
+1 (513) 723-2929

Jan Wantia
<wantia at ifi.unizh.ch>        To:       r-help at stat.math.ethz.ch
Sent by:                     cc:
r-help-bounces at stat.m        Subject:  [R] lists within a list / data-structure problem
ath.ethz.ch

12/13/2004 10:59

Dear all,

this is a rather basic question; i am not sure how to structure my data
well:
I want to extraxt various measures from my raw-data. These measures are
of different sizes, so I decided to store them in a list, like:

run1 <- list(Dom = (my_vector), mean = (my_single_number))

I can do that in a for loop for 40 runs, ending up with 40 lists: run1,
run2, ..., run40.
To have all the measurements neatly together I thought of making another
list, containing  40 sub-lists:

> ALL <- list(run1, run2,..., run40)
> ALL
[[1]]
[[1]]\$Dom
[1] "my_vector"

[[1]]\$mean
[1] "my_single_number"

[[2]]
[[2]]\$Dom
[1] "my_vector"

[[2]]\$mean
[1] "my_single_number"

...

1) This may be a bit clumsy as I have to type all the sub-list's names
in by hand in order to produce my ALL-list: Is there a better way?

2) I have problems of addressing the data now. I can easily access any
single value; for example, for the second component of the second sub-
list:

> ALL[[2]][[2]]
[1] "my_single_number",

but: how could I get the second component of all sub-lists, to plot, for
example, all the \$mean in one plot? For a matrix, mat[,2] would give me
the whole second column, but
ALL[[]][[2]]
does not return all the second components.

I feel that 'lapply' might help me here, but I could not figure out
exactly how to use it, and it always comes down to the problem of how to
correctly address the components in the sublists.

Or is there maybe a smarter way to do that instead of using a list of
lists?

Any hint would be warmly appreciated!

Jan
(R 2.0.1 on windows XP)

--

______________________________________________________

Jan Wantia
Deptartment of Informatics, University of Zurich
Andreasstr. 15
CH 8050 Zurich
Switzerland

Tel.:    +41 (0) 1 635 4315
Fax:     +41 (0) 1 635 45 07
email: wantia at ifi.unizh.ch

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help