Skip to content

Commit

Permalink
Add na_zero parameter to collapse_acs_variables
Browse files Browse the repository at this point in the history
  • Loading branch information
elipousson committed May 22, 2024
1 parent ddc4129 commit 9267f78
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
63 changes: 43 additions & 20 deletions R/collapse_acs_variables.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
#' @param value_col,moe_col Value and margin of error column names (default to
#' "estimate" and "moe").
#' @param na.rm Passed to [sum()], Default: `TRUE`
#' @param na_zero If `TRUE`, and the collapsed sum of a MOE is 0, replaced MOE
#' value with `NA`. This is beneficial for percent estimates with the margin
#' of error falls below 1% and is rounded to 0 with the default number of
#' digits.
#' @param digits Passed to [round()], Default: 2
#' @param extensive Must be `TRUE`. If `FALSE`, summarize collapsed variables
#' using a weighted mean. This is a planned feature.
#' @param extensive Must be `TRUE`. If `FALSE` (not currently supported),
#' summarize collapsed variables using a weighted mean.
#' @examples
#' \dontrun{
#' if (interactive()) {
Expand Down Expand Up @@ -55,6 +59,7 @@ collapse_acs_variables <- function(data,
value_col = "estimate",
moe_col = "moe",
na.rm = TRUE,
na_zero = TRUE,
digits = 2,
.add = FALSE,
extensive = TRUE) {
Expand Down Expand Up @@ -107,10 +112,30 @@ collapse_acs_variables <- function(data,
value_col = value_col,
moe_col = moe_col,
na.rm = na.rm,
na_zero = na_zero,
digits = digits
)
}

moe_sum_safe <- function(moe,
estimate,
na.rm = TRUE,
na_zero = TRUE,
digits = 2) {
dplyr::if_else(
all(is.na(moe)) | (na_zero & sum(moe) == 0),
NA_real_,
round(
tidycensus::moe_sum(
moe,
estimate = estimate,
na.rm = na.rm
),
digits = digits
)
)
}

#' Summarise ACS data assuming extensive variables
#'
#' @importFrom dplyr summarise across
Expand All @@ -124,6 +149,7 @@ summarise_acs_extensive <- function(
value_col = "estimate",
moe_col = "moe",
na.rm = TRUE,
na_zero = TRUE,
digits = 2) {
perc_cols <- acs_perc_cols(
value_col = value_col,
Expand All @@ -140,12 +166,11 @@ summarise_acs_extensive <- function(
sum(.data[[value_col]], na.rm = na.rm),
digits = digits
),
"{moe_col}" := round(
tidycensus::moe_sum(
.data[[moe_col]],
estimate = .data[[value_col]],
na.rm = na.rm
),
"{moe_col}" := moe_sum_safe(
.data[[moe_col]],
.data[[value_col]],
na.rm = na.rm,
na_zero = na_zero,
digits = digits
),
dplyr::across(
Expand All @@ -169,24 +194,22 @@ summarise_acs_extensive <- function(
sum(.data[[value_col]], na.rm = na.rm),
digits = digits
),
"{moe_col}" := round(
tidycensus::moe_sum(
.data[[moe_col]],
estimate = .data[[value_col]],
na.rm = na.rm
),
"{moe_col}" := moe_sum_safe(
.data[[moe_col]],
.data[[value_col]],
na.rm = na.rm,
na_zero = na_zero,
digits = digits
),
"{perc_cols[[1]]}" := round(
sum(.data[[perc_cols[[1]]]], na.rm = na.rm),
digits = digits
),
"{perc_cols[[2]]}" := round(
tidycensus::moe_sum(
.data[[perc_cols[[2]]]],
estimate = .data[[perc_cols[[1]]]],
na.rm = na.rm
),
"{perc_cols[[2]]}" := moe_sum_safe(
.data[[perc_cols[[2]]]],
.data[[perc_cols[[1]]]],
na.rm = na.rm,
na_zero = na_zero,
digits = digits
),
dplyr::across(
Expand Down
10 changes: 8 additions & 2 deletions man/collapse_acs_variables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9267f78

Please sign in to comment.