繁体   English   中英

如何检查 IntegerVector 是否在 Rcpp 中包含 NA 值?

[英]How do I check whether an IntegerVector contains NA values in Rcpp?

我希望检查提供给 C++ function 的 Rcpp IntegerVector 是否不包含NA值。

另一个答案之后,我写了以下内容:

IntegerMatrix stop_if_na(const IntegerVector I) {
  if (Rcpp::is_true(Rcpp::any(Rcpp::IntegerVector::is_na(I)))) {
    Rcpp::stop("`I` contains NA values");
  }

但我看到一个编译错误:

note: candidate: 'template<bool NA, class T> bool Rcpp::is_na(const Rcpp::sugar::SingleLogicalResult<NA, T>&)'
      38 |  inline bool is_na( const Rcpp::sugar::SingleLogicalResult<NA,T>& x){
         |              ^~~~~
note:   template argument deduction/substitution failed:
   <file>.cpp:22:48 note:   'const IntegerVector' {aka 'const Rcpp::Vector<13, Rcpp::PreserveStorage>'} is not derived from 'const Rcpp::sugar::SingleLogicalResult<NA, T>'
      22 |   if (Rcpp::is_true(Rcpp::any(Rcpp::is_na(I)))) {
         |                                            ^

当我删除IntegerVector:: (如 user20650 和 Dirk Eddelbuettel 建议的那样)我看到

error:  matching function for call to 'is_na(const IntegerVector&)
      20 |   if (Rcpp::is_true(Rcpp::any(Rcpp::is_na(I)
         |   

template argument deduction/substitution failed:
   <file>.cpp:20:48:note: 'const IntegerVector{aka 'const note: ndidate: 'template<bool NA, class T> bool Rcpp::is_na(const Rcpp::sugar::SingleLogicalResult<NA, T>&)
      38 |  inline bool is_nast Rcpp::sugar::SingleLogicalResult<NA,T>& x){
         |              ^~~~~
   note: template argument deduction/substitution failed:
<file>.cpp:20:48:note: 'const IntegerVector{aka 'const Rcpp::Vector<13, Rcpp::PreserveStorage> is not derived from 'const Rcpp::sugar::SingleLogicalResult<NA, T>
      20 |   if (Rcpp::is_true(Rcpp::any(Rcpp::is_na(I)
         |                                            ^

(As the C++ function is exported from a package, I can't rely on the calling R function to perform this test.)

sessionInfo() output

R Under development (unstable) (2022-07-19 r82607 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.utf8  LC_CTYPE=English_United Kingdom.utf8   
[3] LC_MONETARY=English_United Kingdom.utf8 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.utf8    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.5.4    compiler_4.3.0       microbenchmark_1.4.9 magrittr_2.0.3      
 [5] fastmap_1.1.0        cli_3.3.0            profvis_0.3.7        tools_4.3.0         
 [9] htmltools_0.5.3      rstudioapi_0.13      stringi_1.7.8        stringr_1.4.0       
[13] digest_0.6.29        rlang_1.0.4  

packageVersion("Rcpp") = 1.0.9

这是一个如此核心的功能,以至于我们从第一天开始就或多或少地拥有它,我有点惊讶你没有看到 2012 年 12 月 Hadley 的早期 Rcpp Gallery 帖子 处理缺失值

请注意,更有趣的功能是“糖”功能:

> Rcpp::cppFunction("LogicalVector foo(NumericVector x) {return is_finite(x);}")
> foo(c(1.0, Inf, -Inf, NA, NaN, 42))
[1]  TRUE FALSE FALSE FALSE FALSE  TRUE
> 

您可能需要研究一些相关的谓词。

如果您只是删除调用中的::IntegerVector ,您建议的 function 也可以工作:

> Rcpp::cppFunction("void stop_if_na(const IntegerVector IV) {
+ if (Rcpp::is_true(Rcpp::any(Rcpp::is_na(IV)))) {
+ Rcpp::stop(\"`I` contains NA values\");
+ }
+ }")
> stop_if_na(1:5)
> stop_if_na(c(1:5, NA, 7:9))
Error: `I` contains NA values
> 

为了便于重用,我的代码片段如下。

Rcpp::cppFunction("LogicalVector foo(NumericVector x) { return is_finite(x); }")
foo(c(1.0, Inf, -Inf, NA, NaN, 42))


Rcpp::cppFunction("void stop_if_na(const IntegerVector IV) {
  if (Rcpp::is_true(Rcpp::any(Rcpp::is_na(IV)))) {
    Rcpp::stop(\"`I` contains NA values\");
  }
}")
stop_if_na(1:5)
stop_if_na(c(1:5, NA, 7:9))

我的问题没有规定,结果很关键,我使用的是<Rcpp/Lightest> header 文件,该文件不包含is_na宏; <Rcpp/Lighter>可以。 因此,我在更新到以下内容时取得了成功:

#include <Rcpp/Lighter>  // NOT "Rcpp/Lightest"
using namespace Rcpp;

IntegerMatrix stop_if_na(const IntegerVector I) {
  if (Rcpp::is_true(Rcpp::any(Rcpp::IntegerVector::is_na(I)))) {
    Rcpp::stop("`I` contains NA values");
  }
  // Rest of function
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM