I want to set a matrix's column names only using Rcpp, but leave the row names unchanged. So far as I can tell, the dimnames
attribute only sets both. For example:
Here's a minimal example of what I want to do, but just in Rcpp instead of R:
my.mat <- diag(3)
colnames( my.mat ) <- c( "A", "B", "C")
my.mat
A B C
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
Is there a way to do this?
Newer versions of Rcpp
provides rownames()
and colnames()
which function as their R
counterparts do:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix test(NumericMatrix x)
{
rownames(x) = CharacterVector::create("a", "b", "c");
colnames(x) = CharacterVector::create("A", "B", "C");
return x;
}
/*** R
test(matrix(1:9, nrow = 3))
*/
gives me
> test(matrix(1:9, nrow = 3))
A B C
a 1 4 7
b 2 5 8
c 3 6 9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With