Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. library(magrittr)
  2. library(dplyr)
  3. library(tidyr)
  4. library(stringr)
  5.  
  6. library(readr)
  7.  
  8. # Import the race/hispanic-origin ACS data downloaded through American Fact-Finder ----
  9. b03002_data <- read_csv(file = "Data/ACS/ZCTA/2013-2017/Raw/B03002/ACS_17_5YR_B03002_with_ann.csv",
  10. skip = 1L,
  11. col_types = cols(
  12. .default = col_integer(),
  13. Id = col_character(),
  14. Id2 = col_character(),
  15. Geography = col_character()
  16. ))
  17.  
  18. # Tidy the data to a more user-friendly format
  19. b03002_data %<>%
  20. gather(key = "Measure_Name", value = "Value",
  21. -one_of(c("Race", "Id", "Id2", "Geography"))) %>%
  22. mutate(Measure_Type = case_when(str_detect(Measure_Name, "^Estimate") ~ "Estimate",
  23. str_detect(Measure_Name, "^Margin") ~ "Margin of Error"),
  24. Subpopulation = str_remove(Measure_Name, "(Estimate;)|(Margin of Error;)") %>%
  25. str_trim() %>% str_remove("(:|;)$")) %>%
  26. select(-Measure_Name) %>%
  27. spread(key = "Measure_Type", value = "Value") %>%
  28. rename_all(.funs = function(x) str_replace_all(x, "[[:space:]]+", "_"))
  29.  
  30. b03002_data %<>%
  31. mutate(Hispanic_or_Latino_Status = case_when(str_detect(Subpopulation, "^Hispanic or Latino") ~ "Hispanic or Latino",
  32. str_detect(Subpopulation, "^Not Hispanic or Latino") ~ "Not Hispanic or Latino",
  33. str_detect(Subpopulation, "^Total") ~ "Total")) %>%
  34. mutate(Race = case_when(Subpopulation == "Hispanic or Latino" ~ "Total",
  35. Subpopulation == "Not Hispanic or Latino" ~ "Total",
  36. TRUE ~ str_remove(Subpopulation, "^(Not |)Hispanic or Latino: -") %>% str_trim)) %>%
  37. select(-Subpopulation)
  38.  
  39. b03002_data %<>%
  40. filter(!Race %in% c("Two or more races: - Two races excluding Some other race, and three or more races",
  41. "Two or more races: - Two races including Some other race"))
  42.  
  43. # Obtain easy-to-read table which separates all Hispanic/Latino persons
  44. # into a distinct category
  45. # (i.e. the 'Black or African-American' category excludes persons who are also Hispanic-Latino)
  46.  
  47. total_population_sizes_by_race_or_hisp_latino_status <- b03002_data %>%
  48. filter((Hispanic_or_Latino_Status == "Hispanic or Latino" & Race == "Total")
  49. |
  50. (Hispanic_or_Latino_Status == "Not Hispanic or Latino" & Race != "Total")) %>%
  51. mutate(Race_or_Hispanic_Latino_Status = case_when(Hispanic_or_Latino_Status == "Hispanic or Latino" ~ "Hispanic or Latino",
  52. TRUE ~ Race)) %>%
  53. select(-Race, - Hispanic_or_Latino_Status) %>%
  54. select(Id, Id2, Geography, Race_or_Hispanic_Latino_Status,
  55. Estimate, Margin_of_Error)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement