[R] cut out single lines out of matrix

Petr PIKAL petr.pikal at precheza.cz
Wed Mar 17 10:48:04 CET 2010

```Hi

I am a bit curious why one shall do such a twisted construction. Accessing
list is basically the same as accessing corresponding matrix row, you only
need to remember drop=FALSE option.

Regards
Petr

r-help-bounces at r-project.org napsal dne 17.03.2010 00:58:54:

> Here is a way of creating a list of the matrices:
>
> > x <- matrix(1:(12*30), nrow=30)
> > # create a list of single row matrices
> > x.l <- lapply(seq(nrow(x)), function(a) x[a,, drop=FALSE])
> >
> > x.l
> [[1]]
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [1,]    1   31   61   91  121  151  181  211  241   271   301   331
> [[2]]
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [1,]    2   32   62   92  122  152  182  212  242   272   302   332
> [[3]]
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [1,]    3   33   63   93  123  153  183  213  243   273   303   333
>
> > # access the 5th element
> > x.l[[5]]
>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
> [1,]    5   35   65   95  125  155  185  215  245   275   305   335
> >
>
>
> On Tue, Mar 16, 2010 at 6:20 PM, Schmidt Martin
<m.schmidt at students.unibe.ch
> > wrote:
>
> > Hey dear users
> >
> >
> > I'm trying to kind of split my matrix which looks as follows:
> >
> >> dim(out)
> >>
> > [1] 30 12
> >
> > What I finally want is each line as it's own matrix which I can handle
then
> > separately.
> > Like, say:
> > out1<- [1,]
> > out2<-[2,]
> > ......
> > Would you do that with a for() loop or does exist an other appropriate
> > solution? I unfortunately couldn't find any solution!
> >
> > Thanks for help
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/
> posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> --
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.

```