1. Bill Venables and Andreas Krause both pointed out that my solution could
be shortened to:
lapply(mylist,"[",1) or lapply(mylist,"[[",1) (or sapply)
2. Bill also pointed out that I could have used unlist() more intelligently
(he didn't say that -- but he should have!):
unlist(mylist, recursive = F)[...] where the ... gives the indices of the
lists I want in the combined vector: c(A1,B1,A2,B2,A3,B3, ...), that is
c(1,3,5,...) . I LIKE THIS as it is straightforward and seems to be what
unlist() is for!
3. This suggests and Bill also pointed out that a trickier(??) way of doing
the same thing as 2) is:
do.call("c",mylist)[...] where the ... is the same as in 2).
4. Nick Ellis also suggested, but disparaged, the use of the Rows() function
as in unlist(Rows(mylist,1)). However, a quick check of the Rows()
definition shows that it is a for() loop, which is what I wanted to avoid in
the first place. So he's right in disparaging this approach.
Again, my thanks for the boundless enthusiasm and cleverness of all who
replied. As usual, I learned a lot. My original post follows.
-- Bert Gunter
>>>>> ORIGINAL POST <<<<<<
To all:
Suppose I have a long list each of whose components is a list with a fixed
structure. e.g.., a list of the form :
mylist=list(list.1=list(A1,B1), list.2=list(A2,B2), list.3=list(A3,B3)
...and so forth)
Such lists are commonly obtained from the apply() family of functions. For
simplicity in this example, assume that A1,A2, ... are just numbers but that
B1, B2, B3, ... are numeric vectors of possibly different lengths.
I would like to form a numeric vector with all the A's only. One way to do
this is:
unlist(lapply(mylist, FUN=function(x) x[ [1] ] ))
but this seems rather clumsy to me. Is there some straightforward method of
list subscripting that allows me to do the same thing? Please reply to me
personally and I'll summarize anything of interest.
Bert Gunter
Biometrics Research RY 70-38
Merck & Company
P.O. Box 2000
Rahway, NJ 07065-0900
Phone: (732) 594-7765 Fax: (732) 594-1565
"The business of the statistician is to catalyze the scientific learning
process." -- George E.P. Box
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news