|
| 1 | +# -------------------------------------------------------------------------------------------- |
| 2 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +# Licensed under the MIT License. See LICENSE.txt in the project root for license information. |
| 4 | +# -------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +#' @title Identify date frequency based on a series of dates |
| 7 | +#' |
| 8 | +#' @description |
| 9 | +#' `r lifecycle::badge('experimental')` |
| 10 | +#' |
| 11 | +#' Takes a vector of dates and identify whether the frequency is 'daily', |
| 12 | +#' 'weekly', or 'monthly'. The primary use case for this function is to provide |
| 13 | +#' an accurate description of the query type used and for raising errors should |
| 14 | +#' a wrong date grouping be used in the data input. |
| 15 | +#' |
| 16 | +#' @param x Vector containing a series of dates. |
| 17 | +#' |
| 18 | +#' @details |
| 19 | +#' Date frequency detection works as follows: |
| 20 | +#' - If at least three days of the week are present (e.g., Monday, Wednesday, |
| 21 | +#' Thursday) in the series, then the series is classified as 'daily' |
| 22 | +#' - If the total number of months in the series is equal to the length, then |
| 23 | +#' the series is classified as 'monthly' |
| 24 | +#' - If the total number of sundays in the series is equal to the length of |
| 25 | +#' the series, then the series is classified as 'weekly |
| 26 | +#' |
| 27 | +#' @section Limitations: |
| 28 | +#' One of the assumptions made behind the classification is that weeks are |
| 29 | +#' denoted with Sundays, hence the count of sundays to measure the number of |
| 30 | +#' weeks. In this case, weeks where a Sunday is missing would result in an |
| 31 | +#' 'unable to classify' error. |
| 32 | +#' |
| 33 | +#' Another assumption made is that dates are evenly distributed, i.e. that the |
| 34 | +#' gap between dates are equal. If dates are unevenly distributed, e.g. only two |
| 35 | +#' days of the week are available for a given week, then the algorithm will fail |
| 36 | +#' to identify the frequency as 'daily'. |
| 37 | +#' |
| 38 | +#' @return |
| 39 | +#' String describing the detected date frequency, i.e.: |
| 40 | +#' - 'daily' |
| 41 | +#' - 'weekly' |
| 42 | +#' - 'monthly' |
| 43 | +#' |
| 44 | +#' @examples |
| 45 | +#' start_date <- as.Date("2022/06/26") |
| 46 | +#' end_date <- as.Date("2022/11/27") |
| 47 | +#' |
| 48 | +#' # Daily |
| 49 | +#' day_seq <- |
| 50 | +#' seq.Date( |
| 51 | +#' from = start_date, |
| 52 | +#' to = end_date, |
| 53 | +#' by = "day" |
| 54 | +#' ) |
| 55 | +#' |
| 56 | +#' identify_datefreq(day_seq) |
| 57 | +#' |
| 58 | +#' # Weekly |
| 59 | +#' week_seq <- |
| 60 | +#' seq.Date( |
| 61 | +#' from = start_date, |
| 62 | +#' to = end_date, |
| 63 | +#' by = "week" |
| 64 | +#' ) |
| 65 | +#' |
| 66 | +#' identify_datefreq(week_seq) |
| 67 | +#' |
| 68 | +#' # Monthly |
| 69 | +#' month_seq <- |
| 70 | +#' seq.Date( |
| 71 | +#' from = start_date, |
| 72 | +#' to = end_date, |
| 73 | +#' by = "month" |
| 74 | +#' ) |
| 75 | +#' identify_datefreq(month_seq) |
| 76 | +#' |
| 77 | +#' @export |
| 78 | + |
| 79 | +identify_datefreq <- function(x){ |
| 80 | + |
| 81 | + # Data frame for checking |
| 82 | + date_df <- data.frame( |
| 83 | + weekdays = names(table(weekdays(x))), |
| 84 | + n = as.numeric(table(weekdays(x))) |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | + dweekchr <- c( |
| 89 | + "Sunday", |
| 90 | + "Saturday", |
| 91 | + "Monday", |
| 92 | + "Tuesday", |
| 93 | + "Wednesday", |
| 94 | + "Thursday", |
| 95 | + "Friday" |
| 96 | + ) |
| 97 | + |
| 98 | + # At least 3 days of the week must be present |
| 99 | + check_wdays <- ifelse( |
| 100 | + sum(dweekchr %in% date_df$weekdays) >= 3, TRUE, FALSE) |
| 101 | + |
| 102 | + # Check number of Sundays - should equal number of weeks if weekly |
| 103 | + check_nsun <- sum(date_df$n[date_df$weekdays == "Sunday"]) |
| 104 | + |
| 105 | + ifelse( |
| 106 | + length(months(x)) == length(x), |
| 107 | + "monthly", |
| 108 | + ifelse( |
| 109 | + check_nsun == length(x), |
| 110 | + "weekly", |
| 111 | + ifelse( |
| 112 | + check_wdays, |
| 113 | + "daily", |
| 114 | + "Unable to identify date frequency." |
| 115 | + ) |
| 116 | + ) |
| 117 | + ) |
| 118 | +} |
0 commit comments