简体   繁体   English

在Rcpp中 - 如何返回带有名称的向量

[英]In Rcpp - how to return a vector with names

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 该矩阵具有列名V1V6

Suppose that there is another matrix where I remove the column name: 假设有另一个矩阵我删除列名:

> testM2<-testM
> colnames(testM2)<-NULL

Then if I try colMeans on testM and testM2 R returns a numeric class in both cases, except in the first case the answer has colnames . 然后,如果我尝试colMeanstestMtestM2 [R返回一个numeric在这两种情况下类,除了在回答具有第一种情况colnames

> 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: 现在假设我在RCpp中编写了相同的函数,如下所示:

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 both testM and testM2 return numeric vectors, but the one for testM does not contain the colnames as it hasn't been set. 对于输出colMeansCppMt两个testMtestM2返回数字载体,而是一个testM不包含colnames ,因为它尚未设置。

Now, suppose I change the colMeansCppMt function to include attributes like this: 现在,假设我将colMeansCppMt函数更改为包含如下属性:

//[[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 doesn't contain the column names. testM的输出仍然是不包含列名的向量。

I also tried out.attr("names")=x.attr("colnames") and out.attr("colnames")=x.attr("colnames") . 我也试过out.attr("names")=x.attr("colnames")out.attr("colnames")=x.attr("colnames")

a). 一个)。 How can I check in RCpp where the colnames of a matrix (eg x in the example function above) has been set or not? 如何检查RCpp中colnames设置了矩阵的名称(例如上面示例函数中的x )?

b). B)。 How can I return a numeric vector in R with names in Rcpp? 如何在R中返回名称在Rcpp中的数字向量?

To set the landscape: 设置格局:

  1. Regular R vectors have an (optional) names attribute, 常规R向量具有(可选) names属性,
  2. data.frame s have a row.names attribute for rows, and names attribute for columns, and data.frame有行的row.names属性,列的names属性和
  3. matrix s have an (optional) dimnames attribute; matrix s有一个(可选的) dimnames属性; this attribute is a list containing 2 character vectors (rows then columns). 此属性是包含2个字符向量的list (行然后是列)。

So what you want is the column names of x attached to the 'names' attribute of out , so something like: 所以你想要的是附加到out的'names'属性的x的列名,所以类似于:

out.attr("names") = VECTOR_ELT(x.attr("dimnames"), 1);

would work. 会工作。

(I cannot recall if Rcpp has a nice API for getting / setting dimension names in arrays...) (我不记得Rcpp是否有一个很好的API来获取/设置数组中的维度名称...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM