R/create_groups_df.R

Defines functions create_groups_df

#' Creates the groups data frame
#'
#' Creates the groups data frame by unnesting group columns. Leaves the nested
#' `areas` column which will be used to create the `areas_df`. Created directly
#' from the base data frame.
#'
#' Returns `NULL` if there is no `groups` column in the base data frame.
#'
#' @noRd
create_groups_df <- function(df) {
  if ("groups" %in% names(df)) {
    df <- dplyr::select(df, -dplyr::any_of("areas"))
    df <- drop_estimate_columns(df)

    if (!is.data.frame(df$groups)) {
      df <- dplyr::filter(df, !is.na(.data$groups))

      df_groups <- double_unnest(
        df = df,
        col = "groups"
      )
    } else {
      df_groups <- tidyr::unnest(
        df,
        col = "groups"
      )
    }

    df_groups <- dplyr::rename(
      .data = df_groups,
      "group_id" = "id",
      "group_name" = "name"
    )
    type_convert_silent(df_groups)
  } else {
    NULL
  }
}

Try the ripc package in your browser

Any scripts or data that you put into this service are public.

ripc documentation built on June 25, 2024, 5:08 p.m.