gpt4 book ai didi

r - 多元 XTS 作为参数适用于 lapply 但不适用于 mclapply

转载 作者:行者123 更新时间:2023-12-02 00:14:09 25 4
gpt4 key购买 nike

我有一个函数可以与 lapply 一起使用,但如果我用 mclapply 尝试它会返回一个错误。该函数的参数是一个多变量 XTS。这是一个示例:

library(quantmod)
library(doMC)
registerDoMC(4)

test <- function(x){
return(mean(x))
}

myEnv <- new.env()
getSymbols(c("^GSPC", "^RUT"), env=myEnv)
data <- do.call(merge, c(eapply(myEnv, Ad), all=TRUE))

lapply(data, test)
mclapply(data, test)

lapply 按预期返回结果,但 mclapply 返回:

Error in `[.xts`(X, seq(i, length(X), by = cores)) : 
subscript out of bounds

有人可以帮我吗?谢谢。

session 信息

R version 2.15.2 (2012-10-26)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] doMC_1.2.5 multicore_0.1-7 iterators_1.0.6 foreach_1.4.0 quantmod_0.3-22 TTR_0.21-1 xts_0.9-0 zoo_1.7-9
[9] Defaults_1.1-1

loaded via a namespace (and not attached):
[1] codetools_0.2-8 grid_2.15.2 lattice_0.20-10 Rcpp_0.9.15 tools_2.15.2

最佳答案

xts 对象在以下意义上很有趣:

length(data)
# [1] 3010
data[3010]
# Error in `[.xts`(data, 3010) : subscript out of bounds

而且,mclapply 不喜欢...

你会发现 mclapply(as.list(data), test) 会工作,尽管文档说:

X: a vector (atomic or list) or an expressions vector. Other objects (including classed objects) will be coerced by as.list.

想想……这可能值得向作者提及。

关于r - 多元 XTS 作为参数适用于 lapply 但不适用于 mclapply,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13999860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com