合并大量的xts对象 [英] merging a large list of xts objects

查看:120
本文介绍了合并大量的xts对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xts对象的列表,这些对象是互斥的日子.我想将列表merge放入一个大的xts对象.我这样做的尝试是"

I have a list of xts objects that are mutually exclusive days. I would like to merge the list into one large xts object. My attempt at doing this was to"

merged_reg_1_min_prices <- do.call(cbind, reg_1_min_prices)

但是,这似乎耗尽了内存. reg_1_min_prices是在互斥的日子中有6,000天的1分钟回报,因此它不是很大.有人知道如何解决这个问题吗?

However this seems to run out of memory. reg_1_min_prices is 6,000 days of 1 minute returns on mutually exclusive days so it's not very large. Does anyone know how to get around this?

要清楚:reg_1_min_prices包含互斥的天,每天的价格为1分钟,并且列表中的每个条目都是一个xts对象.

To be clear: reg_1_min_prices contains mutually exclusive days with 1 minute prices on each day and each entry in the list is an xts object.

推荐答案

我使用 Dominik 他对 -parallelized-in-r>这个问题

I use the strategy provided by Dominik in his answer to this question

我已将其转换为功能 noreferrer> qmao 软件包.该代码也是 FinancialInstrument软件包.

I have turned it into a function in my qmao package. This code is also at the core of getSymbols.FI in the FinancialInstrument package.

do.call.rbind <- function(lst) {
  while(length(lst) > 1) {
    idxlst <- seq(from=1, to=length(lst), by=2)
    lst <- lapply(idxlst, function(i) {
      if(i==length(lst)) { return(lst[[i]]) }
      return(rbind(lst[[i]], lst[[i+1]]))
    })
  }
  lst[[1]]
}

如果要rbind data.frames @JoshuaUlrich 提供了一个优雅的解决方案此处

If you want to rbind data.frames, @JoshuaUlrich has provided an elegant solution here

据我所知(无需仔细观察),内存并不是所提供的三种解决方案中的任何一个问题( @ JoshuaUlrich's @ Alex's 和qmao :: do.call.rbind).所以,这归结为速度...

As far as I can tell (without looking very closely) memory is not an issue with any of the three solutions offered (@JoshuaUlrich's, @Alex's, and qmao::do.call.rbind). So, it comes down to speed...

library(xts)
l <- lapply(Sys.Date()-6000:1, function(x) {
    N=60*8;xts(rnorm(N),as.POSIXct(x)-seq(N*60,1,-60))})
GS <- do.call.rbind
JU <- function(x) Reduce(rbind, x)
Alex <- function(x) do.call(rbind, lapply(x, as.data.frame)) #returns data.frame, not xts

identical(GS(l), JU(l)) #TRUE

library(rbenchmark)
benchmark(GS(l), JU(l), Alex(l), replications=1)
     test replications elapsed relative user.self sys.self user.child sys.child
3 Alex(l)            1  89.575 109.9080    56.584   33.044          0         0
1   GS(l)            1   0.815   1.0000     0.599    0.216          0         0
2   JU(l)            1 209.783 257.4025   143.353   66.555          0         0

do.call.rbind显然是速度上的胜利.

do.call.rbind clearly wins on speed.

这篇关于合并大量的xts对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆