Search code examples
rquantmod

filter date index in xts format


I download some rates from FRED, how I can filter the index date in order to keep only the dates after '2010' ?

enter image description here


Solution

  • It it is an xts object, then we extract the year from index to create a logical vector for subsetting

    library(xts)
    xt1[lubridate::year(index(xt1)) >= 2010]
    

    Or use subset with %>%

    library(dplyr)
    xt1 %>%
           subset(year(index(.))>= 2010)
    

    data

    set.seed(24)
    xt1 <- xts(rnorm(50), order.by = seq(as.Date('2009-01-01'), 
        length.out = 50, by = '1 month'))