Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amend calc_freq #21

Open
Analyst-In-The-Wild opened this issue Jul 6, 2021 · 6 comments
Open

amend calc_freq #21

Analyst-In-The-Wild opened this issue Jul 6, 2021 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@Analyst-In-The-Wild
Copy link
Member

compleeeteeeeeeeeeeeeeeeeeeeeee me!

@milanwiedemann
Copy link
Contributor

I know what lori means but I know I will forget, so here more details ...
when monthly sums get calculated, there may be groups with a count of zero.
the current function does not have a row for these cases in the data that gets returned and therefore it wont show in up in figures.
as this functions purpose is mainly to prepare data for figures this is VERY BAD! and needs t obe fixed.

I think I will use the https://tsibble.tidyverts.org/reference/fill_gaps.html function from the the https://tsibble.tidyverts.org/ package will help ...

I know that @ChrisBeeley wrote something for this a while ago that @Analyst-In-The-Wild and I reused in one of our reports, but it felt a bit hacky and this approach may be better?

@milanwiedemann
Copy link
Contributor

wait wait wait, I think I just found the code, it looks like this I believe:

  complete(month_floor_date = seq.Date(min(month_floor_date), max(month_floor_date), 
                   by = "month")) %>% 
  tidyr::replace_na(list(count = 0))

and I have to admit, looking at it right now it doesnt seem too hacky, maybe I just add something like this to our function

@milanwiedemann
Copy link
Contributor

just for reference, this is the function we are talking about

#' Calculate frequencies of groups per month
#'
#' @param data Data
#' @param by Grouping variables used in group_by
#' @param date_var_name String specifying date variable, needs to be class date
#' @param .drop_latest_month Logical, specifying whether or not to drop the most recent month
#' @param .calc_year_month_day_vars Logical, specifying whether to calculate separate year, month, and day variable
#'
#' @return
#' @export
#'
#' @examples
calc_monthly_freq <- function(data, by, date_var_name, .drop_latest_month = TRUE, .calc_year_month_day_vars = TRUE) {
data <- data %>%
dplyr::mutate(floor_date_m = lubridate::floor_date( {{date_var_name}} ,
unit = "month"),
floor_date_m = lubridate::as_date(floor_date_m))
if (.drop_latest_month) {
max_date_m <- max(data$floor_date_m)
data <- data %>%
dplyr::filter(floor_date_m != max_date_m)
}
data <- data %>%
dplyr::group_by(dplyr::across(c( {{by}}, floor_date_m))) %>%
dplyr::summarise(n = dplyr::n())
if (.calc_year_month_day_vars) {
data <- data %>%
mutate(year = lubridate::year(floor_date_m),
month = lubridate::month(floor_date_m,
label = TRUE,
abbr = TRUE),
day = lubridate::wday(floor_date_m,
label = TRUE,
abbr = TRUE)) %>%
dplyr::relocate(floor_date_m) %>%
dplyr::relocate(year, month, day,
.after = floor_date_m)
}
data %>%
dplyr::ungroup()
}

@milanwiedemann
Copy link
Contributor

@Analyst-In-The-Wild do you feel ready to implement this and make a pull request?
if not, how about we do it together?

@Analyst-In-The-Wild
Copy link
Member Author

@milanwiedemann let’s do it together as my only contribution to this was mentioning complete and you suddenly realised it was missing here then made me be your secretary to make the issue 😝

@Lextuga007
Copy link
Member

@Analyst-In-The-Wild complete() code doesn't appear in this function yet. Is this something you still have a record of/local branch at all?

@Lextuga007 Lextuga007 added the enhancement New feature or request label Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants