From 57f538dc6dbf48b40ed8141618bcc39e73d4dffa Mon Sep 17 00:00:00 2001 From: Grant Engel Date: Mon, 16 May 2022 11:21:28 -0400 Subject: [PATCH 1/2] Updated method of replacing all NAs in numeric columns to most current dplyr method --- ipd.Rproj | 13 +++++++++++++ script-2017.R | 10 +++++++++- script-2018.R | 9 ++++++++- script-2019-PhilaOnly.r | 8 +++++++- script-2019.R | 8 +++++++- script-2020.R | 8 +++++++- 6 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 ipd.Rproj diff --git a/ipd.Rproj b/ipd.Rproj new file mode 100644 index 0000000..8e3c2eb --- /dev/null +++ b/ipd.Rproj @@ -0,0 +1,13 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX diff --git a/script-2017.R b/script-2017.R index 4b38b83..5c3ed9d 100755 --- a/script-2017.R +++ b/script-2017.R @@ -43,6 +43,7 @@ ipd_counties <- c("34005", "34007", "34015", "34021", # census_api_key("YOUR API KEY GOES HERE", install = TRUE) census_api_key("Insert API Key Here", overwrite = TRUE) + # Functions min <- function(i, ..., na.rm = TRUE) { base::min(i, ..., na.rm = na.rm) @@ -429,10 +430,17 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) + # Bin break points breaks <- ipd_summary %>% select(ends_with("PctEst")) export_breaks <- round(mapply(st_dev_breaks, x = breaks, i = 5, na.rm = TRUE), digits = 3) diff --git a/script-2018.R b/script-2018.R index a3eb0a5..6ac10c0 100755 --- a/script-2018.R +++ b/script-2018.R @@ -10,6 +10,7 @@ library(tidycensus); library(tidyverse); library(tigris) # census_api_key("YOUR API KEY GOES HERE", install = TRUE) census_api_key("Insert API Key Here", overwrite = TRUE) + # Fields # See https://www.census.gov/data/developers/data-sets/acs-5year.html @@ -520,9 +521,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points diff --git a/script-2019-PhilaOnly.r b/script-2019-PhilaOnly.r index e192f54..fbb6189 100644 --- a/script-2019-PhilaOnly.r +++ b/script-2019-PhilaOnly.r @@ -529,9 +529,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points diff --git a/script-2019.R b/script-2019.R index 98956e9..5eb78f7 100644 --- a/script-2019.R +++ b/script-2019.R @@ -524,9 +524,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points diff --git a/script-2020.R b/script-2020.R index 927419c..5fe766b 100644 --- a/script-2020.R +++ b/script-2020.R @@ -532,9 +532,15 @@ export_counts$Classification <- factor(export_counts$Classification, "Well Above Average", "NoData")) export_counts <- arrange(export_counts, Variable, Classification) + +# 2022-04-27 # replacing mutate_all with across, which supercedes it in dplyr +# was causing issues because replace_na was trying to apply to "Variable" column, +# a character data type +# across + where allows us to apply the function replace_na to just numeric columns export_counts <- export_counts %>% spread(Classification, Count) %>% - mutate_all(~(replace_na(., 0))) %>% + # mutate_all(~(replace_na(., 0))) %>% + mutate(across(where(is.numeric), ~replace_na(., 0))) %>% mutate(TOTAL = rowSums(.[2:7], na.rm = TRUE)) # Bin break points From a3ac78b2d35f8b100df34a085505b901689f2c21 Mon Sep 17 00:00:00 2001 From: Grant Engel Date: Mon, 16 May 2022 12:33:56 -0400 Subject: [PATCH 2/2] Implementing retrieval of Census API Key from .Renviron file --- .gitignore | 3 +++ script-2017.R | 3 +-- script-2018.R | 3 +-- script-2019-PhilaOnly.r | 3 +-- script-2019.R | 3 +-- script-2020.R | 3 +-- 6 files changed, 8 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 4ff5907..0e958ce 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,9 @@ # Session Data files .RData +# Environment with secret values +.Renviron + # Example code in package build process *-Ex.R diff --git a/script-2017.R b/script-2017.R index 5c3ed9d..f9cd70b 100755 --- a/script-2017.R +++ b/script-2017.R @@ -40,8 +40,7 @@ ipd_states <- c("NJ", "PA") ipd_counties <- c("34005", "34007", "34015", "34021", "42017", "42029", "42045", "42091", "42101") # Census API Key -# census_api_key("YOUR API KEY GOES HERE", install = TRUE) -census_api_key("Insert API Key Here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Functions diff --git a/script-2018.R b/script-2018.R index 6ac10c0..75673e1 100755 --- a/script-2018.R +++ b/script-2018.R @@ -7,8 +7,7 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris) # Census API Key -# census_api_key("YOUR API KEY GOES HERE", install = TRUE) -census_api_key("Insert API Key Here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Fields diff --git a/script-2019-PhilaOnly.r b/script-2019-PhilaOnly.r index fbb6189..acfe64a 100644 --- a/script-2019-PhilaOnly.r +++ b/script-2019-PhilaOnly.r @@ -8,8 +8,7 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris); library(dplyr); library(descr) # Census API Key - -census_api_key("", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Fields diff --git a/script-2019.R b/script-2019.R index 5eb78f7..325ed63 100644 --- a/script-2019.R +++ b/script-2019.R @@ -8,8 +8,7 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris); library(dplyr); library(descr) # Census API Key - -census_api_key("API_Key_Goes_Here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Fields diff --git a/script-2020.R b/script-2020.R index 5fe766b..3356a5d 100644 --- a/script-2020.R +++ b/script-2020.R @@ -8,8 +8,7 @@ library(plyr); library(here); library(sf); library(summarytools); library(tidycensus); library(tidyverse); library(tigris); library(dplyr); library(descr) # Census API Key - -census_api_key("insert_api_key_here", overwrite = TRUE) +census_api_key(Sys.getenv("CENSUS_API_KEY"), overwrite = TRUE) # Fields