[Rcpp-devel] Bug or intended behaviour?
Søren Højsgaard
sorenh at math.aau.dk
Tue Mar 18 09:35:42 CET 2014
Ooooops; just realised that I did not allocate memory inside the loop; now it works:
// [[Rcpp::export]]
List do_colmat2list_str(SEXP XX_)
{
CharacterMatrix X(XX_);
int nc=X.ncol(), nr=X.nrow(), k;
List out(nc);
for (int j=0; j<nc; j++){
CharacterVector v(nr);
for (k=0; k<nr; ++k){
v[ k ] = X( k, j);
}
out(j) = v ;
}
return out;
}
Sorry for the trouble...
Søren
-----Original Message-----
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 Søren Højsgaard
Sent: 18. marts 2014 07:59
To: rcpp-devel at lists.r-forge.r-project.org (rcpp-devel at r-forge.wu-wien.ac.at)
Subject: [Rcpp-devel] Bug or intended behaviour?
Dear all
I want to split a matrix with characters into a list "by columns", for example:
m2 <- matrix(letters[1:16], 4); m2
[,1] [,2] [,3] [,4]
[1,] "a" "e" "i" "m"
[2,] "b" "f" "j" "n"
[3,] "c" "g" "k" "o"
[4,] "d" "h" "l" "p"
I do:
// [[Rcpp::export]]
List do_colmat2list_str(SEXP XX_)
{
CharacterMatrix X(XX_);
int nc=X.ncol(), nr=X.nrow(), k;
CharacterVector v(nr);
List out(nc);
for (int j=0; j<nc; j++){
for (k=0; k<nr; ++k){
v[ k ] = X( k, j);
}
Rprintf("current v:"); Rf_PrintValue( v );
out(j) = v;
}
return out;
}
And I get the following result (which surprises me):
do_colmat2list_str(m2)
current v:[1] "a" "b" "c" "d"
current v:[1] "e" "f" "g" "h"
current v:[1] "i" "j" "k" "l"
current v:[1] "m" "n" "o" "p"
[[1]]
[1] "m" "n" "o" "p"
[[2]]
[1] "m" "n" "o" "p"
[[3]]
[1] "m" "n" "o" "p"
[[4]]
[1] "m" "n" "o" "p"
If I clone v before "sticking it into" out I get the result I expect:
// [[Rcpp::export]]
List do_colmat2list_str(SEXP XX_)
{
CharacterMatrix X(XX_);
int nc=X.ncol(), nr=X.nrow(), k;
CharacterVector v(nr);
List out(nc);
for (int j=0; j<nc; j++){
for (k=0; k<nr; ++k){
v[ k ] = X( k, j);
}
Rprintf("current v:"); Rf_PrintValue( v );
out(j) = clone( v ); // here we clone...........
}
return out;
}
do_colmat2list_str(m2)
current v:[1] "a" "b" "c" "d"
current v:[1] "e" "f" "g" "h"
current v:[1] "i" "j" "k" "l"
current v:[1] "m" "n" "o" "p"
[[1]]
[1] "a" "b" "c" "d"
[[2]]
[1] "e" "f" "g" "h"
[[3]]
[1] "i" "j" "k" "l"
[[4]]
[1] "m" "n" "o" "p"
Is this how things are supposed to work, or is there a memory issue somewhere??
Cheers
Søren
sessionInfo()
R version 3.0.3 Patched (2014-03-06 r65198)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=Danish_Denmark.1252 LC_CTYPE=Danish_Denmark.1252 LC_MONETARY=Danish_Denmark.1252
[4] LC_NUMERIC=C LC_TIME=Danish_Denmark.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Rcpp_0.11.1 devtools_1.4.1 shTools_1.0 markdown_0.6.4 knitr_1.5
loaded via a namespace (and not attached):
[1] compiler_3.0.3 digest_0.6.4 evaluate_0.5.1 formatR_0.10 httr_0.2 memoise_0.1 parallel_3.0.3
[8] RCurl_1.95-4.1 stringr_0.6.2 tools_3.0.3 whisker_0.3-2
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
More information about the Rcpp-devel
mailing list