Suppose I have the following matrix:
testM <- as.matrix(read.table(textConnection(" 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 5 4 1 3 2 2 1 ")))
This matrix has column names V1 to V6
Suppose there is another matrix where I delete the column name:
> testM2<-testM > colnames(testM2)<-NULL
Then, if I try colMeans on testM and testM2 R returns the numeric class in both cases, except for the first response of the colnames answer.
> colMeans(testM) V1 V2 V3 V4 V5 V6 2.166667 2.333333 2.833333 2.500000 2.666667 2.666667 > colMeans(testM2) [1] 2.166667 2.333333 2.833333 2.500000 2.666667 2.666667
Now suppose I have the same function written in RCpp as follows:
double do_mean(NumericVector x) { return mean(na_omit(x)); } //[[Rcpp::export]] NumericVector colMeansCppMt(NumericMatrix& x) { int nCols=x.ncol(); NumericVector out=no_init(nCols); for (int i=0;i<nCols;i++) { NumericMatrix::Column tmp=x(_,i); out[i]=do_mean(tmp); } return out; }
The output for colMeansCppMt for testM and testM2 returns numeric vectors, but for testM does not contain colnames since it was not set.
Now suppose I change the colMeansCppMt function to include attributes like this:
//[[Rcpp::export]] NumericVector colMeansCppMt(NumericMatrix& x) { int nCols=x.ncol(); NumericVector out=no_init(nCols); for (int i=0;i<nCols;i++) { NumericMatrix::Column tmp=x(_,i); out[i]=do_mean(tmp); } out.attr("names")=x.attr("names"); return out; }
The output for testM is still a vector that does not contain column names.
I also tried out.attr("names")=x.attr("colnames") and out.attr("colnames")=x.attr("colnames") .
a). How can I check RCpp where the colnames matrix (for example, x in the example function above) were set or not?
b) How can I return a numeric vector in R with names in Rcpp?