Search code examples
rdataframetime-seriesxtsmidasr

Enforce equal amount of days each quarter R 'xts'


Suppose I have the following data set:

Daily observations of the S&P500, and Quarterly Total Public Debt. The observation of the quarter is at time

xxxx-01-01
xxxx-04-01
xxxx-07-01
xxxx-10-01

The non trading days such as weekend and holidays are denoted with NAs

2020-01-01 NA
2020-01-02 3257.85
2020-01-02 3234.85
.
.
.
.
2020-03-31 2584.59

This will yield an unequal amount of observation per quarter. MY question is how do I remove a certain amount of dates such that within each quarter I will have exactly 66 observations of the S&P500?


Solution

  • We can convert the index to yearqtr (from zoo), use that to create a logical index for first 66 observations

    xt1[ave(seq_along(index(xt1)), as.yearqtr(index(xt1)), FUN = 
            seq_along) <= 66]
    

    As @G.Grothendieck mentioned in the comments, the idea would be to first remove the NA elements

    xt2 <- na.omit(xt1)
    

    then, calculate the minimum number of elements per each quarter

    n <- min(tapply(seq_along(index(xt1)), as.yearqtr(index(xt1)), FUN = length))
    

    Use that in first code block

    xt2[ave(seq_along(index(xt2)), as.yearqtr(index(xt2)), FUN = 
            seq_along) <= n]