[英]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
该矩阵具有列名
V1
至V6
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
. 然后,如果我尝试
colMeans
上testM
和testM2
[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
两个testM
和testM2
返回数字载体,而是一个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: 设置格局:
names
attribute, names
属性, data.frame
s have a row.names
attribute for rows, and names
attribute for columns, and data.frame
有行的row.names
属性,列的names
属性和 matrix
s have an (optional) dimnames
attribute; matrix
s有一个(可选的) dimnames
属性; this attribute is a list
containing 2 character vectors (rows then columns). 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.