Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to filter ZCTA results by state in get_acs #580

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Imports:
utils,
rlang,
crayon,
tidyselect
tidyselect,
zctaCrosswalk
Suggests:
ggplot2,
survey,
Expand Down
12 changes: 10 additions & 2 deletions R/acs.R
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,10 @@ get_acs <- function(geography, variables = NULL, table = NULL, cache_table = FAL
} else {
vars <- format_variables_acs(variables)

dat <- suppressWarnings(load_data_acs(geography, vars, key, year, state, county,
zcta, survey, show_call = show_call))

dat <- suppressWarnings(load_data_acs(geography, vars, key, year, state, county,
zcta, survey, show_call = show_call))

}

vars2 <- format_variables_acs(variables)
Expand Down Expand Up @@ -740,6 +742,12 @@ get_acs <- function(geography, variables = NULL, table = NULL, cache_table = FAL
dat <- dat[!duplicated(names(dat), fromLast = TRUE)]
dat <- dat[c("GEOID", "NAME", var_vector)]

}

# Filters zctas within state queried
if(!is.na(state) & geography == "zip code tabulation area"){
dat <- dat[dat$GEOID %in% zctaCrosswalk::zcta_crosswalk[zctaCrosswalk::zcta_crosswalk$state_usps == state,]$zcta,]


# Convert missing values to NA
dat[dat == -111111111] <- NA
Expand Down
13 changes: 9 additions & 4 deletions R/load_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,6 @@ load_data_acs <- function(geography, formatted_variables, key, year, state = NUL

if (!is.null(zcta)) {

# if (is.null(state)) {
# stop("The `zcta` argument requires specifying a state.", call. = FALSE)
# }

if (!is.null(county)) {
stop("ZCTAs do not nest within counties, so `county` should not be specified.",
call. = FALSE)
Expand All @@ -173,6 +169,10 @@ load_data_acs <- function(geography, formatted_variables, key, year, state = NUL

in_area <- paste0("state:", state)

if(geography == "zip code tabulation area"){
in_area <- NULL
}

} else {
in_area <- NULL
}
Expand Down Expand Up @@ -233,6 +233,11 @@ load_data_acs <- function(geography, formatted_variables, key, year, state = NUL

}

# If state is supplied for zctas, query all and filter after in get_acs
if(geography == "zip code tabulation area"){
in_area <- NULL
}

vars_to_get <- paste0(formatted_variables, ",NAME")

if (geography == "state" && !is.null(state)) {
Expand Down