简体   繁体   English

R:修改每天系列的xts矩阵

[英]R: modify xts matrix for each day series

I have an xts numeric matrix that includes multiple days of minute interval series. 我有一个xts数值矩阵,其中包含几天的分钟间隔序列。 I need to calculate statistics for each day on the minute periods, add new columns, and then put all the day series back together. 我需要计算分钟时段内每一天的统计信息,添加新列,然后将所有的日系列放在一起。

I have tried apply.daily(), which calls my stats function with an xts matrix, for each day, but I can't figure how to return the modified day series back to the invoking function and reassemble the full set of modified data. 我已经尝试了apply.daily(),它每天都使用xts矩阵调用stats函数,但是我无法弄清楚如何将修改后的日期序列返回给调用函数并重新组合全部修改后的数据。

One solution that could work is to use endpoints(x, on = "day") in a loop, then call rbind to reassemble the processed day frames. 一种可行的解决方案是在循环中使用端点(x,on =“ day”),然后调用rbind重新组合处理后的日期框架。 Is there a better solution? 有更好的解决方案吗?

process = function(myxts) {  
  day.indexes = endpoints(myxts, on="days")
  days = length(day.indexes) - 1

  l = list()
  list.index = 1

  for( i in 1:days ) {
    day.begin = day.indexes[i] + 1
    day.end = day.indexes[i+1]
    l[[list.index]] = ets.sym.process.daily(myxts[day.begin:day.end])
    list.index = list.index + 1
  }

  return(do.call("rbind", l))
}

You should be able to use some combination of do.call(rbind, lapply(split(myxts,"days"), myfun)) . 您应该可以使用do.call(rbind, lapply(split(myxts,"days"), myfun))某种组合。 It's hard to be more specific without a reproducible example. 没有可复制的示例,很难更具体。

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

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