#' Recode Ethnicity
#'
#'This function recodes ethnicity to align with VDOE practices and replaces codes with descriptions.
#'It assumes that the ethnicity variable is named "ethnic" and the hispanic variable is names "hispanic_latino"
#'
#' @param df data
#' @param source the table the data originally comes from; must be either "students" (from the RE_Students table) or "src" (from one of the SRC tables).
#'
#' @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_ethnicity()
#'
#' }
recode_ethnicity <- function(df, source = "students") {
#checking that input is a dataframe
if (!is.data.frame(df)) {
rlang::abort(paste0("`df` must be a dataframe, not", typeof(df)))
}
#check what source is
if (!source %in% c("students", "src")) {
rlang::abort(paste0("`source` must be either 'students' or 'src', not ", source))
}
if (source == "students") {
df %>%
dplyr::mutate(ethnic = dplyr::case_when(
.data$hispanic_latino == "Y" ~ "Hispanic",
.data$hispanic_latino == "N" & .data$ethnic == "01" ~ "American Indian/Alaska Native",
.data$hispanic_latino == "N" & .data$ethnic == "02" ~ "Asian",
.data$hispanic_latino == "N" & .data$ethnic == "03" ~ "Black or African American",
.data$hispanic_latino == "N" & .data$ethnic == "05" ~ "White",
.data$hispanic_latino == "N" & .data$ethnic == "06" ~ "Hawaiian or Pacific Islander",
TRUE ~ "Two or More"
))
} else {
df %>%
dplyr::mutate(race_code = dplyr::case_when(
.data$ethnic_flag == "Y" ~ "Hispanic",
.data$ethnic_flag == "N" & .data$race_code == "01" ~ "American Indian/Alaska Native",
.data$ethnic_flag == "N" & .data$race_code == "02" ~ "Asian",
.data$ethnic_flag == "N" & .data$race_code == "03" ~ "Black or African American",
.data$ethnic_flag == "N" & .data$race_code == "05" ~ "White",
.data$ethnic_flag == "N" & .data$race_code == "06" ~ "Hawaiian or Pacific Islander",
TRUE ~ "Two or More"
))
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.