# [Rcpp-devel] Splitting a matrix by its rows into a list

Søren Højsgaard sorenh at math.aau.dk
Wed Feb 20 00:10:37 CET 2013

```Thanks for the suggestions!
Regards
Søren

From: rcpp-devel-bounces at lists.r-forge.r-project.org [mailto:rcpp-devel-bounces at lists.r-forge.r-project.org] On Behalf Of John Merrill
Sent: 19. februar 2013 02:46
To: Dirk Eddelbuettel
Cc: Rcpp-devel at lists.R-forge.R-project.org
Subject: Re: [Rcpp-devel] Splitting a matrix by its rows into a list

There is a sleazy hack which works, but wastes a fair amount of memory: take the transpose of the matrix in R, and use the fact that the columns of that transpose are the rows of the original.

On Mon, Feb 18, 2013 at 5:37 PM, Dirk Eddelbuettel <edd at debian.org<mailto:edd at debian.org>> wrote:

On 18 February 2013 at 17:02, Kevin Ushey wrote:
| One solution: X.row(ii) is actually generating a CharacterMatrix::Row ; ie, a
| view of that row in the CharacterMatrix, and I think you need to explicitly
| copy it to a CharacterVector and then assign that CharacterVector to the list.
|
| You could write something like:
|
| src <- '
|   CharacterMatrix X(XX_);
|   int Nx = X.nrow();
|   List ans;
|   for (int ii=0; ii<Nx; ii++) {
|     CharacterMatrix::Row view = X.row(ii);
|     CharacterVector tmp = no_init( X.ncol() );
|     for( int j=0; j < X.ncol(); j++ ) {
|       tmp[j] = view[j];
|     }
|     ans.push_back( tmp );
|   }
|   return(wrap(ans));
| '
|
| although it is a bit clunkier. I'm not sure if there's a more idiomatic method
| available.
I think all approaches have to be clunky as all our data structures store
column-wise, because that is what R does.

So I would probably start by element-wise copying, see if that works / if
that is fast enough and leave it at that.

Dirk
|
| FWIW, trying to copy and assign directly, e.g. 'CharacterVector tmp = X.row
| (ii);', doesn't seem to work, even though the same code works (and is in the
| quick-start vignette) for NumericVector.
|
| -Kevin
|
| On Mon, Feb 18, 2013 at 3:37 PM, Søren Højsgaard <sorenh at math.aau.dk<mailto:sorenh at math.aau.dk>> wrote:
|
|     I want to split a matrix by its rows into a list as:
|     > a<- matrix(letters[1:6],ncol=2)
|     > split(a,row(a))
|     \$`1`
|     [1] "a" "d"
|
|     \$`2`
|     [1] "b" "e"
|
|     \$`3`
|     [1] "c" "f"
|
|     I do as follows and get a strange result. Any suggestions?
|
|     Søren
|
|     ---------------
|
|     src <- '
|       CharacterMatrix X(XX_);
|       int Nx = X.nrow();
|       List ans;
|       for (int ii=0; ii<Nx; ii++){
|         ans.push_back(X.row(ii));
|       }
|       return(wrap(ans));
|     '
|     split_ <- cxxfunction(signature(XX_="matrix"), plugin="Rcpp", body=src)
|
|     split_(a)
|
|     [[1]]
|     [[1]][[1]]
|     <CHARSXP: "a">
|
|     [[1]][[2]]
|     <CHARSXP: "d">
|
|
|     [[2]]
|     [[2]][[1]]
|     <CHARSXP: "b">
|
|     [[2]][[2]]
|     <CHARSXP: "e">
|
|
|     [[3]]
|     [[3]][[1]]
|     <CHARSXP: "c">
|
|     [[3]][[2]]
|     <CHARSXP: "f">
|
|
|     _______________________________________________
|     Rcpp-devel mailing list
|     Rcpp-devel at lists.r-forge.r-project.org<mailto:Rcpp-devel at lists.r-forge.r-project.org>
|     https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
|
|
|
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org<mailto:Rcpp-devel at lists.r-forge.r-project.org>
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Dirk Eddelbuettel | edd at debian.org<mailto:edd at debian.org> | http://dirk.eddelbuettel.com
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel at lists.r-forge.r-project.org<mailto:Rcpp-devel at lists.r-forge.r-project.org>
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130219/420552f1/attachment.html>
```