2

使用Rcpp我试图在传递给 C++(类)NA的向量中进行测试。似乎该功能适用​​于, ... 但不适用于.POSIXctDatetimeVectorRcpp::is_na(.)NumericVectorCharcterVectorDatetimeVector

这是C++测试但如果添加则无法编译NA的代码NumericVectorCharacterVectorDatetimeVector

#include <Rcpp.h>
using namespace std;
using namespace Rcpp;

//[[Rcpp::export]]
List testNA(DataFrame df){

    const int N = df.nrows();

    //Test for NA in an IntegerVector
    IntegerVector intV = df["intV"];
    LogicalVector resInt = is_na(intV); 
    //Test for NA in an CharacterVector
    CharacterVector strV = df["strV"];
    LogicalVector resStr = is_na(strV);

    //Test for NA in an DatetimeVector
    DatetimeVector dtV = df["dtV"];
    LogicalVector resDT;
    //resDT = is_na(dtV); UNCOMMENT => DOES NOT COMPILE

    return(List::create(_["df"]=df, 
                        _["resInt"]=resInt,
                        _["resStr"]=resStr,
                        _["resDT"]=resDT));

}
/*** R
cat("testing for NA\n")
intV <- c(1,NA,2)
df <- data.frame(intV=intV, strV=as.character(intV), dtV=as.POSIXct(intV,origin='1970-01-01'))
str(df)
testNA(df)
*/

在 R 中

library("Rcpp")
sourceCpp("theCodeAbove.cpp")
4

2 回答 2

4

is_na我添加了for的(Rcpp 的 4405 版)实现DateVector并且DatetimeVector不需要强制转换 to NumericVector,这会创建一个我们实际上不需要的临时对象。

但是,我们并没有受到太大的性能影响,因为大部分时间都花在了构造DatetimeVector对象上。

#include <Rcpp.h>
using namespace Rcpp ;

// [[Rcpp::export]]
LogicalVector isna_cast( DatetimeVector d){
    // version with the cast
    return is_na( as<NumericVector>( d ) ) ;
}

// [[Rcpp::export]]
LogicalVector isna( DatetimeVector d){
    // without cast
    return is_na( d ) ;
}

// [[Rcpp::export]]
void do_nothing( DatetimeVector d){
    // just measuring the time it takes to 
    // create a DatetimeVector from an R object    
}

以此为基准microbenchmark

require(microbenchmark)

intV <- rep( c(1,NA,2), 100000 )
dtV  <- as.POSIXct(intV,origin='1970-01-01')

microbenchmark( 
    isna_cast( dtV ),
    isna( dtV ), 
    do_nothing( dtV )
)

#    Unit: milliseconds
#            expr      min       lq   median       uq      max neval
#  isna_cast(dtV) 67.03146 68.04593 68.71991 69.39960 96.46747   100
#       isna(dtV) 65.71262 66.43674 66.77992 67.16535 95.93567   100
# do_nothing(dtV) 57.15901 57.72670 58.08646 58.39948 58.97939   100

大约 85% 的时间用于创建DatetimeVector对象。这是因为DatetimeVectorDateVector类不使用我们在 Rcpp 中其他地方使用的代理设计。ADatetimeVector本质上是 astd::vector<Datetime>并且这些Datetime对象中的每一个都是从 R 的基础对象的相应元素创建的。

更改 api 并使它们基于代理可能为时已晚DatetimeVectorDateVector但也许有空间容纳诸如类之POSIXct类的东西。

相比之下,让我们测量一下什么都不做 a 所花费的时间NumericVector

// [[Rcpp::export]]
void do_nothing_NumericVector( NumericVector d){}

#    Unit: microseconds
#                     expr      min         lq     median        uq       max
#           isna_cast(dtV) 66985.21 68103.0060 68960.7880 69416.227 95724.385
#                isna(dtV) 65699.72 66544.9935 66893.5720 67213.064 95262.267
#          do_nothing(dtV) 57209.26 57865.1140 58306.8780 58630.236 69897.636
# do_nothing_numeric(intV)     4.22     9.6095    15.2425    15.511    33.978
于 2013-07-25T08:39:13.087 回答
2

编译器错误表明该方法(还没有?)可用于DateTimeVectors:

test.cpp:18:13: error: no matching function for call to 'is_na'

一个简单的解决方法:

resDT = is_na( as<NumericVector>(dtV) ); // As per Dirk's suggestion
于 2013-07-19T16:45:45.940 回答