如何优化此R代码以加快处理速度并避免循环?

时间:2019-04-02 21:08:47

标签: r optimization

我有以下代码可以正常工作:

period <- 3600000
timeticks <- c(1552820400000, 1552824000000, 1552827600000)
data <- structure(list(start = c(1552820400000, 1552820460000, 1552820520000, 1552824000000, 1513846500000, 1552827720000), 
                       close = c(1552820459999, 1552820519999, 1513846579999, 1552824059999, 1513846559999, 1552827779999),
                       value = c(0.0012,  0.004, 0.0011, 0.00125, 0.0014, 0.0018)), 
                  row.names = c(NA, 6L), 
                  class = "data.frame")
rows <- list()
for (i in 1:length(timeticks)) {
  minIndex = min(which(data$start >= timeticks[i][1] & data$close <= (timeticks[i][1]+period-1), arr.ind = TRUE))
  maxIndex = max(which(data$start >= timeticks[i][1] & data$close <= (timeticks[i][1]+period-1), arr.ind = TRUE))
  if (is.infinite(minIndex) || is.infinite(maxIndex)) {
    newRow <- data.frame(start=timeticks[i][1], close=(timeticks[i][1]+period-1))
  } else {
    newRow <- data.frame(highest=as.character(max(data$value[minIndex:maxIndex])), lowest=as.character(min(data$value[minIndex:maxIndex])), start=timeticks[i][1], end=(timeticks[i][1]+period-1))
  }
  rows <- list.append(rows, newRow)
}
rows <- data.table::rbindlist(rows, use.names=TRUE)

目标是从“时间标记”中找到要考虑的数据的最小和最大索引,然后在该索引范围内找到data $ value的最低和最高值。

目标结果示例:

structure(list(start = c(1552820400000, 1552824000000, 1552820520000, 1552824000000, 1513846500000, 1552838520000), 
               end =   c(1552823999999, 1552827599999, 1513846579999, 1552824059999, 1513846559999, 1552838580000),
               highest = c(0.004, 0.00125, 0.0018),
               lowest = c(0.0011, 0.00125, 0.0018)), 
          row.names = c('start', 'end', 'lowest', 'highest'), 
          class = "data.frame")

如何优化此代码?

0 个答案:

没有答案