Say I want to use rollapply with a function that returns more than on value. Like this:
library(quantmod)
getSymbols("YHOO")
openYHOO <- YHOO[1:10,1]
rollapply(openYHOO, width = 2, range)
I get an error. I also tried merging the results inside the function:
rollapply(openYHOO, width = 2, function(x) {
cbind(range(x))
})
rollapply(openYHOO, width = 2, function(x) {
merge(range(x))
})
More errors.
I can do this:
cbind(
rollapply(openYHOO, width = 2, function(x) {
range(x)[1]
}),
rollapply(openYHOO, width = 2, function(x) {
range(x)[2]
})
)
...and it works.
However, what if I want to call fivenum
or use something much more complicated and computationally intensive in the fun argument? Do I have to call rollapply for each value that I want to return, generating the same object over and over again?
Am I missing something or should I abandon rollapply and roll my own rolling window function?
Can you explain why this rollapply(openYHOO, width = 2, range)
does not work?