xts - Convert daily to weekly/monthly data with R -


i have daily prices series on wide range of products; want convert new dataframe weekly or monthly data.

enter image description here

i first used xts in order apply to.weekly function...which works ohlc format. sure there may exist function similar to.weekly dataframe format not ohlc.

there different posts related following: does rollapply() allow array of results call function? or averaging daily data weekly data

i using:

length(bra)

1 2416

test<-bra[seq(1,2416,7),]

would there more efficient approach? thanks.

let's try data:

library(zoo) tt <- seq(sys.date(), by='day', length=365) vals <- data.frame(a=runif(365), b=rnorm(365), c=1:365) z <- zoo(vals, tt) 

now define function extracts year , number of week (drop %y if don't need distinguish between years):

week <- function(x)format(x, '%y.%w') 

you can use function aggregate zoo object mean (for example):

aggregate(z, by=week, fun=mean) 

which produces result:

                          b  c 2013.18 0.3455357  0.34129269  3 2013.19 0.4506297  0.57665133  9 2013.20 0.3950585  0.46197173 16 2013.21 0.5990886 -0.02689994 23 2013.22 0.5115043  0.18726564 30 2013.23 0.5327597  0.16250339 37 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -