Hi Sorry to ask such a basic question. I have a list, each element of which is a vector of two values. What I actually want is a matrix with two columns, and one row per element of the list. Obviously I have tried as.matrix(), and as.vector() but I didn't expect the latter to work. I feel so lame asking this. Any suggestions? Mick
do.call() is your friend: mat <- do.call("rbind", myList) Andy> From: michael watson (IAH-C) > > Hi > > Sorry to ask such a basic question. I have a list, each element of > which is a vector of two values. What I actually want is a > matrix with > two columns, and one row per element of the list. Obviously I have > tried as.matrix(), and as.vector() but I didn't expect the latter to > work. > > I feel so lame asking this. Any suggestions? > > 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 > >
try this: mat <- matrix(unlist(lis), ncol=2, byrow=TRUE) I hope it helps. 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 stat.math.ethz.ch> Sent: Thursday, February 03, 2005 4:08 PM Subject: [R] How to convert a list to a matrix> Hi > > Sorry to ask such a basic question. I have a list, each element of > which is a vector of two values. What I actually want is a matrix > with > two columns, and one row per element of the list. Obviously I have > tried as.matrix(), and as.vector() but I didn't expect the latter to > work. > > I feel so lame asking this. Any suggestions? > > 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 >
Will do.call('rbind',yourlist) do what you want? Sean On Feb 3, 2005, at 10:08 AM, michael watson ((IAH-C)) wrote:> Hi > > Sorry to ask such a basic question. I have a list, each element of > which is a vector of two values. What I actually want is a matrix with > two columns, and one row per element of the list. Obviously I have > tried as.matrix(), and as.vector() but I didn't expect the latter to > work. > > I feel so lame asking this. Any suggestions? > > 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
Hello, try the following :> l <- list(NULL) > l[[1]] <- 1:2 > l[[2]] <- 2:3 > l[[3]] <- 4:5 > l[[1]] [1] 1 2 [[2]] [1] 2 3 [[3]] [1] 4 5> matrix(unlist(l),ncol=2)[,1] [,2] [1,] 1 3 [2,] 2 4 [3,] 2 5 Is that what you want ? Romain Le 03.02.2005 16:08, michael watson (IAH-C) a ?crit :>Hi > >Sorry to ask such a basic question. I have a list, each element of >which is a vector of two values. What I actually want is a matrix with >two columns, and one row per element of the list. Obviously I have >tried as.matrix(), and as.vector() but I didn't expect the latter to >work. > >I feel so lame asking this. Any suggestions? > >Mick > > >-- Romain FRANCOIS : francoisromain at free.fr page web : http://addictedtor.free.fr/ (en construction) 06 18 39 14 69 / 01 46 80 65 60 _______________________________________________________ Etudiant en 3eme ann?e Institut de Statistique de l'Universit? de Paris (ISUP) Fili?re Industrie et Services http://www.isup.cicrp.jussieu.fr/
list1 <- list(x=c(1,2), y=c(3,4), z=c(4,5)) matrix(unlist(list1), nrow=length(list1), byrow=T) HTH On Thu, 3 Feb 2005, michael watson (IAH-C) wrote:> Hi > > Sorry to ask such a basic question. I have a list, each element of > which is a vector of two values. What I actually want is a matrix with > two columns, and one row per element of the list. Obviously I have > tried as.matrix(), and as.vector() but I didn't expect the latter to > work. > > I feel so lame asking this. Any suggestions? > > 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 >