Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 转换带时间戳的行数据时出现性能问题_R_Xts - Fatal编程技术网

R 转换带时间戳的行数据时出现性能问题

R 转换带时间戳的行数据时出现性能问题,r,xts,R,Xts,我已经编写了一个函数,它接受一个data.frame,它表示在1分钟的时间范围内发生的数据间隔。该功能的目的是将这些1分钟的间隔转换为更高的间隔。例如,1分钟变为5分钟、60分钟等。数据集本身可能存在数据间隙,即时间跳跃,因此必须适应这些错误数据的出现。我已经编写了以下代码,这些代码似乎可以工作,但在大型数据集上的性能绝对糟糕 我希望有人能给我一些建议,告诉我怎样才能加快速度。见下文 compressMinute = function(interval, DAT) { #Grab all

我已经编写了一个函数,它接受一个data.frame,它表示在1分钟的时间范围内发生的数据间隔。该功能的目的是将这些1分钟的间隔转换为更高的间隔。例如,1分钟变为5分钟、60分钟等。数据集本身可能存在数据间隙,即时间跳跃,因此必须适应这些错误数据的出现。我已经编写了以下代码,这些代码似乎可以工作,但在大型数据集上的性能绝对糟糕

我希望有人能给我一些建议,告诉我怎样才能加快速度。见下文

compressMinute = function(interval, DAT) {
    #Grab all data which begins at the same interval length
    retSet = NULL
    intervalFilter = which(DAT$time$min %% interval == 0)
    barSet = NULL
    for (x in intervalFilter) {
        barEndTime = DAT$time[x] + 60*interval
        barIntervals = DAT[x,]
        x = x+1
        while(x <= nrow(DAT) & DAT[x,"time"] < barEndTime) {
            barIntervals = rbind(barIntervals,DAT[x,])
            x = x + 1
        }
        bar = data.frame(date=barIntervals[1,"date"],time=barIntervals[1,"time"],open=barIntervals[1,"open"],high=max(barIntervals[1:nrow(barIntervals),"high"]),
                        low=min(barIntervals[1:nrow(barIntervals),"low"]),close=tail(barIntervals,1)$close,volume=sum(barIntervals[1:nrow(barIntervals),"volume"]))
        if (is.null(barSet)) {
            barSet = bar
        } else {
            barSet = rbind(barSet, bar)
        }

    }
    return(barSet)
}

您可能希望重用现有的工具,特别是
POSIXct
时间类型,以及现有的包

例如,看看这个包——它已经有了一个通用函数
to.period()
,以及方便的包装器
to.minutes()
to.minutes3()
to.minutes10()

以下是帮助页面中的一个示例:

R> example(to.minutes)

t.mn10R> data(sample_matrix)

t.mn10R> samplexts <- as.xts(sample_matrix)

t.mn10R> to.monthly(samplexts)
         samplexts.Open samplexts.High samplexts.Low samplexts.Close
Jan 2007        50.0398        50.7734       49.7631         50.2258
Feb 2007        50.2245        51.3234       50.1910         50.7709
Mar 2007        50.8162        50.8162       48.2365         48.9749
Apr 2007        48.9441        50.3378       48.8096         49.3397
May 2007        49.3457        49.6910       47.5180         47.7378
Jun 2007        47.7443        47.9413       47.0914         47.7672

t.mn10R> to.monthly(sample_matrix)
         sample_matrix.Open sample_matrix.High sample_matrix.Low sample_matrix.Close
Jan 2007            50.0398            50.7734           49.7631             50.2258
Feb 2007            50.2245            51.3234           50.1910             50.7709
Mar 2007            50.8162            50.8162           48.2365             48.9749
Apr 2007            48.9441            50.3378           48.8096             49.3397
May 2007            49.3457            49.6910           47.5180             47.7378
Jun 2007            47.7443            47.9413           47.0914             47.7672

t.mn10R> str(to.monthly(samplexts))
An ‘xts’ object from Jan 2007 to Jun 2007 containing:
  Data: num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "samplexts.Open" "samplexts.High" "samplexts.Low" "samplexts.Close"
  Indexed by objects of class: [yearmon] TZ: 
  xts Attributes:  
 NULL

t.mn10R> str(to.monthly(sample_matrix))
 num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:6] "Jan 2007" "Feb 2007" "Mar 2007" "Apr 2007" ...
  ..$ : chr [1:4] "sample_matrix.Open" "sample_matrix.High" "sample_matrix.Low" "sample_matrix.Close"
R> 
R>示例(至.min)
t、 mn10R>数据(样本矩阵)
t、 mn10R>samplexts至.月(samplexts)
samplexts.打开samplexts.高samplexts.低samplexts.关闭
2007年1月50.0398 50.7734 49.7631 50.2258
2007年2月50.224551.323450.191050.7709
2007年3月50.8162 50.8162 48.2365 48.9749
2007年4月48.9441 50.3378 48.8096 49.3397
2007年5月49.3457 49.6910 47.5180 47.7378
2007年6月47.7443 47.9413 47.0914 47.7672
t、 mn10R>至每月(样本矩阵)
样本矩阵。打开样本矩阵。高样本矩阵。低样本矩阵。关闭
2007年1月50.0398 50.7734 49.7631 50.2258
2007年2月50.224551.323450.191050.7709
2007年3月50.8162 50.8162 48.2365 48.9749
2007年4月48.9441 50.3378 48.8096 49.3397
2007年5月49.3457 49.6910 47.5180 47.7378
2007年6月47.7443 47.9413 47.0914 47.7672
t、 mn10R>str(至每月(样本集))
2007年1月至2007年6月的“xts”对象包含:
数据:num[1:6,1:4]50.250.848.949.3。。。
-属性(*,“dimnames”)=2个列表
..$:空
..$:chr[1:4]“samplexts.Open”“samplexts.High”“samplexts.Low”“samplexts.Close”
由类的对象索引:[yearmon]TZ:
xts属性:
无效的
t、 mn10R>str(至每月(样本矩阵))
数字[1:6,1:4]50.250.848.949.3。。。
-属性(*,“dimnames”)=2个列表
…$:chr[1:6]“2007年1月”“2007年2月”“2007年3月”“2007年4月”。。。
..$:chr[1:4]“样本矩阵。打开”“样本矩阵。高”“样本矩阵。低”“样本矩阵。关闭”
R>

不知道您在上面真正想做什么,但是行
barIntervals=rbind(barIntervals,DAT[x,])
是一个瓶颈。每次迭代都会增加barIntervals对象,这迫使R复制它……如果预先分配barIntervals,它会工作得更好。谷歌
R预分配
阅读更多信息。另外,package
zoo
中的函数
na.approx()
是否满足您的要求?检查这个问题:发现XTS。巨大的差异。+1只是增加了一个注释:
到.period
有利于聚合到OHLC数据<代码>期间。应用允许您使用用户定义的函数进行聚合。
R> example(to.minutes)

t.mn10R> data(sample_matrix)

t.mn10R> samplexts <- as.xts(sample_matrix)

t.mn10R> to.monthly(samplexts)
         samplexts.Open samplexts.High samplexts.Low samplexts.Close
Jan 2007        50.0398        50.7734       49.7631         50.2258
Feb 2007        50.2245        51.3234       50.1910         50.7709
Mar 2007        50.8162        50.8162       48.2365         48.9749
Apr 2007        48.9441        50.3378       48.8096         49.3397
May 2007        49.3457        49.6910       47.5180         47.7378
Jun 2007        47.7443        47.9413       47.0914         47.7672

t.mn10R> to.monthly(sample_matrix)
         sample_matrix.Open sample_matrix.High sample_matrix.Low sample_matrix.Close
Jan 2007            50.0398            50.7734           49.7631             50.2258
Feb 2007            50.2245            51.3234           50.1910             50.7709
Mar 2007            50.8162            50.8162           48.2365             48.9749
Apr 2007            48.9441            50.3378           48.8096             49.3397
May 2007            49.3457            49.6910           47.5180             47.7378
Jun 2007            47.7443            47.9413           47.0914             47.7672

t.mn10R> str(to.monthly(samplexts))
An ‘xts’ object from Jan 2007 to Jun 2007 containing:
  Data: num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:4] "samplexts.Open" "samplexts.High" "samplexts.Low" "samplexts.Close"
  Indexed by objects of class: [yearmon] TZ: 
  xts Attributes:  
 NULL

t.mn10R> str(to.monthly(sample_matrix))
 num [1:6, 1:4] 50 50.2 50.8 48.9 49.3 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:6] "Jan 2007" "Feb 2007" "Mar 2007" "Apr 2007" ...
  ..$ : chr [1:4] "sample_matrix.Open" "sample_matrix.High" "sample_matrix.Low" "sample_matrix.Close"
R>