Skip to content
Merged
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
2 changes: 1 addition & 1 deletion facebook/delphiFacebook/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ Suggests:
testthat (>= 1.0.1),
covr (>= 2.2.2)
LinkingTo: Rcpp
RoxygenNote: 7.1.1
RoxygenNote: 7.2.0
Encoding: UTF-8
2 changes: 2 additions & 0 deletions facebook/delphiFacebook/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ importFrom(dplyr,bind_rows)
importFrom(dplyr,case_when)
importFrom(dplyr,coalesce)
importFrom(dplyr,desc)
importFrom(dplyr,distinct)
importFrom(dplyr,everything)
importFrom(dplyr,filter)
importFrom(dplyr,full_join)
Expand Down Expand Up @@ -115,6 +116,7 @@ importFrom(stats,setNames)
importFrom(stats,weighted.mean)
importFrom(stringi,stri_extract)
importFrom(stringi,stri_extract_first)
importFrom(stringi,stri_pad)
importFrom(stringi,stri_replace)
importFrom(stringi,stri_replace_all)
importFrom(stringi,stri_split)
Expand Down
51 changes: 45 additions & 6 deletions facebook/delphiFacebook/R/contingency_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#' @export
write_contingency_tables <- function(data, params, geo_type, groupby_vars, theme = NULL) {
if (!is.null(data) && nrow(data) != 0) {

# Reorder the group-by columns and sort the dataset by them.
groupby_vars <- c("geo_id", sort(setdiff(groupby_vars, "geo_id")))
data <- data %>%
Expand Down Expand Up @@ -61,8 +60,9 @@ write_contingency_tables <- function(data, params, geo_type, groupby_vars, theme
#' @param params A parameters object with the `static_dir` resources folder.
#' @param geo_type "nation", "state".
#'
#' @importFrom dplyr bind_cols left_join select
#' @importFrom dplyr bind_cols left_join select distinct mutate
#' @importFrom readr read_csv cols
#' @importFrom stringi stri_pad
#' @noRd
add_geo_vars <- function(data, params, geo_type) {

Expand All @@ -75,7 +75,6 @@ add_geo_vars <- function(data, params, geo_type) {
)

if (geo_type == "nation") {

rest <- data.frame(
region = overall,
GID_1 = NA_character_,
Expand All @@ -86,7 +85,6 @@ add_geo_vars <- function(data, params, geo_type) {
)

} else if (geo_type == "state") {

states <- read_csv(
file.path(params$static_dir, "state_list.csv"),
col_types = cols(.default = "c")
Expand All @@ -109,15 +107,56 @@ add_geo_vars <- function(data, params, geo_type) {
.data$county_fips
)
} else if (geo_type == "county") {
warning("county metadata not supported")
counties <- read_csv(
file.path(params$static_dir, "02_20_uszips.csv"),
col_types = cols(.default = "c")
) %>%
mutate(
fips = stri_pad(.data$fips, 5, pad="0")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually use sprintf("%05d", .data$fips), but I guess you've already imported stri so this is fine (and probably easier to understand)

) %>%
select(fips, county_name, state_id, state_name) %>%
distinct()

rest <- data.frame(
county_fips = data$geo_id
)

rest <- left_join(rest, counties, by = c("county_fips" = "fips")) %>%
select(
region = .data$state_id,
state = .data$state_id,
county = .data$county_name,
county_fips = .data$county_fips
)

# Fill in state fips and GID_1
states <- read_csv(
file.path(params$static_dir, "state_list.csv"),
col_types = cols(.default = "c")
)

rest <- left_join(rest, states, by = "state") %>%
select(
.data$region,
.data$GID_1,
.data$state,
.data$state_fips,
.data$county,
.data$county_fips
)
}

geo_vars <- bind_cols(first, rest)

# Insert the geographic variables in place of the "geo_id" variable.
index <- which(names(data) == "geo_id")
before <- if (index > 1) data[, 1:(index-1)] else NULL
after <- data[, (index+1):ncol(data)]

if (ncol(data) == 1) {
after <- NULL
} else {
after <- select(data, (index+1):ncol(data))
}
result <- bind_cols(before, geo_vars, after)

return(result)
Expand Down