从 Rcpp 返回 NA [英] Return NA from Rcpp

查看:52
本文介绍了从 Rcpp 返回 NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Rcpp 返回 NA.我不明白为什么 get_na() 在这里不起作用,正如这个 post 所建议的那样?

<代码>>Rcpp::cppFunction('NumericVector temp1() {返回 NumericVector::get_na();}')>温度 1()temp1() 中的错误:不允许负长度向量

如果我尝试 create() 它会起作用.

<代码>>Rcpp::cppFunction('NumericVector temp2() {返回 NumericVector::create(NA_REAL);}')>温度2()不适用

解决方案

您需要执行以下操作:

Rcpp::cppFunction('NumericVector temp1() {数字向量 y(1);y[0] = NumericVector::get_na();返回 y;}')温度 1()#R>[1] 不适用

如果你想使用 NumericVector::get_na().请注意,this member/a> 只是返回 NA_REAL 这就是为什么你可能会在 NumericVector 的构造函数中得到错误:

Rcpp::cppFunction('NumericVector temp1() {返回 NumericVector::get_na();}')

您同样可以按照您的建议使用 NumericVector::create.你也可以这样做:

Rcpp::cppFunction('NumericVector temp2() {返回 NumericVector(1, NA_REAL);}')

Rcpp::cppFunction('double temp3() {返回 NA_REAL;}')

<块引用>

从 Rcpp 返回 NA

如果您正在处理其他类型的向量,那么 NumericVector 那么 get_na 函数可能非常有用.这是一个示例,我们返回 NA,但根据输入具有不同的类型.

Rcpp::sourceCpp(code = '#include "Rcpp.h";使用命名空间 Rcpp;模板矢量 Tget_na_genric(){return Vector(1, Vector::get_na());}//[[Rcpp::export]]SEXP get_nan_vec(SEXP x) {开关(TYPEOF(x)){case INTSXP : return get_na_genric();案例LGLSXP:返回get_na_genric();案例 REALSXP:返回 get_na_genric();案例STRSXP:返回get_na_genric();case VECSXP : 返回 get_na_genric();停止(类型未实现");}返回 get_na_genric();}')for(x in list(integer(), logical(), numeric(), character(),列表())){out <- get_nan_vec(x)猫(得到:\n")打印)cat("with type ", typeof(out), "\n")}#R>得到了:#R>[1] 不适用#R>带整数类型#R>得到了:#R>[1] 不适用#R>逻辑类型#R>得到了:#R>[1] 不适用#R>带类型双#R>得到了:#R>[1] 不适用#R>带类型字符#R>得到了:#R>[[1]]#R>空值#R>#R>带类型列表

I am trying to return NA via Rcpp. I do not understand why get_na() doesn't work here as suggested in this post?

> Rcpp::cppFunction('NumericVector temp1() {
   return NumericVector::get_na();
 }')
> temp1()
Error in temp1() : negative length vectors are not allowed

If I try create() it works.

> Rcpp::cppFunction('NumericVector temp2() {
    return NumericVector::create(NA_REAL);
  }')
> temp2()
  NA

解决方案

You need to do something like:

Rcpp::cppFunction('NumericVector temp1() {
  NumericVector y(1);
  y[0] = NumericVector::get_na();
  return y;
}')

temp1()
#R> [1] NA

if you want to use NumericVector::get_na(). Note that this member function simply returns NA_REAL which is why you presuambly get the error with NumericVector's constructor with:

Rcpp::cppFunction('NumericVector temp1() {
   return NumericVector::get_na();
 }')

You can equally well use NumericVector::create as you suggest. You can also do:

Rcpp::cppFunction('NumericVector temp2() {
  return NumericVector(1, NA_REAL);
}')

or

Rcpp::cppFunction('double temp3() {
  return NA_REAL;
}')

Return NA from Rcpp

If you are dealing with other types of vectors then the NumericVector then the get_na function can be very useful. Here is an example where we return NA but with different types depending on the input.

Rcpp::sourceCpp(code = '
  #include "Rcpp.h"
  using namespace Rcpp;
  
  template<int T>
  Vector<T> get_na_genric(){
    return Vector<T>(1, Vector<T>::get_na());
  }
  
  // [[Rcpp::export]]
  SEXP get_nan_vec(SEXP x) {
    switch (TYPEOF(x)) {
      case INTSXP : return get_na_genric<INTSXP >();
      case LGLSXP : return get_na_genric<LGLSXP >();
      case REALSXP: return get_na_genric<REALSXP>();
      case STRSXP : return get_na_genric<STRSXP >();
      case VECSXP : return get_na_genric<VECSXP >();
      stop("type not implemented");
    }
    
    return get_na_genric<REALSXP>();
  }')

for(x in list(integer(), logical(), numeric(), character(), 
              list())){
  out <- get_nan_vec(x)
  cat("got:\n")
  print(out)
  cat("with type ", typeof(out), "\n")
}
#R> got:
#R> [1] NA
#R> with type  integer 
#R> got:
#R> [1] NA
#R> with type  logical 
#R> got:
#R> [1] NA
#R> with type  double 
#R> got:
#R> [1] NA
#R> with type  character 
#R> got:
#R> [[1]]
#R> NULL
#R> 
#R> with type  list 

这篇关于从 Rcpp 返回 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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