通过 RCpp 返回 NA [英] return NA via RCpp

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

问题描述

这里的新手 RCpp 问题:如何使 NumericVector 返回 NA 到 R?例如,假设我有一个 RCpp 代码,它将 NA 分配给向量的第一个元素.

Newbie RCpp question here: how can I make a NumericVector return NA to R? For example, suppose I have a RCpp code that assigns NA to the first element of a vector.

// [[RCpp::export]]
NumericVector myFunc(NumericVector x) {
   NumericVector y=clone(x);
   y[0]=NA; // <----- what's the right expression here?
   return y;
}

推荐答案

获得给定向量类型(NumericVector, IntegerVector, ...) 是使用静态的 get_na 方法.类似的东西:

The canonical way to get the correct NA for a given vector type (NumericVector, IntegerVector, ...) is to use the static get_na method. Something like:

y[0] = NumericVector::get_na() ;

FWIW,您的代码与 Rcpp11 一样工作,它知道如何从静态 Na_Proxy 实例 NA 转换为正确的缺失值对于目标类型.

FWIW, your code works as is with Rcpp11, which knows how to convert from the static Na_Proxy instance NA into the correct missing value for the target type.

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

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