R/recode_demos.R

Defines functions recode_demos

Documented in recode_demos

#' Recode Demographics
#'
#' This will recode special education status, ell status, and gifted status.
#' The function assumes that janitor::clean_names() has already been run and that the names
#' of the demographic variables have not otherwise been changed
#'
#'
#' @param df data
#' @param ell_source the source of the ell data; either 'students' or 'ell_tbl'. If 'students', the column holding the data to be recoded is assumed to be named 'ell_status.' If 'ell_tbl,' the column holding the data to be recoded is assumed to be named 'prof_level'. Both recode the data into a variable named 'ell_status'.
#'
#' @return
#' @export
#'
#' @importFrom magrittr %>%
#'
#' @examples \dontrun{
#' library(tidyverse)
#' library(janitor)
#' library(ccpsr)
#'
#' con <- set_con()
#'
#' ex <- odbc::dbGetQuery(con, 'SELECT *
#' FROM [CCPS_Shared].[CCPS_Shared].[RE_Students]
#' WHERE School_Year = 2019')
#'
#' ex %>%
#' clean_names() %>%
#' recode_demos()
#'
#' }
recode_demos <- function(df, ell_source = "students") {

  #checking that input is a dataframe
  if (!is.data.frame(df)) {
    rlang::abort(paste0("`df` must be a dataframe, not", typeof(df)))
  }

  if (!ell_source %in% c("students", "ell_tbl")) {
    rlang::abort(paste0("`ell_source` must be either 'students' or 'ell_tbl', not ", ell_source))
  }

  tmp <- df %>%
    dplyr::mutate(indicator_speced = dplyr::if_else(.data$indicator_speced == "N", "Not_SPED", "SPED"),
                  ccps_gate = dplyr::if_else(.data$ccps_gate == "Y", "Gifted", "Not_Gifted"),
                  sex = dplyr::case_when(.data$sex == "M" ~ "Male",
                                         .data$sex == "F" ~ "Female",
                                         .data$sex == "X" ~ "Nonbinary",
                                         TRUE ~ NA_character_))

  if (ell_source == "students") {

    tmp %>%
      dplyr::mutate(ell_status = dplyr::if_else(.data$ell_status == "N", "Not_ELL", "ELL"))

  } else {

    tmp %>%
      dplyr::mutate(ell_status = dplyr::case_when(
        .data$prof_level %in% c("1", "2", "3", "4", "5") ~ "ELL",
        .data$prof_level %in% c("6", "7", "8", "9", "11", "F") ~ "Monitor_or_Former_ELL",
        TRUE ~ "Not_ELL"
      ))

  }

}
ekholme-ccps/ccpsr documentation built on Aug. 17, 2021, 10:01 p.m.