如何快速检查大型 XTS 对象中是否存在日期(或时间)?
How to quickly check if a date (or time) exists in a large XTS object?
我在 R 中有一个名为 Data
的非常大的 xts 对象,每天有 10 或 100 行和数百万行。
这是我当前的代码:
Data #my xts data set.
myDate <- "2018-02-15"
if(nrow(Data[as.character(myDate)]) > 0)
#Run code.
问题是 1 天的子集有数百万行并且需要大量时间,尤其是当我检查许多日期时。
有没有一种方法可以检查日期是否存在或只获取第一次出现的日期,这样我就不会浪费时间提取大量数据?
我想在本机 R 中执行此操作,但最受欢迎的是 Rcpp 解决方案。
谢谢。
编辑:
根据 ngm 的回答,我能够完成一个 Rcpp 解决方案。
// [[Rcpp::export]]
bool doesDateExist(const Rcpp::NumericMatrix& Data, double startDate, double maxDiff = 86400)
{
double endDate = startDate + maxDiff;
NumericVector time = Data.attr("index");
for(int ii = 0; ii < Data.nrow();ii++)
{
if(time(ii) >= startDate)
{
if(time(ii) < endDate)
return true;
else
return false;
}
}
return false;
}
为了使用它,我有:
myDate <-as.POSIXct("2018-02-15", tz = indexTZ(Data))
if(doesDateExist(Data, myDate, 86400))
#Run code.
as.POSIXct是我遗失的一块,我总是忘记它。
编辑:为最大时差向 rcpp 代码添加了可选字段。一天 86400 秒,小时 3600 分钟,60 分钟等等。
直接访问 xts
对象的索引会更快。
您似乎在尝试查看特定日期是否包含在 xts
对象索引的日期部分中。这对我有用:
library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
myDate <- as.POSIXct("2007-01-04")
myDate %in% as.POSIXct(index(sample.xts), format="%Y-%m-%d")
这里是 counter-example 使用 %in%
:
R> x <- xts(1:20,
+ order.by=Sys.time() + cumsum(sample(1:10, 20, TRUE)*1e-6))
R> x
[,1]
2018-04-05 12:09:12.818800 1
2018-04-05 12:09:12.818805 2
2018-04-05 12:09:12.818809 3
2018-04-05 12:09:12.818810 4
2018-04-05 12:09:12.818819 5
2018-04-05 12:09:12.818827 6
2018-04-05 12:09:12.818832 7
2018-04-05 12:09:12.818837 8
2018-04-05 12:09:12.818843 9
2018-04-05 12:09:12.818847 10
2018-04-05 12:09:12.818848 11
2018-04-05 12:09:12.818849 12
2018-04-05 12:09:12.818858 13
2018-04-05 12:09:12.818867 14
2018-04-05 12:09:12.818872 15
2018-04-05 12:09:12.818877 16
2018-04-05 12:09:12.818881 17
2018-04-05 12:09:12.818888 18
2018-04-05 12:09:12.818889 19
2018-04-05 12:09:12.818890 20
R> reftime <- anytime::anytime("2018-04-05 12:09:12.818832")
R> reftime
[1] "2018-04-05 12:09:12.818831 CDT"
R> reftime %in% index(x)
[1] FALSE
R>
我从字面上复制并粘贴了一个随机条目(值 7)并 re-parsed 它。然而 %in%
失败了。
关注 R FAQ 7.31 你 可以 做类似
的事情
R> which( abs(reftime - index(x)) < 1e-6)
[1] 7
R>
R> x[which( abs(reftime - index(x)) < 1e-6)]
[,1]
2018-04-05 12:09:12.818832 7
R>
我在 R 中有一个名为 Data
的非常大的 xts 对象,每天有 10 或 100 行和数百万行。
这是我当前的代码:
Data #my xts data set.
myDate <- "2018-02-15"
if(nrow(Data[as.character(myDate)]) > 0)
#Run code.
问题是 1 天的子集有数百万行并且需要大量时间,尤其是当我检查许多日期时。
有没有一种方法可以检查日期是否存在或只获取第一次出现的日期,这样我就不会浪费时间提取大量数据?
我想在本机 R 中执行此操作,但最受欢迎的是 Rcpp 解决方案。
谢谢。
编辑: 根据 ngm 的回答,我能够完成一个 Rcpp 解决方案。
// [[Rcpp::export]]
bool doesDateExist(const Rcpp::NumericMatrix& Data, double startDate, double maxDiff = 86400)
{
double endDate = startDate + maxDiff;
NumericVector time = Data.attr("index");
for(int ii = 0; ii < Data.nrow();ii++)
{
if(time(ii) >= startDate)
{
if(time(ii) < endDate)
return true;
else
return false;
}
}
return false;
}
为了使用它,我有:
myDate <-as.POSIXct("2018-02-15", tz = indexTZ(Data))
if(doesDateExist(Data, myDate, 86400))
#Run code.
as.POSIXct是我遗失的一块,我总是忘记它。
编辑:为最大时差向 rcpp 代码添加了可选字段。一天 86400 秒,小时 3600 分钟,60 分钟等等。
直接访问 xts
对象的索引会更快。
您似乎在尝试查看特定日期是否包含在 xts
对象索引的日期部分中。这对我有用:
library(xts)
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
myDate <- as.POSIXct("2007-01-04")
myDate %in% as.POSIXct(index(sample.xts), format="%Y-%m-%d")
这里是 counter-example 使用 %in%
:
R> x <- xts(1:20,
+ order.by=Sys.time() + cumsum(sample(1:10, 20, TRUE)*1e-6))
R> x
[,1]
2018-04-05 12:09:12.818800 1
2018-04-05 12:09:12.818805 2
2018-04-05 12:09:12.818809 3
2018-04-05 12:09:12.818810 4
2018-04-05 12:09:12.818819 5
2018-04-05 12:09:12.818827 6
2018-04-05 12:09:12.818832 7
2018-04-05 12:09:12.818837 8
2018-04-05 12:09:12.818843 9
2018-04-05 12:09:12.818847 10
2018-04-05 12:09:12.818848 11
2018-04-05 12:09:12.818849 12
2018-04-05 12:09:12.818858 13
2018-04-05 12:09:12.818867 14
2018-04-05 12:09:12.818872 15
2018-04-05 12:09:12.818877 16
2018-04-05 12:09:12.818881 17
2018-04-05 12:09:12.818888 18
2018-04-05 12:09:12.818889 19
2018-04-05 12:09:12.818890 20
R> reftime <- anytime::anytime("2018-04-05 12:09:12.818832")
R> reftime
[1] "2018-04-05 12:09:12.818831 CDT"
R> reftime %in% index(x)
[1] FALSE
R>
我从字面上复制并粘贴了一个随机条目(值 7)并 re-parsed 它。然而 %in%
失败了。
关注 R FAQ 7.31 你 可以 做类似
的事情R> which( abs(reftime - index(x)) < 1e-6)
[1] 7
R>
R> x[which( abs(reftime - index(x)) < 1e-6)]
[,1]
2018-04-05 12:09:12.818832 7
R>