[R] lists within a list / data-structure problem
Kjetil Brinchmann Halvorsen
kjetil at acelerate.com
Tue Dec 14 01:17:54 CET 2004
Berton Gunter wrote:
>Jan:
>
>One thing to keep in mind: A list is vector. So vector-type operations like
>c(), "[", etc. work on lists, too (but be careful). Some comments inline
>below that I hope will be helpful. A good reference on the S language is
>V&R's S PROGRAMMING, which I recommend highly.
>
>
>
You can also have a matrix of lists.
Kjetil
>-- Bert Gunter
>Genentech Non-Clinical Statistics
>South San Francisco, CA
>
>"The business of the statistician is to catalyze the scientific learning
>process." - George E. P. Box
>
>
>
>
>
>>-----Original Message-----
>>From: r-help-bounces at stat.math.ethz.ch
>>[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jan Wantia
>>Sent: Monday, December 13, 2004 8:00 AM
>>To: r-help at stat.math.ethz.ch
>>Subject: [R] lists within a list / data-structure problem
>>
>>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:
>>
>>
>>
>
>As you found, this is clumsy. The usual way to do this is to put the results
>into a *single* list as follows:
>
>## Contruct the empty list with 40 components:
>out<-vector("list", 40)
>### loop
>... ## do the calculations
>out[[i]] <- list(Dom = yourvec,mean=yournumb)
>...
>
>
>
>> > 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:
>>
>>
>
>out[[i]] is the ith component of the list, i.e. the ith 2 component list
>containing the result of the ith loop. So out[[i]][[1]] is yourvec for the
>ith loop and out[[i]][[2]] is yournumb. These can be abbreciated as
>out[[c(i,1]] and out[[c(i,2)]]
>
>
>
>> > 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
>>PLEASE do read the posting guide!
>>http://www.R-project.org/posting-guide.html
>>
>>
>>
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
>
>
--
Kjetil Halvorsen.
Peace is the most effective weapon of mass construction.
-- Mahdi Elmandjra
More information about the R-help
mailing list