Compare data over time periodsSource:
Given a tibble/data.frame, you can get date from two different but comparative date ranges. Lets say you want to compare visits in one year to visits from 2 years before without also seeing the previous 1 year. You can do that with this function.
The date.frame/tibble that holds the data
The column with the date value
The start of the period you want to analyze
The end of the period you want to analyze
How long ago do you want to compare data too. Time units are collapsed using
lubridate::floor_date(). The value can be:
Arbitrary unique English abbreviations as in the
lubridate::period()constructor are allowed.
suppressPackageStartupMessages(library(dplyr)) suppressPackageStartupMessages(library(timetk)) data_tbl <- ts_to_tbl(AirPassengers) %>% select(-index) ts_compare_data( .data = data_tbl , .date_col = date_col , .start_date = "1955-01-01" , .end_date = "1955-12-31" , .periods_back = "2 years" ) %>% summarise_by_time( .date_var = date_col , .by = "year" , visits = sum(value) ) #> # A tibble: 2 × 2 #> date_col visits #> <date> <dbl> #> 1 1953-01-01 2700 #> 2 1955-01-01 3408