R/hfr_gather.R

Defines functions hfr_gather

Documented in hfr_gather

#' Reshape HFR Data frame long
#'
#' Tidy Wide or Wide-LIMITED submissions by pivoting them long and separating
#' column name into relevant parts.
#'
#' @param df HFR data frame imported via `hfr_import()`
#'
#' @export

hfr_gather <- function(df){

  #only need to gather if the data set is wide (will not have indicator as column)

    if(!var_exists(df, "val")){

      #identify data columns
        data_cols <- setdiff(names(df), template_cols_meta)

      #reshape from wide to long
        df <- df %>%
          tidyr::pivot_longer(tidyr::all_of(data_cols),
                              names_to = "ind",
                              values_to = "val",
                              values_drop_na = TRUE)

      #separate former col names into indicator & disaggregates
        df <- df %>%
          tidyr::separate(ind,
                          c("indicator", "agecoarse", "sex", "otherdisaggregate"),
                          sep = "\\.", fill = "right")
      #reorganize
        df <- dplyr::relocate(df, sex, .before = agecoarse)
    }

  return(df)

}
USAID-OHA-SI/Wavelength documentation built on March 24, 2023, 10:07 a.m.