Dear all, I need to stack the columns of a matrix with a variable number of columns. I am aware of new<-c(a[,1],a[,2]) if I have a matrix 'a' with e.g. two columns. For a simulation study I need to stack the matrix with a variable number of columns. Is there a way to do this? Many thanks in advance Peter -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Peter Schlattman asks:> -----Original Message----- > From: Dr. Peter Schlattmann > [mailto:schlatti at mailer.ukbf.fu-berlin.de] > Sent: Wednesday, June 12, 2002 11:37 PM > To: r-help at stat.math.ethz.ch > Subject: [R] How to stack the columns of matrix > > Dear all, > > I need to stack the columns of a matrix with a variable number of columns. > I am aware of > > new<-c(a[,1],a[,2]) > > if I have a matrix 'a' with e.g. two columns. For a simulation study I > need to stack > the matrix with a variable number of columns. Is there a way to do this?[WNV] In a sense they are already 'stacked', as you put it, in a sense. X[i, j] refers to the matrix element, X[k] refers to the kth element of the column stacked matrix (unless k itself is a matrix, but that is another story). To make it official with no going back you could use either new <- as.vector(a) new <- a; dim(new) <- NULL or a variety of other dodges. After either of these new is the stacked version of a. Bill Venables.> Many thanks in advance > Peter > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.- > r-help mailing list -- Read > http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Wed, 12 Jun 2002, Dr. Peter Schlattmann wrote:> Dear all, > > I need to stack the columns of a matrix with a variable number of columns. > I am aware of > > new<-c(a[,1],a[,2]) > > if I have a matrix 'a' with e.g. two columns. For a simulation study I > need to stack > the matrix with a variable number of columns. Is there a way to do this? >aa <- matrix(1:25, nrow=5) aa as.vector(aa) as.vector() seems to stack as you require by column. Roger -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Breiviksveien 40, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93 e-mail: Roger.Bivand at nhh.no and: Department of Geography and Regional Development, University of Gdansk, al. Mar. J. Pilsudskiego 46, PL-81 378 Gdynia, Poland. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 06/12/02 15:36, Dr. Peter Schlattmann wrote:>I need to stack the columns of a matrix with a variable number of columns. >I am aware of > >new<-c(a[,1],a[,2]) > >if I have a matrix 'a' with e.g. two columns. For a simulation study I >need to stack >the matrix with a variable number of columns. Is there a way to do this?I'm not sure what you mean by "stack," but I think as.vector(a) will do what you want. It creates a vector out of a matrix, column by column. The length of the vector will be rows x columns of a. -- Jonathan Baron, Professor of Psychology, University of Pennsylvania R page: http://finzi.psych.upenn.edu/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
as.vector is probably what you need. Giovanni -- __________________________________________________ [ ] [ Giovanni Petris GPetris at uark.edu ] [ Department of Mathematical Sciences ] [ University of Arkansas - Fayetteville, AR 72701 ] [ Ph: (479) 575-6324, 575-8630 (fax) ] [ http://definetti.uark.edu/~gpetris/ ] [__________________________________________________]> Date: Wed, 12 Jun 2002 15:36:40 +0200 (CEST) > From: "Dr. Peter Schlattmann" <schlatti at mailer.ukbf.fu-berlin.de> > Sender: owner-r-help at stat.math.ethz.ch > Precedence: SfS-bulk > Content-Type: TEXT/PLAIN; charset=US-ASCII > Content-Length: 663 > > Dear all, > > I need to stack the columns of a matrix with a variable number of columns. > I am aware of > > new<-c(a[,1],a[,2]) > > if I have a matrix 'a' with e.g. two columns. For a simulation study I > need to stack > the matrix with a variable number of columns. Is there a way to do this? > > Many thanks in advance > Peter > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
If you have no idea of the max. number of cols, then a <- NULL and each time you have a new vector to add: a <- cbind(a,newvector) If you can have some idea of the maximum number of cols., it seems that it's more efficient for the memory to create a <- matrix(0,nrow=nr, ncol=maxcol) and then newcolnumber <- newcolnumber + 1 #having newcolnumber <- 0 at the begining of the function a[,newcolnumber] <- newvector A the end, keep only those cols that have been used (the last value of newcolnumber): a[,1:newcolnumber] Agus On Wed, 12 Jun 2002, Dr. Peter Schlattmann wrote:> Dear all, > > I need to stack the columns of a matrix with a variable number of columns. > I am aware of > > new<-c(a[,1],a[,2]) > > if I have a matrix 'a' with e.g. two columns. For a simulation study I > need to stack > the matrix with a variable number of columns. Is there a way to do this? > > Many thanks in advance > Peter > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._