[R] Converting a list to a matrix - I still don't think I have itright

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Feb 17 17:58:04 CET 2005


Please try to use something other than "l2" for showing an example
because it is looks awfully similar to the number "12".

Here is another of doing this but this will only work if you have equal
lengths in each of your elements.

# simulate data
mylist <- lapply( 1:5, rnorm, n=2 )
names(mylist) <- LETTERS[1:5]

t( sapply(mylist, "c") )
      [,1]      [,2]
A 1.908977 0.1801167
B 2.384840 3.3355432
C 4.463627 3.5321393
D 2.952132 5.7028436
E 3.891464 3.6214637

If your vectors are of unequal lengths, then you would have to think of
something clever.

Regards, Adai


On Thu, 2005-02-17 at 16:08 +0100, Dimitris Rizopoulos wrote:
> mat <- matrix(unlist(l2), nrow=length(l2), byrow=TRUE)
> rownames(mat) <- names(l2)
> mat
> 
> Best,
> Dimitris
> 
> ----
> Dimitris Rizopoulos
> Ph.D. Student
> Biostatistical Centre
> School of Public Health
> Catholic University of Leuven
> 
> Address: Kapucijnenvoer 35, Leuven, Belgium
> Tel: +32/16/336899
> Fax: +32/16/337015
> Web: http://www.med.kuleuven.ac.be/biostat/
>      http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
> 
> 
> ----- Original Message ----- 
> From: "michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk>
> To: <r-help at r-project.org>
> Sent: Thursday, February 17, 2005 3:36 PM
> Subject: [R] Converting a list to a matrix - I still don't think I 
> have itright
> 
> 
> > Hi
> >
> > We have touched on this before, but I don't think I quite got it 
> > right.
> >
> > So I have a list, each element of which is a a vector of 2 numbers:
> >
> >> l2
> > $cat000_a01
> > [1] 0.3429944 4.5138244
> >
> > $cat000_a02
> > [1] 0.1929336 4.3064944
> >
> > $cat000_a03
> > [1] -0.2607796  4.1551591
> >
> > What I actually want to convert this into is a matrix with the names
> > (cat000_a01 etc) as row names, the first element of each of the 
> > vectors
> > forming the first column of the new matrix, and the second element 
> > of
> > each of the vectors forming the second column:
> >
> > cat000_a01 0.3429944 4.5138244
> > cat000_a02 0.1929336 4.3064944
> > cat000_a03 -0.2607796  4.1551591
> >
> > What was suggested on the list last time was
> > matrix(unlist(mylist),nrow=length(mylist)).  But if I do this I get:
> >
> >> matrix(unlist(l2),nrow=length(l2))
> >          [,1]       [,2]
> > [1,] 0.3429944  4.3064944
> > [2,] 4.5138244 -0.2607796
> > [3,] 0.1929336  4.1551591
> >
> > Which is not what I want.  Here, the second element of the first 
> > vector
> > in my list has gone into the first column of the new matrix, and 
> > that's
> > not what I want at all.
> >
> > Any more help would be appreciated.
> >
> > Thanks
> > Mick
> >
> > ______________________________________________
> > 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
>




More information about the R-help mailing list