[R] Matrix from List

jim holtman jholtman at gmail.com
Sat Jul 26 04:44:20 CEST 2008


This gets you close to what you want.  The 31/33 are row names that
you can extract and store as actual columns,

> x <- list('1995'=list('31'=1, '33'=2), '2006'=list('31'=3, '33'=4))
> y <- lapply(names(x), function(.L1){
+     cbind(as.numeric(.L1),do.call(rbind, x[[.L1]]))
+ })
> (z <- do.call(rbind, y))
   [,1] [,2]
31 1995    1
33 1995    2
31 2006    3
33 2006    4
> cbind(z, as.numeric(rownames(z)))
   [,1] [,2] [,3]
31 1995    1   31
33 1995    2   33
31 2006    3   31
33 2006    4   33
>



On Fri, Jul 25, 2008 at 5:32 PM, Nelson Villoria <nvilloria at gmail.com> wrote:
> Hello, I have a list in which each element is a list. I want to
> create a matrix indexed by the two indices of the list. I have been
> using do.call, but I am not getting what I want. Let me show you:
>
>> l.intercepts #the list that nests another list
> $`1995`
> $`1995`$`31`
> (Intercept)
>   25.37164
>
> $`1995`$`33`
> (Intercept)
>   26.66755
>
>
> $`2006`
> $`2006`$`31`
> (Intercept)
>   25.86621
>
> $`2006`$`33`
> (Intercept)
>   26.44245
>
> I want a matrix like
>
> 1995 31 25.37164
> 1995 33 26.66755
> 2006 31 25.86621
> 2006 33 26.44245
>
> I notice that if I do:
>
>> l.intercepts_1 <- lapply(l.intercepts, function(x) do.call(rbind, x))
>
> I get:
>
>> l.intercepts_1
> $`1995`
>    (Intercept)
> 31     25.37164
> 33     26.66755
>
> $`2006`
>    (Intercept)
> 31     25.86621
> 33     26.44245
>
> However,If I further write:
>> do.call("rbind", l.intercepts_1)
> I get:
>    (Intercept)
> 31     25.37164
> 33     26.66755
> 31     25.86621
> 33     26.44245
>
> Why do.call did not index by year (i.e. 1995 and 2006) as it did
> before for 31 and 33? Any suggestion about how to accomplish this
> task? Help is greatly appreciated.
>
> Nelson Villoria
>
> ______________________________________________
> R-help at r-project.org 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?



More information about the R-help mailing list