# how to collapse a list of 1 column matrix to a matrix?

8 messages
Open this post in threaded view
|

## how to collapse a list of 1 column matrix to a matrix?

 Hi, I encounter a situation where I have a list whose element is a column matrix. Says, \$'1' [,1] 1 2 3 \$'2' [,1] 4 5 6 Is there fast way to collapse the list into a matrix like a cbind operation in this case? Meaning, the result should be a matrix that looks like:       [,1]  [,2] [1,]    1      4 [2,]    2      5 [3,]    3      6 I can loop through all elements and do cbind manually. But I think there must be a simpler way that I don't know. Thank you. - adschai ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to collapse a list of 1 column matrix to a matrix?

 [[hidden email]] >I encounter a situation where I have a list whose element is a column >matrix. Says, >\$'1' >[,1] >1 >2 >3 >\$'2' >[,1] >4 >5 >6 >Is there fast way to collapse the list into a matrix like a cbind >operation in this case? Meaning, the result should be a matrix that >looks like: >      [,1]  [,2] >[1,]    1      4 >[2,]    2      5 >[3,]    3      6 >I can loop through all elements and do cbind manually. But I think >there must be a simpler way that I don't know. Thank you. The "do.call" function is the R equivalent of the "apply" from many other languages.  I guess that, in R, "apply" was already taken :-) For example: > a = list(x=matrix(1:3, 3, 1), y=matrix(4:6, 3, 1)) > a \$x      [,1] [1,]    1 [2,]    2 [3,]    3 \$y      [,1] [1,]    4 [2,]    5 [3,]    6 > do.call(cbind, a)      [,1] [,2] [1,]    1    4 [2,]    2    5 [3,]    3    6 -- François Pinard   http://pinard.progiciels-bpi.ca______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to collapse a list of 1 column matrix to a matrix?

 Ah, it works. I was using lapply and it doesn't seem to work. Thank you!----- Original Message -----From: François Pinard Date: Sunday, August 19, 2007 8:36 pmSubject: Re: [R] how to collapse a list of 1 column matrix to a matrix?To: [hidden email]: [hidden email]> [[hidden email]]> > >I encounter a situation where I have a list whose element is a > column > >matrix. Says,> > >\$'1'> >[,1]> >1> >2> >3> > >\$'2'> >[,1]> >4> >5> >6> > >Is there fast way to collapse the list into a matrix like a > cbind > >operation in this case? Meaning, the result should be a matrix > that > >looks like: > > >      [,1]  [,2]> >[1,]    1      4> >[2,]    2      5> >[3,]    3      6> > >I can loop through all elements and do cbind manually. But I > think > >there must be a simpler way that I don't know. Thank you.> > The "do.call" function is the R equivalent of the "apply" from > many > other languages.  I guess that, in R, "apply" was already taken > :-)> For example:> > > a = list(x=matrix(1:3, 3, 1), y=matrix(4:6, 3, 1))> > a> \$x>     [,1]> [1,]    1> [2,]    2> [3,]    3> > \$y>     [,1]> [1,]    4> [2,]    5> [3,]    6> > > do.call(cbind, a)>     [,1] [,2]> [1,]    1    4> [2,]    2    5> [3,]    3    6> > > -- > François Pinard   http://pinard.progiciels-bpi.ca>         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to collapse a list of 1 column matrix to a matrix?

 In reply to this post by adschai Try this: L <- list(`1` = matrix(1:4, 4), `2` = matrix(5:8, 4)) sapply(L, c) Note that the list component names are kept as column names in the result On 8/19/07, [hidden email] <[hidden email]> wrote: > Hi, > > I encounter a situation where I have a list whose element is a column matrix. Says, > > \$'1' > [,1] > 1 > 2 > 3 > > \$'2' > [,1] > 4 > 5 > 6 > > Is there fast way to collapse the list into a matrix like a cbind operation in this case? Meaning, the result should be a matrix that looks like: > >      [,1]  [,2] > [1,]    1      4 > [2,]    2      5 > [3,]    3      6 > > I can loop through all elements and do cbind manually. But I think there must be a simpler way that I don't know. Thank you. > > - adschai > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to collapse a list of 1 column matrix to a matrix?

 One more question. After I collapse everything into one matrix, I would like to find the index of column that holds minimum value for each row. I remember that there is a function like maxCols but I can't seem to find the same thing for minimum value. Any suggestion please? - adschai  ----- Original Message -----From: Gabor Grothendieck Date: Sunday, August 19, 2007 8:48 pmSubject: Re: [R] how to collapse a list of 1 column matrix to a matrix?To: "[hidden email]" Cc: [hidden email]> Try this:> > L <- list(`1` = matrix(1:4, 4), `2` = matrix(5:8, 4))> sapply(L, c)> > Note that the list component names are kept as column names in > the result> > > On 8/19/07, [hidden email]  wrote:> > Hi,> >> > I encounter a situation where I have a list whose element is a > column matrix. Says,> >> > \$'1'> > [,1]> > 1> > 2> > 3> >> > \$'2'> > [,1]> > 4> > 5> > 6> >> > Is there fast way to collapse the list into a matrix like a > cbind operation in this case? Meaning, the result should be a > matrix that looks like:> >> >      [,1]  [,2]> > [1,]    1      4> > [2,]    2      5> > [3,]    3      6> >> > I can loop through all elements and do cbind manually. But I > think there must be a simpler way that I don't know. Thank you.> >> > - adschai> >> > __!  ____________________________________________> > [hidden email] mailing list> > https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-> project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code.> >> ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: how to collapse a list of 1 column matrix to a matrix?

 > x = matrix(rnorm(9),3) > x             [,1]       [,2]        [,3] [1,]  1.29693748  0.2018959  0.02534092 [2,] -1.19468858 -1.2249229 -0.85948508 [3,] -0.09274508  0.4071841 -0.11481410 > apply(x,1,which.min) [1] 3 2 3 On 8/20/07, [hidden email] <[hidden email]> wrote: > > One more question. After I collapse everything into one matrix, I would > like to find the index of column that holds minimum value for each row. I > remember that there is a function like maxCols but I can't seem to find the > same thing for minimum value. Any suggestion please? > > - adschai > > > ----- Original Message -----From: Gabor Grothendieck Date: Sunday, August > 19, 2007 8:48 pmSubject: Re: [R] how to collapse a list of 1 column matrix > to a matrix?To: "[hidden email]" Cc: [hidden email]> Try > this:> > L <- list(`1` = matrix(1:4, 4), `2` = matrix(5:8, 4))> sapply(L, > c)> > Note that the list component names are kept as column names in > the > result> > > On 8/19/07, [hidden email]  wrote:> > Hi,> >> > I > encounter a situation where I have a list whose element is a > column > matrix. Says,> >> > \$'1'> > [,1]> > 1> > 2> > 3> >> > \$'2'> > [,1]> > 4> > > 5> > 6> >> > Is there fast way to collapse the list into a matrix like a > > cbind operation in this case? Meaning, the result should be a > matrix that > looks like:> >> >      [,1]  [,2]> > [1,]    1      4> > [2,]    2      5> > > [3,]    3      6> >> > I can loop through all elements and do cbind > manually. But I > think there must be a simpler way that I don't know. Thank > you.> >> > - adschai> >> > __! > ____________________________________________> > [hidden email] list> > > https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting > guide http://www.R-> project.org/posting-guide.html> > and provide > commented, minimal, self-contained, reproducible code.> >> > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.