Skip to content

Years as a cohort interval #5

@carrieseltzer

Description

@carrieseltzer

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions