r - 转换带时间戳的行数据时的性能问题

标签 r xts

我编写了一个函数,它接受一个 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)
}

编辑:

下面是我的一行数据。每行代表一个 1 分钟的间隔,我试图将其转换为任意桶,这些桶是这些 1 分钟间隔的总和,即 5 分钟、15 分钟、60 分钟、240 分钟等...

date                time    open    high     low   close volume
2005-09-06 2005-09-06 16:33:00 1297.25 1297.50 1297.25 1297.25     98

最佳答案

您可能想要重用现有的设施,特别是 POSIXct 时间类型,以及现有的包。

例如,查看 xts包 --- 它已经有一个通用函数 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 - 转换带时间戳的行数据时的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7485107/

相关文章:

r - 将数据帧的元素转换为二进制数据

r - 增加观测值数量会使 R 抛出随机系数 - 数值稳定性问题?

r - 在 R 上安装 xts 包时出错

r - 使用颜色和线型美学属性控制绘图的图例标签

R Shiny 应用程序 - "Disconnected from the server. Reload."

r - 用索引将xts/zoo对象写入csv

R - 线条之间的颜色或阴影区域

r - 向 quantmod/xts 添加因子列

r - 使用 ts 对象或 xts 对象的 ccf 提供不同的滞后

python - 将分块文件读入数据帧