是否有解决 R 2.15.2 中 do.call(cbind.xts,...) 性能缓慢的解决方法? [英] Is there a work around for slow performance of do.call(cbind.xts,...) in R 2.15.2?
问题描述
我希望 cbind.xts
和 do.call(cbind.xts)
以相似的运行时间执行.R2.11、R2.14 也是如此.
I would expect cbind.xts
and do.call(cbind.xts)
to perform with similar elapsed time.
That was true for R2.11, R2.14.
对于 R2.15.2 和 xts 0.8-8,do.call(cbind.xts,...)
变体执行慢得多,这有效地打破了我以前的代码.
For R2.15.2 and xts 0.8-8, the do.call(cbind.xts,...)
variant performs drastically slower, which effectively breaks my previous codes.
正如 Josh Ulrich 在下面的评论中指出的那样,xts 包维护者已经意识到这个问题.在此期间,是否有方便的解决方法?
As Josh Ulrich notes in a comment below, the xts package maintainers are aware of this problem. In the meantime, is there a convenient work around?
可重现的例子:
library(xts)
secs <- function (rows, from = as.character(Sys.time()), cols = 1, by = 1)
{
deltas <- seq(from = 0, by = by, length.out = rows)
nacol <- matrix(data = NA, ncol = cols, nrow = rows)
xts(x = nacol, order.by = strptime(from, format = "%Y-%m-%d %X") +
deltas)
}
n <- 20
d1 <- secs(rows=n*100,cols=n)
d2 <- secs(rows=n*100,cols=n)
system.time(cbind.xts(d1,d2))
对比
system.time(do.call(cbind.xts, list(d1,d2)))
推荐答案
一种解决方法是在 do.call
中设置 quote=TRUE
.
One work-around is to set quote=TRUE
in do.call
.
R> system.time(cb <- cbind.xts(d1,d2))
user system elapsed
0.004 0.000 0.004
R> system.time(dc <- do.call(cbind.xts, list(d1,d2), quote=TRUE))
user system elapsed
0.000 0.004 0.004
R> identical(cb,dc)
[1] TRUE
缓慢是由 do.call
默认在评估函数调用之前评估参数造成的,这导致调用要大得多.例如,比较这两个调用:
The slowness is caused by do.call
evaluating the arguments before evaluating the function call by default, which causes the call to be much larger. For example, compare these two calls:
call("cbind", d1, d2) # huge
call("cbind", quote(d1), quote(d2)) # dainty
这篇关于是否有解决 R 2.15.2 中 do.call(cbind.xts,...) 性能缓慢的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!