[Rcpp-devel] Copy rownames and colnames from a NumericMatrix to another NumericMatrix

Dirk Eddelbuettel edd at debian.org
Tue Aug 11 13:19:46 CEST 2015


On 11 August 2015 at 12:30, Florian Plaza Oñate wrote:
| Hi everyone,
| 
| I am trying to copy the rownames and the colnames from a NumericMatrix 
| to another NumericMatrix by using Rcpp
| 
| Here is my code:
| 
| Rcpp::NumericMatrix copy(const Rcpp::NumericMatrix& m)
| {
|    Rcpp::NumericMatrix m2(num_genes, num_samples);
|    Rcpp::rownames(m2) = Rcpp::rownames(m);
|    Rcpp::colnames(m2) = Rcpp::colnames(m);

If you want a full copy, try 

   Rcpp::NumericMatrix m2 = Rcpp::clone(m);

as in

R> cppFunction("NumericMatrix foo(NumericMatrix m) { return Rcpp::clone(m); }");
R> m <- matrix(1:4,2,2,TRUE,list(c("a","b"), c("c", "d")))
R> foo(m)
  c d
a 1 2
b 3 4
R> 
 
|    return m2;
| }
| 
| It does change the default rownames and colnames of the NumericMatrix.
| Do you know how to do it?

Now I am confused. You do want to change them or not?

If you do _not_ use clone(), then m2 and m share the same underlying
pointer.  This has been explained before: we use proxy objects which are more
lightweight.  Distinct copies require clone().

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list