简体   繁体   English

使用包含索引的IntegerVector子集Rcpp :: DateVector? 运算符[]重载

[英]Subsetting Rcpp::DateVector using IntegerVector that contains indices? Operator [] overload

Rcpp Users, Rcpp用户,

I am new to Rcpp. 我是Rcpp的新手。 I would like to subset a DateVector using an IntegerVector that has the integer indices. 我想使用具有整数索引的IntegerVector子集DateVector However, a direct use of [] leads to error message: 但是,直接使用[]会导致错误消息:

no viable overloaded operator[] for type 'Rcpp:DateVector'.

Here is an illustrate example: 这是一个说明性的例子:

In cpp file - dv.cpp: 在cpp文件-dv.cpp中:

#include < Rcpp.h >

using namespace Rcpp;

// [[Rcpp::export]]
DataFrame subsetDateVector1(DataFrame df) {

  DateVector    dv = df["dv"];
  IntegerVector iv = df["iv"];

  for (int i = 0; i < dv.size(); i++) {
    dv[i] = dv[i] + 7;
  }

  IntegerVector idx = seq_len(2) - 1;

  // DateVector dv1 = dv[idx]; 
  // Q: cannot subset DateVector using IntegerVector? 
  // Any Reason? 
  // How to work around, efficiently?

  // my work around
  Function formatDate("format.Date");
  CharacterVector dvc(dv.size());
  for (int i = 0; i < dv.size(); i++) {
    dvc[i] = as<std::string>(formatDate(wrap(dv[i])));
  }
  CharacterVector dv1 = dvc[idx];

  IntegerVector iv1 = iv[idx];

  return (DataFrame::create(_["dv"] = dv1, _["iv"] = iv1));

}

In R file - dv.r: 在R文件-dv.r中:

# subsetting Rcpp::DateVector [Ex]

source("dv.cpp")

dv = Sys.Date() + -2:2

iv = 1:5

df = data.frame(dv = dv, iv = iv)

> df
          dv iv
1 2015-12-06  1
2 2015-12-07  2
3 2015-12-08  3
4 2015-12-09  4
5 2015-12-10  5

> subsetDateVector1(df)
          dv iv
1 2015-12-13  1
2 2015-12-14  2

I felt that the work around in subsetDateVector1 is not very efficient. 我感觉到subsetDateVector1中的工作效率不是很高。

Any reason that DateVector dv1 = dv[idx]; DateVector dv1 = dv[idx];任何原因DateVector dv1 = dv[idx]; not being implemented? 没有执行?

Any efficient way to take subset on DateVector based on indices? 有什么有效的方法可以基于索引在DateVector上获取子集?

Thanks in advances. 在此先感谢。

DateVector and DatetimeVector are a little apart in Rcpp and survive from the very earliest days (when they were accessed from RQuantLib). DateVector和DatetimeVector在Rcpp中相距甚远,并且从最早的日子(从RQuantLib访问它们)开始就可以生存。 They should probably be deprecated and replaced by something better, but I have not yet gotten to that. 他们可能应该被弃用,而应该用更好的东西代替,但是我还没有做到这一点。

You can just work with the numeric Date (or Datetime) values in a NumericVector which also gives you the subsetting you want. 您可以只在NumericVector使用数字 Date(或Datetime)值,这也会为您提供所需的子集。 The Rblpapi package has some examples---in essence I just place the POSIXct equivalent of fractional seconds since epoch into a NumericVector and then reclass it later to POSIXct . Rblpapi包中有一些示例-本质上,我只是将POSIXct等于从纪元以来的秒数放入NumericVector ,然后稍后将其重分类为POSIXct

Lastly, if I may, please don't do 最后,如果可以的话,请不要

  Function formatDate("format.Date");
  CharacterVector dvc(dv.size());
  for (int i = 0; i < dv.size(); i++) {
    dvc[i] = as<std::string>(formatDate(wrap(dv[i])));
  }

You can perfectly well format dates and times in C++. 您可以用C ++很好地格式化日期和时间。 It is worth learning that. 值得学习。

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

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