[R-SIG-Finance] rollapply over several columns

Murali.Menon at avivainvestors.com Murali.Menon at avivainvestors.com
Thu Sep 9 11:54:47 CEST 2010


The by.column = FALSE argument in rollapply() should help?
Murali 

-----Original Message-----
From: r-sig-finance-bounces at stat.math.ethz.ch [mailto:r-sig-finance-bounces at stat.math.ethz.ch] On Behalf Of Ulrich Staudinger
Sent: 09 September 2010 10:10
To: r-sig-finance
Subject: [R-SIG-Finance] rollapply over several columns

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

_______________________________________________
R-SIG-Finance at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.


More information about the R-SIG-Finance mailing list