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

Years as a cohort interval #5

Open
carrieseltzer opened this issue Oct 28, 2022 · 0 comments
Open

Years as a cohort interval #5

carrieseltzer opened this issue Oct 28, 2022 · 0 comments

Comments

@carrieseltzer
Copy link

Thank you for creating the cohorts package! I was looking for something to help me analyze donor retention across years and this package and explanation was far and away the simplest thing I found.

However, I wanted to look at years rather than months, so I created a new function with very minor changes that uses lubridate instead of zoo:

cohort_table_year <- function(df, id_var, date)
{
  dt <- dtplyr::lazy_dt(df)
  dt %>% dplyr::rename(id = {
    {
      id_var
    }
  }) %>% dplyr::group_by(id) %>% dplyr::mutate(year = lubridate::year({
    {
      date
    }
  })) %>% dplyr::mutate(cohort = min(year)) %>% dplyr::group_by(cohort,
                                                                 year) %>% dplyr::summarise(users = dplyr::n_distinct(id)) %>%
    tidyr::pivot_wider(names_from = year, values_from = users) %>%
    dplyr::ungroup() %>% dplyr::mutate(cohort = 1:dplyr::n_distinct(cohort)) %>%
    tibble::as_tibble()
}

Feel free to add this (and/or rewrite with zoo instead).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant