gpt4 book ai didi

r - 如何在 Rcpp 中测试 NA 的日期时间?

转载 作者:行者123 更新时间:2023-12-02 17:43:29 26 4
gpt4 key购买 nike

使用 Rcpp我正在尝试在传递给 C++(类 DatetimeVector)的 POSIXct 向量中测试 NARcpp::is_na(.) 函数似乎适用于 NumericVectorCharcterVector... 但不适用于 DatetimeVector.

这是测试 NAC++ 代码,用于 NumericVectorCharacterVector 但如果您添加则无法编译DatetimeVector

#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")

最佳答案

我添加了(Rcpp 的修订版 4405)is_na 的实现对于 DateVectorDatetimeVector不需要转换为 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 中其他地方使用的代理设计。 DatetimeVector本质上是一个 std::vector<Datetime>和每一个 Datetime objects 是从 R 的底层对象的相应元素创建的。

更改DatetimeVector 的api 可能为时已晚和 DateVector并使它们基于代理,但也许还有像 POSIXct 这样的空间类(class)。

相比之下,让我们测量一下对 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

关于r - 如何在 Rcpp 中测试 NA 的日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17750889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com