在 Rcpp 中 - 如何返回带有名称的向量 [英] In Rcpp - how to return a vector with names

查看:30
本文介绍了在 Rcpp 中 - 如何返回带有名称的向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下矩阵:

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
")))

这个矩阵有列名 V1V6

This matrix has column names V1 to V6

假设有另一个矩阵,我删除了列名:

Suppose that there is another matrix where I remove the column name:

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

然后,如果我在 testMtestM2 上尝试 colMeans 在这两种情况下都返回一个 numeric 类,除了在第一种情况下,答案有 colnames.

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.

> 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

现在假设我用 RCpp 编写了相同的函数,如下所示:

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;
}

testMtestM2colMeansCppMt 的输出返回数字向量,但 testM 的输出不包含 colnames,因为它尚未设置.

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 函数以包含如下属性:

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;
}

testM 的输出仍然是一个不包含列名的向量.

The output for testM is still a vector that doesn't contain the column names.

我也试过 out.attr("names")=x.attr("colnames")out.attr("colnames")=x.attr("colnames")).

a).如何在 RCpp 中检查矩阵的 colnames(例如上面示例函数中的 x)是否已设置?

a). How can I check in RCpp where the colnames of a matrix (e.g. x in the example function above) has been set or not?

b).如何在 Rcpp 中返回带有名称的 R 中的数字向量?

b). How can I return a numeric vector in R with names in Rcpp?

推荐答案

设置横向:

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

所以你想要的是 x 的列名附加到 out 的 'names' 属性,所以像:

So what you want is the column names of x attached to the 'names' attribute of out, so something like:

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

会起作用.

(我不记得 Rcpp 是否有一个很好的 API 来获取/设置数组中的维度名称...)

(I cannot recall if Rcpp has a nice API for getting / setting dimension names in arrays...)

这篇关于在 Rcpp 中 - 如何返回带有名称的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆