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

Romain Francois romain at r-enthusiasts.com
Tue Feb 19 16:40:20 CET 2013


This was a bug, and is fixed in rev 4262.

Romain

Le 19/02/13 00:37, Søren Højsgaard a écrit :
> 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?
>
> Thanks in advance!
> 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">


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://romainfrancois.blog.free.fr
|- http://bit.ly/14LJhmm : bibtex 0.3-5
`- http://bit.ly/RE6sYH  : OOP with Rcpp modules



More information about the Rcpp-devel mailing list