具有返回多个值的函数的rollapply
假设我想将rollapply与返回的值大于的函数一起使用。像这样:具有返回多个值的函数的rollapply,r,rollapply,R,Rollapply,假设我想将rollapply与返回的值大于的函数一起使用。像这样: library(quantmod) getSymbols("YHOO") openYHOO <- YHOO[1:10,1] rollapply(openYHOO, width = 2, range) 更多的错误。 我可以这样做: cbind( rollapply(openYHOO, width = 2, function(x) { range(x)[1] }), rollapply(openYHO
library(quantmod)
getSymbols("YHOO")
openYHOO <- YHOO[1:10,1]
rollapply(openYHOO, width = 2, range)
更多的错误。
我可以这样做:
cbind(
rollapply(openYHOO, width = 2, function(x) {
range(x)[1]
}),
rollapply(openYHOO, width = 2, function(x) {
range(x)[2]
})
)
…而且很有效
然而,如果我想称之为fivenum,或者在有趣的争论中使用更复杂和计算密集的东西,该怎么办?我是否必须为要返回的每个值调用rollapply,一次又一次地生成相同的对象
我是否遗漏了什么,或者我应该放弃rollapply并滚动我自己的滚动窗口功能
你能解释一下为什么这个rollapplyopenYHOO,width=2,range不起作用吗?使用by.column参数
这很有效。谢谢不是我从帮助文档中对by.column logical的描述所期望的。如果为TRUE,则会将FUN分别应用于每个列。但是文档中的示例确实显示了我正在寻找的行为。适用于我的示例:rollappyopenyhoo,width=3,FUN=functionx coeflmx~seq_alongx,by.column=FALSE,align=right
cbind(
rollapply(openYHOO, width = 2, function(x) {
range(x)[1]
}),
rollapply(openYHOO, width = 2, function(x) {
range(x)[2]
})
)
rollapply(openYHOO, width=2, range, by.column=FALSE)
# [,1] [,2]
#2007-01-03 NA NA
#2007-01-04 25.64 25.85
#2007-01-05 25.64 26.70
#2007-01-08 26.70 27.70
#2007-01-09 27.70 28.00
#2007-01-10 27.48 28.00
#2007-01-11 27.48 28.76
#2007-01-12 28.76 28.98
#2007-01-16 28.98 29.88
#2007-01-17 29.40 29.88
> rollapply(openYHOO, width=2,
function(x) fivenum(as.numeric(x)),
by.column=FALSE)
# [,1] [,2] [,3] [,4] [,5]
#2007-01-03 NA NA NA NA NA
#2007-01-04 25.64 25.64 25.745 25.85 25.85
#2007-01-05 25.64 25.64 26.170 26.70 26.70
#2007-01-08 26.70 26.70 27.200 27.70 27.70
#2007-01-09 27.70 27.70 27.850 28.00 28.00
#2007-01-10 27.48 27.48 27.740 28.00 28.00
#2007-01-11 27.48 27.48 28.120 28.76 28.76
#2007-01-12 28.76 28.76 28.870 28.98 28.98
#2007-01-16 28.98 28.98 29.430 29.88 29.88
#2007-01-17 29.40 29.40 29.640 29.88 29.88