この質問への回答でドミニクが提供した戦略を使用します
私はそれを私のqmaoパッケージの関数に変えました。このコードは、FinancialInstrument パッケージのgetSymbols.FIの中核でもあります。
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がこちらでエレガントなソリューションを提供しています
私が知る限り、(よく見なくても) メモリは、提供されている 3 つのソリューション ( @JoshuaUlrich、@Alex、および qmao::do.call.rbind ) のいずれにも問題はありません。だから、それはスピードに帰着します...
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
明らかにスピードで勝っています。