[R] Converting a list to a matrix - I still don't think I have itright
Ravi Varadhan
rvaradha at jhsph.edu
Thu Feb 17 16:04:52 CET 2005
This should do it:
matrix(unlist(mylist),nrow=length(mylist), by=T)
--------------------------------------------------------------------------
Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at jhmi.edu
--------------------------------------------------------------------------
> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-
> bounces at stat.math.ethz.ch] On Behalf Of michael watson (IAH-C)
> Sent: Thursday, February 17, 2005 9:37 AM
> To: r-help at r-project.org
> 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
More information about the R-help
mailing list