[Rcpp-devel] appearance of matrix returned from Rcpp
Jack Wasey
jack at jackwasey.com
Sat Feb 14 12:59:07 CET 2015
Hello,
I am trying to return a matrix derived from a std::vector<bool> with row
names.
I followed the advice of Kevin and Dirk I read at
https://stackoverflow.com/questions/19864226/convert-stdvector-to-rcpp-matrix
Strangely, the matrix is correct, but when I print it in R, it prints
differently to defining it in R:
#include <Rcpp.h>
#include <string>
// [[Rcpp::export]]
SEXP make_mat(std::vector<bool> x, std::vector<std::string> n, int nrow,
int ncol) {
Rcpp::LogicalVector y = Rcpp::wrap(x);
y.attr("dim") = Rcpp::Dimension(nrow, ncol);
y.attr(".Dimnames") = Rcpp::List::create(n, R_NilValue);
return y;
}
rcpp_mat <- make_mat(c(T,T,F,F), c("a", "b"), 2, 2)
rcpp_mat
[,1] [,2]
[1,] TRUE FALSE
[2,] TRUE FALSE
attr(,".Dimnames")
attr(,".Dimnames")[[1]]
[1] "a" "b"
attr(,".Dimnames")[[2]]
NULL
rmat
[,1] [,2]
a TRUE FALSE
b TRUE FALSE
> typeof(rmat)
[1] "logical"
> typeof(rcpp_mat)
[1] "logical"
> class(rmat)
[1] "matrix"
> class(rcpp_mat)
[1] "matrix"
identical(rmat, rcpp_mat)
[1] FALSE
What's going on here, please?
Ideally, I'd rather construct a LogicalMatrix directly from
std::vector<bool>, but I don't think you have that constructor.
Thanks, Jack.
More information about the Rcpp-devel
mailing list