From 38a7e2f2424ac10a14558f6b541bb81a56a6e650 Mon Sep 17 00:00:00 2001 From: Jared Knowles Date: Sun, 16 Oct 2022 17:53:59 -0400 Subject: [PATCH] rerun doco --- DESCRIPTION | 2 +- NAMESPACE | 3 ++ R/utils.R | 72 +++++++++++++++++++++++++++++++++++++++ man/grade_level_to_num.Rd | 17 +++++++++ man/race_short_names.Rd | 17 +++++++++ man/star_subs.Rd | 26 ++++++++++++++ 6 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 man/grade_level_to_num.Rd create mode 100644 man/race_short_names.Rd create mode 100644 man/star_subs.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 313e21e..75d9eca 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,4 +21,4 @@ Encoding: UTF-8 LazyData: true Suggests: testthat -RoxygenNote: 7.1.0 +RoxygenNote: 7.1.1 diff --git a/NAMESPACE b/NAMESPACE index 9a581ce..291af75 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ export(countNA) export(dbSafeNames) export(findDots) export(get_png) +export(grade_level_to_num) export(has_caption) export(make_logo_grob) export(measure_caption) @@ -16,8 +17,10 @@ export(nvals) export(plot_jpeg) export(pretty_count) export(pretty_per) +export(race_short_names) export(safe_max) export(simpleCap) +export(star_subs) export(theme_civilytics) import(ggplot2) importFrom(ggplot2,theme) diff --git a/R/utils.R b/R/utils.R index a73f34f..f56d67a 100644 --- a/R/utils.R +++ b/R/utils.R @@ -73,3 +73,75 @@ pretty_count <- function(x) { x <- prettyNum(x, big.mark = ",") return(x) } + + + +#' Unsuppress data using sampling +#' +#' @param x +#' @param replace_char the character you want to replace in the vector +#' @param zeros the number of zeroes to oversample when replacing replace_char +#' @param max_value the numeric maximum value the replacement for the "*" can be +#' @return a numeric vector with no characters representing suppressed values +#' @export +#' +#' @examples +#' suppr_data <- c("2", "8", "*", "*", "7", "9", "100") +#' star_subs(suppr_data, zeros = 1, max_value = 10) +#' star_subs(suppr_data, zeros = 1, max_value = 200) +star_subs <- function(x, replace_char = "*", + zeros = 15, max_value = 20) { + x[x == "*"] <- sample(c(rep("0", zeros), as.character(0:max_value)), + sum(x==replace_char), replace = TRUE) + x <- as.numeric(x) + return(x) +} + + + +#' Recode grade level from character to numeric +#' +#' @param x character description of grade levels from NCES style data +#' +#' @return +#' @export +#' +#' @examples +grade_level_to_num <- function(x) { + # Cannot generate new levels if it is a factor so we coerce to character first + x <- as.character(x) + x[x %in% c("KG", "Kindergarten")] <- "0" + x[x %in% c("Pre-K", "Pre-k", "Preschool", "Pre-Kindergarten")] <- "-1" + x[x %in% c("Adult", "Adult Education")] <- "13" + y <- as.numeric(x) + return(y) +} + + +#' Recode NCES race categories to shorter names +#' +#' @param x a character vector with NCES race codes, often from Urban Institute +#' +#' @return +#' @export +#' +#' @examples +race_short_names <- function(x) { + x <- as.character(x) + x[x %in% c("Black", "Black Or African American", "Black or African American", + "African American")] <- "black" + x[x %in% c("Hispanic", "Hispanic Or Latino", "Hispanic or Latino")] <- + "hisp_lat" + x[x %in% c("White", "white", "White and Not Hispanic")] <- "white" + x[x %in% c("Asian", "Asian American")] <- "asian" + x[x %in% c("Two Or More Races", "Two or More Races")] <- "two_or_more" + x[x %in% c("Native Hawaiian Or Other Pacific Islander", + "Native Hawaiian or Other Pacific Islander", + "Native Hawaiian Pacific Islander")] <- "native_haw" + x[x %in% c("American Indian", "American Indian Or Alaska Native", + "American Indian or Alaska Native", "American Indian or Native Alaskan")] <- "amind" + x[x %in% c("Not Reported")] <- "other" + return(x) +} + + diff --git a/man/grade_level_to_num.Rd b/man/grade_level_to_num.Rd new file mode 100644 index 0000000..0a2e7d7 --- /dev/null +++ b/man/grade_level_to_num.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{grade_level_to_num} +\alias{grade_level_to_num} +\title{Recode grade level from character to numeric} +\usage{ +grade_level_to_num(x) +} +\arguments{ +\item{x}{character description of grade levels from NCES style data} +} +\value{ + +} +\description{ +Recode grade level from character to numeric +} diff --git a/man/race_short_names.Rd b/man/race_short_names.Rd new file mode 100644 index 0000000..f1eebad --- /dev/null +++ b/man/race_short_names.Rd @@ -0,0 +1,17 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{race_short_names} +\alias{race_short_names} +\title{Recode NCES race categories to shorter names} +\usage{ +race_short_names(x) +} +\arguments{ +\item{x}{a character vector with NCES race codes, often from Urban Institute} +} +\value{ + +} +\description{ +Recode NCES race categories to shorter names +} diff --git a/man/star_subs.Rd b/man/star_subs.Rd new file mode 100644 index 0000000..abb0b27 --- /dev/null +++ b/man/star_subs.Rd @@ -0,0 +1,26 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{star_subs} +\alias{star_subs} +\title{Unsuppress data using sampling} +\usage{ +star_subs(x, replace_char = "*", zeros = 15, max_value = 20) +} +\arguments{ +\item{replace_char}{the character you want to replace in the vector} + +\item{zeros}{the number of zeroes to oversample when replacing replace_char} + +\item{max_value}{the numeric maximum value the replacement for the "*" can be} +} +\value{ +a numeric vector with no characters representing suppressed values +} +\description{ +Unsuppress data using sampling +} +\examples{ +suppr_data <- c("2", "8", "*", "*", "7", "9", "100") +star_subs(suppr_data, zeros = 1, max_value = 10) +star_subs(suppr_data, zeros = 1, max_value = 200) +}