try this:

> b <- as.matrix(do.call('rbind', lapply(a,'[[','df')))
> str(b)
 num [1:2, 1:3] 1 4 2 5 3 6
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:2] "1" "2"
  ..$ : chr [1:3] "A" "B" "C"

> colMeans(b)
  A   B   C
2.5 3.5 4.5
> b
  A B C
1 1 2 3
2 4 5 6
>



On 12/14/06, Joerg van den Hoff <[EMAIL PROTECTED]> wrote:
>
> I have encountered the following problem: I need to extract from
> a list of lists equally named compenents who happen to be 'one row'
> data frames. a trivial example would be:
>
> a <- list(list(
> df = data.frame(A = 1, B = 2, C = 3)), list(df = data.frame(A = 4,B = 5,C
> = 6)))
>
> I want the extracted compenents to fill up a matrix or data frame row by
> row.
> the obvious thing to do seems:
>
> b <- sapply(a, "[[", "df")
> b <- t(b)
>
> now `b' looks all right:
>
> b
> class(b)
>
> but it turns out that all elements in this matrix are one element lists:
>
> class(b[1,1])
>
> which prevents any further standard processing of `b' (like `colMeans',
> e.g.)
>
> question 1: is their a straightforward way to enforce that `b' contains
> simple numbers as elements right from the start (instead of something like
> apply(b, 1:2, "class<-", "numeric") afterwards)?
>
> question 2: should not sapply do this further 'simplification' anyway in a
> situation
> like this (matrix elements turn out to be one-element lists)?
>
> regards
>
> joerg
>
> ______________________________________________
> R-help@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
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
R-help@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
and provide commented, minimal, self-contained, reproducible code.

Reply via email to