[R-SIG-Finance] rollapply over several columns
Ulrich Staudinger
ustaudinger at gmail.com
Thu Sep 9 11:10:10 CEST 2010
Hi list,
I am trying to use rollapply on am wondering about the following question:
1) How can I tell rollapply to process my data not column after column
but all columns at once?
Consider the following data set (a record of quotes):
> part
V2 V3 V4 V5
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
2010-08-22 23:03:18 105500 12 107400 1
> class(part)
[1] "xts" "zoo"
>
... and the following very simply function ....
testFunction <- function(x)
{
cat(class(x), "///", x, "\n")
rnorm(1)
}
.... the output of using rollapply on this is:
> rollapply(part, 4, testFunction)
numeric /// 105500 105500 105500 105500
numeric /// 105500 105500 105500 105500
numeric /// 105500 105500 105500 105500
numeric /// 105500 105500 105500 105500
numeric /// 105500 105500 105500 105500
numeric /// 105500 105500 105500 105500
numeric /// 105500 105500 105500 105500
numeric /// 12 12 12 12
numeric /// 12 12 12 12
numeric /// 12 12 12 12
numeric /// 12 12 12 12
numeric /// 12 12 12 12
numeric /// 12 12 12 12
numeric /// 12 12 12 12
numeric /// 107400 107400 107400 107400
numeric /// 107400 107400 107400 107400
numeric /// 107400 107400 107400 107400
numeric /// 107400 107400 107400 107400
numeric /// 107400 107400 107400 107400
numeric /// 107400 107400 107400 107400
numeric /// 107400 107400 107400 107400
numeric /// 1 1 1 1
numeric /// 1 1 1 1
numeric /// 1 1 1 1
numeric /// 1 1 1 1
numeric /// 1 1 1 1
numeric /// 1 1 1 1
numeric /// 1 1 1 1
V2 V3 V4 V5
2010-08-22 23:03:18 -0.26829508 1.3107688 1.4787251 -0.2583725
2010-08-22 23:03:18 -2.17382434 0.1600959 0.4368223 -1.3948148
2010-08-22 23:03:18 -0.48042277 -0.3413495 -0.8816699 -0.2819355
2010-08-22 23:03:18 0.88840373 -0.7273423 -1.1688194 0.2605899
2010-08-22 23:03:18 -0.04364438 -0.3462516 -1.2633264 -0.7175950
2010-08-22 23:03:18 -0.79960171 0.2439625 0.1893828 -1.0448513
2010-08-22 23:03:18 0.53943691 1.2008782 0.2796326 -1.4020305
>
......... as you can see, it processes column after column, with a
nice rolling window. But I would like it to hand in all four columns
at once, with a sliding window.
How?
Thanks in advance,
Ulrich
--
Ulrich Staudinger
http://new.activequant.org
More information about the R-SIG-Finance
mailing list