michael watson (IAH-C)
2005-Feb-17 14:36 UTC
[R] Converting a list to a matrix - I still don't think I have it right
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
Sean Davis
2005-Feb-17 15:03 UTC
[R] Converting a list to a matrix - I still don't think I have it right
Mick, Does this do it? mymat <- do.call('rbind',l2) rownames(mymat) <- names(l2) Sean On Feb 17, 2005, at 9:36 AM, michael watson ((IAH-C)) wrote:> 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
Ravi Varadhan
2005-Feb-17 15:04 UTC
[R] Converting a list to a matrix - I still don't think I have itright
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
Dimitris Rizopoulos
2005-Feb-17 15:08 UTC
[R] Converting a list to a matrix - I still don't think I have itright
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 >
Roger D. Peng
2005-Feb-17 15:23 UTC
[R] Converting a list to a matrix - I still don't think I have it right
Does do.call("rbind", l2) do what you want? -roger michael watson (IAH-C) wrote:> 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 >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
Petr Pikal
2005-Feb-17 15:28 UTC
[R] Converting a list to a matrix - I still don't think I have it right
Hi Michael On 17 Feb 2005 at 14:36, michael watson (IAH-C) wrote:> 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.1551591Try byrow =TRUE argument> x<-list(a=c(1,2), b=c(4,5), d= c(10,12))> matrix(unlist(x),nrow=length(x), byrow=T)[,1] [,2] [1,] 1 2 [2,] 4 5 [3,] 10 12 Cheers Petr> 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.htmlPetr Pikal petr.pikal at precheza.cz
Peter Dalgaard
2005-Feb-17 15:39 UTC
[R] Converting a list to a matrix - I still don't think I have it right
"michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk> writes:> 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.1551591do.call("rbind", list) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Reasonably Related Threads
- Converting a list to a matrix - I still don't think I hav e it right
- Define replacement functions
- is.null(mylist[1]) and is.null(mylist$a) returns different values
- NULL elements in lists ... a nightmare
- applying quantile to a list using values of another object as probs