在 Rcpp 中使用 R 的 "which" 函数,不返回值
Using R's "which" function in Rcpp, not returning values
我编写了一个 Rcpp 函数来调用 R 的 which 函数来检查是否相等。它编译得很好,但它似乎只返回向量中第一项的值:mywhich(samplevector, samplevector[1])
returns 一个值,mywhich(samplevector, samplevector[2])
returns numeric(0)
.
函数代码如下,只需要运行 on Numeric and Integer Vectors
#include <Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
SEXP mywhich(SEXP x, SEXP y) {
//For each supported type, turn it into the 'real' type and
//perform the operation. We can use TYPEOF to check the type.
switch(TYPEOF(x)){
case REALSXP: {
Environment base("package:base");
Function f("which");
NumericVector answer = f(as<NumericVector>(y) == as<NumericVector>(x));
return wrap(answer);
}
case INTSXP: {
Environment base("package:base");
Function f("which");
IntegerVector answer = f(as<IntegerVector>(y) == as<IntegerVector>(x));
return wrap(answer);
}
default: {
stop("Only integer and numeric vectors are supported");
}
}}
任何帮助将不胜感激
当您在 R 中执行 <long vector> == <short vector>
时,短向量将被回收以匹配长向量的长度。这不会发生在 Rcpp 中!在你的情况下,你想做 <vector> == <single element vector>
,这可以在 Rcpp 中用 <vector> == <double/int/...>
完成。这意味着您必须 select 来自单元素向量的 0 元素。在您的代码中:
#include <Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
SEXP mywhich(SEXP x, SEXP y) {
//For each supported type, turn it into the 'real' type and
//perform the operation. We can use TYPEOF to check the type.
switch(TYPEOF(x)){
case REALSXP: {
Environment base("package:base");
Function f("which");
NumericVector answer = f(as<NumericVector>(y)(0) == as<NumericVector>(x));
// ^^^
return wrap(answer);
}
case INTSXP: {
Environment base("package:base");
Function f("which");
IntegerVector answer = f(as<IntegerVector>(y)(0) == as<IntegerVector>(x));
// ^^^
return wrap(answer);
}
default: {
stop("Only integer and numeric vectors are supported");
}
}}
顺便说一句,我不相信您需要 which
从 R 中找到 LogicalVector
中的索引 true
.
我编写了一个 Rcpp 函数来调用 R 的 which 函数来检查是否相等。它编译得很好,但它似乎只返回向量中第一项的值:mywhich(samplevector, samplevector[1])
returns 一个值,mywhich(samplevector, samplevector[2])
returns numeric(0)
.
函数代码如下,只需要运行 on Numeric and Integer Vectors
#include <Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
SEXP mywhich(SEXP x, SEXP y) {
//For each supported type, turn it into the 'real' type and
//perform the operation. We can use TYPEOF to check the type.
switch(TYPEOF(x)){
case REALSXP: {
Environment base("package:base");
Function f("which");
NumericVector answer = f(as<NumericVector>(y) == as<NumericVector>(x));
return wrap(answer);
}
case INTSXP: {
Environment base("package:base");
Function f("which");
IntegerVector answer = f(as<IntegerVector>(y) == as<IntegerVector>(x));
return wrap(answer);
}
default: {
stop("Only integer and numeric vectors are supported");
}
}}
任何帮助将不胜感激
当您在 R 中执行 <long vector> == <short vector>
时,短向量将被回收以匹配长向量的长度。这不会发生在 Rcpp 中!在你的情况下,你想做 <vector> == <single element vector>
,这可以在 Rcpp 中用 <vector> == <double/int/...>
完成。这意味着您必须 select 来自单元素向量的 0 元素。在您的代码中:
#include <Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
SEXP mywhich(SEXP x, SEXP y) {
//For each supported type, turn it into the 'real' type and
//perform the operation. We can use TYPEOF to check the type.
switch(TYPEOF(x)){
case REALSXP: {
Environment base("package:base");
Function f("which");
NumericVector answer = f(as<NumericVector>(y)(0) == as<NumericVector>(x));
// ^^^
return wrap(answer);
}
case INTSXP: {
Environment base("package:base");
Function f("which");
IntegerVector answer = f(as<IntegerVector>(y)(0) == as<IntegerVector>(x));
// ^^^
return wrap(answer);
}
default: {
stop("Only integer and numeric vectors are supported");
}
}}
顺便说一句,我不相信您需要 which
从 R 中找到 LogicalVector
中的索引 true
.