Skip to content

Updated method of replacing all NAs in numeric columns #1

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

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: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# Session Data files
.RData

# Environment with secret values
.Renviron

# Example code in package build process
*-Ex.R

Expand Down
13 changes: 13 additions & 0 deletions ipd.Rproj
Original file line number Diff line number Diff line change
@@ -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
13 changes: 10 additions & 3 deletions script-2017.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ 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
min <- function(i, ..., na.rm = TRUE) {
Expand Down Expand Up @@ -429,10 +429,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)
Expand Down
12 changes: 9 additions & 3 deletions script-2018.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ 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

Expand Down Expand Up @@ -520,9 +520,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
Expand Down
11 changes: 8 additions & 3 deletions script-2019-PhilaOnly.r
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -529,9 +528,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
Expand Down
11 changes: 8 additions & 3 deletions script-2019.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -524,9 +523,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
Expand Down
11 changes: 8 additions & 3 deletions script-2020.R
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -532,9 +531,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
Expand Down