Re: [R] Matrix from List

From: jim holtman <jholtman_at_gmail.com>
Date: Fri, 25 Jul 2008 22:44:20 -0400

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?

______________________________________________
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.
Received on Sat 26 Jul 2008 - 02:47:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 26 Jul 2008 - 04:32:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive