我有一个xts
包含多个股票代码时间序列的对象。我需要将xts
对象拆分为特定于符号的子组并处理每个符号的数据,然后重新组合xts
包含完整行集的原始矩阵中的所有子组。每个符号是一个介于 1 到 4 个字符之间的字段,用作将矩阵拆分为子组的因子索引。
这些是报告在调用时拆分我的矩阵的时间by()
,lapply()
并且ddply()
:
> dim(ets)
[1] 442750 24
> head(ets)
Symbol DaySec ExchTm LclTm Open High Low Close CloseRet
2011-07-22 09:35:00 "AA" "34500" "09:34:54.697.094" "09:34:54.697.052" " 158100" " 158400" " 157900" " 158200" " 6.325111e-04"
2011-07-22 09:35:00 "AAPL" "34500" "09:34:59.681.827" "09:34:59.681.797" "3899200" "3899200" "3892200" "3894400" "-1.231022e-03"
2011-07-22 09:35:00 "ABC" "34500" "09:34:49.805.994" "09:34:49.806.008" " 400100" " 401800" " 400100" " 401600" " 3.749063e-03"
2011-07-22 09:35:00 "ALL" "34500" "09:34:59.009.001" "09:34:59.008.810" " 285500" " 285500" " 285300" " 285300" "-7.005254e-04"
2011-07-22 09:35:00 "AMAT" "34500" "09:34:59.982.447" "09:34:59.982.423" " 130200" " 130500" " 130200" " 130500" " 2.304147e-03"
2011-07-22 09:35:00 "AMZN" "34500" "09:34:48.012.576" "09:34:48.012.565" "2137400" "2139100" "2137400" "2139100" " 7.953588e-04"
... (15 more columns)
> system.time(by(ets, ets$Symbol, function(x) { return(x) }))
user system elapsed
78.725 0.932 79.735
> system.time(ddply(as.data.frame(ets), "Symbol", function(x) { return (x) }))
user system elapsed
100.590 0.416 101.105
> system.time(lapply(split.default(ets, ets$Symbol), function(x) { return(x) }))
user system elapsed
1.572 0.280 1.853
这篇出色的博客文章中提供了有关使用数据框和矩阵子组的更多信息。
为什么使用 lapply/split.default 时性能会有如此大的差异?