<div dir="ltr">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.</div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Mon, Feb 18, 2013 at 5:37 PM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im"><br>
On 18 February 2013 at 17:02, Kevin Ushey wrote:<br>
| One solution: X.row(ii) is actually generating a CharacterMatrix::Row ; ie, a<br>
| view of that row in the CharacterMatrix, and I think you need to explicitly<br>
| copy it to a CharacterVector and then assign that CharacterVector to the list.<br>
|<br>
| You could write something like:<br>
|<br>
| src <- '<br>
|   CharacterMatrix X(XX_);<br>
|   int Nx = X.nrow();<br>
|   List ans;<br>
|   for (int ii=0; ii<Nx; ii++) {<br>
|     CharacterMatrix::Row view = X.row(ii);<br>
|     CharacterVector tmp = no_init( X.ncol() );<br>
|     for( int j=0; j < X.ncol(); j++ ) {<br>
|       tmp[j] = view[j];<br>
|     }<br>
|     ans.push_back( tmp );<br>
|   }<br>
|   return(wrap(ans));<br>
| ' <br>
|<br>
| although it is a bit clunkier. I'm not sure if there's a more idiomatic method<br>
| available.<br>
<br>
</div>I think all approaches have to be clunky as all our data structures store<br>
column-wise, because that is what R does.<br>
<br>
So I would probably start by element-wise copying, see if that works / if<br>
that is fast enough and leave it at that.<br>
<br>
Dirk<br>
<div><div class="h5">|<br>
| FWIW, trying to copy and assign directly, e.g. 'CharacterVector tmp = X.row<br>
| (ii);', doesn't seem to work, even though the same code works (and is in the<br>
| quick-start vignette) for NumericVector.<br>
|<br>
| -Kevin<br>
|<br>
| On Mon, Feb 18, 2013 at 3:37 PM, Søren Højsgaard <<a href="mailto:sorenh@math.aau.dk">sorenh@math.aau.dk</a>> wrote:<br>
|<br>
|     I want to split a matrix by its rows into a list as:<br>
|     > a<- matrix(letters[1:6],ncol=2)<br>
|     > split(a,row(a))<br>
|     $`1`<br>
|     [1] "a" "d"<br>
|<br>
|     $`2`<br>
|     [1] "b" "e"<br>
|<br>
|     $`3`<br>
|     [1] "c" "f"<br>
|<br>
|     I do as follows and get a strange result. Any suggestions?<br>
|<br>
|     Thanks in advance!<br>
|     Søren<br>
|<br>
|     ---------------<br>
|<br>
|     src <- '<br>
|       CharacterMatrix X(XX_);<br>
|       int Nx = X.nrow();<br>
|       List ans;<br>
|       for (int ii=0; ii<Nx; ii++){<br>
|         ans.push_back(X.row(ii));<br>
|       }<br>
|       return(wrap(ans));<br>
|     '<br>
|     split_ <- cxxfunction(signature(XX_="matrix"), plugin="Rcpp", body=src)<br>
|<br>
|     split_(a)<br>
|<br>
|     [[1]]<br>
|     [[1]][[1]]<br>
|     <CHARSXP: "a"><br>
|<br>
|     [[1]][[2]]<br>
|     <CHARSXP: "d"><br>
|<br>
|<br>
|     [[2]]<br>
|     [[2]][[1]]<br>
|     <CHARSXP: "b"><br>
|<br>
|     [[2]][[2]]<br>
|     <CHARSXP: "e"><br>
|<br>
|<br>
|     [[3]]<br>
|     [[3]][[1]]<br>
|     <CHARSXP: "c"><br>
|<br>
|     [[3]][[2]]<br>
|     <CHARSXP: "f"><br>
|<br>
|<br>
|     _______________________________________________<br>
|     Rcpp-devel mailing list<br>
|     <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
|     <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
|<br>
|<br>
|<br>
</div></div>| ----------------------------------------------------------------------<br>
<div class="im HOEnZb">| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div><span class="HOEnZb"><font color="#888888">--<br>
Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</div></div></blockquote></div><br></div>