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

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

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

adschai
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.
Reply | Threaded
Open this post in threaded view
|

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

François Pinard
[[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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

adschai
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Gabor Grothendieck
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

adschai
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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

Jared O'Connell
> 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-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

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

François Pinard
In reply to this post by adschai
[[hidden email]]
> 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?

Here is a possible avenue:

> z <- matrix(sample(1:25), 5)
> z
     [,1] [,2] [,3] [,4] [,5]
[1,]   16    2    9   24    7
[2,]   21   19   22   23   18
[3,]   12    3    5   13   15
[4,]   20    4   25   11   10
[5,]   17    1    8   14    6
> apply(z, 2, which.min)
[1] 3 5 3 4 5

I would presume (yet I did not recently check) that "do.call",
"which.min", and a flurry of other useful functions, are introduced in
various R tutorials.  If you plan to use R seriously, it might be worth
scrutinizing a few of those.

                                        Keep happy!

--
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
[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.
Reply | Threaded
Open this post in threaded view
|

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

adschai
In reply to this post by Jared O'Connell
Thank you. I found the same answer.

----- Original Message -----From: Jared O'Connell Date: Sunday, August 19, 2007 9:56 pmSubject: Re: [R] how to collapse a list of 1 column matrix to a matrix?To: "[hidden email]" Cc: [hidden email]> > 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]  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: r-> [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> >> > __!> > ____________________________________________> > R-> [hidden email] list> >> > https://stat.ethz!
 .ch/mailman/listinfo/r-help> > PLEASE do read > the posting> > guide h
ttp://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.> >>

______________________________________________
[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.