0
votes

I have daily time series (of working days) which I would like to transform in monthly average.

The date format is %d/%m/%Y, moreover there are some missing observations (NA).

How can I do this?


# my data
timeseries <- structure(c(309L, 319L, 329L, 339L, 348L, 374L, 384L, 394L, 404L, 413L, 
  2317L, 2327L, 2337L, 2347L, 2356L, 2382L, 2392L, 2402L, 2412L, 2421L, 2447L, 2457L, 
  422L, 432L, 441L, 467L, 477L, 487L, 497L, 506L, 2467L, 2477L, 2487L, 2497L, 2506L,
   2532L, 2542L, 2552L, 2562L, 2571L, 2597L, 2607L, 2617L, 2627L, 2636L, 
  [...]), .Label = c("01/01/1992", "01/01/1993", "01/01/1996", "01/01/1997", "01/01/1998", "01/01/1999", "01/01/2001 [...] ), class = "factor")
1
Welcome to SO. Would be great if you can dput your daily times series e.g dput(yourTS)agstudy
How about using the xts apply.monthly function: apply.monthly(x,mean) where x is an xts objectchandler
structure(c(309L, 319L, 329L, 339L, 348L, 374L, 384L, 394L, 404L, 413L, 2317L, 2327L, 2337L, 2347L, 2356L, 2382L, 2392L, 2402L, 2412L, 2421L, 2447L, 2457L, 422L, 432L, 441L, 467L, 477L, 487L, 497L, 506L, 2467L, 2477L, 2487L, 2497L, 2506L, 2532L, 2542L, 2552L, 2562L, 2571L, 2597L, 2607L, 2617L, 2627L, 2636L, [...]), .Label = c("01/01/1992", "01/01/1993", "01/01/1996", "01/01/1997", "01/01/1998", "01/01/1999", "01/01/2001 [...] ), class = "factor")BrunoGG

1 Answers

1
votes

You can do this many, many ways. Using base R packages:

d <- data.frame(Date=Sys.Date()+1:60, Data=1:60)
tapply(d$Data, format(d$Date,"%Y%m"), mean)
aggregate(d$Data, by=list(Date=format(d$Date,"%Y%m")), mean)