```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(mapboxapi)
library(tidyverse)
library(tigris)
library(sf)
library(leaflet)
library(censusapi)
options(scipen = 999)

Sys.setenv(CENSUS_KEY="c8aa67e4086b4b5ce3a8717f59faa9a28f611dab")

acs_vars_2019_5yr <- readRDS("acs_vars_2019_5yr.rds")
```

Update these variables to point to a new city. Only slight modification necessary to switch to non-city boundaries, just manually change `boundary`.

Only other update to make is to the filename of the .rda file saved in the last chunk.

```{r}
city <- "East Palo Alto"
state_abbr <- "CA"
state <- "06"
county <- "081"
```

```{r}
boundary <- places(state, cb = T, progress_bar = F) %>% 
  filter(NAME == city) %>% 
  select(geometry) %>% 
  st_transform(26910)

cbgs <- 
  block_groups(state, county, cb = T, progress_bar = F) %>%
  st_transform(26910) %>% 
  .[boundary, ] %>%
  st_buffer(-10) %>% 
  .[boundary, ] %>%
  st_set_geometry(NULL) %>% 
  left_join(
    block_groups(state, county, cb = T, progress_bar = F) %>% select(GEOID)
  ) %>% 
  st_as_sf() %>% 
  select(CBG = GEOID) %>% 
  arrange(CBG) %>% 
  left_join(
    getCensus(
      name = "acs/acs5",
      vintage = 2018,
      region = "block group:*",
      regionin = paste0("state:",state,"+county:",county),
      vars = "B01001_001E"
    ) %>% 
      transmute(
        CBG = paste0(state,county,tract,block_group),
        POP = B01001_001E
      )
  ) %>% 
  st_transform(26910) %>% 
  mutate(original_area = st_area(.))
```

```{r}
covid_testing <-
  st_read("https://opendata.arcgis.com/datasets/11fe8f374c344549815a716c8472832f_0.geojson")

vaccine_sites <- covid_testing %>%
  .[cbgs %>% st_buffer(5000) %>% 
  st_transform(4326), ] %>% 
  filter(status == "Open")

walk <- mb_isochrone(
  vaccine_sites,
  profile = "walking",
  time = 10
)

bike <- mb_isochrone(
  vaccine_sites,
  profile = "cycling",
  time = 10
)

drive <- mb_isochrone(
  vaccine_sites,
  profile = "driving",
  time = 5
)

vaccine_access <-
  vaccine_sites %>% 
  as.data.frame() %>% 
  rename(location = geometry) %>% 
  cbind(walk$geometry) %>% 
  rename(walk = geometry) %>% 
  cbind(bike$geometry) %>% 
  rename(bike = geometry) %>% 
  cbind(drive$geometry) %>% 
  rename(drive = geometry) %>% 
  separate(fulladdr, c("address1","address2"), sep = ",", extra = "merge")

vaccine_walk <- 
  vaccine_access %>% 
  st_set_geometry("walk") %>% 
  summarize()

vaccine_bike <- 
  vaccine_access %>% 
  st_set_geometry("bike") %>% 
  summarize()

vaccine_drive <-
  vaccine_access %>% 
  st_set_geometry("drive") %>% 
  summarize()
```

```{r}
cbgs_city_intersect <-
  cbgs %>% 
  st_intersection(boundary) %>% 
  mutate(
    leftover_area = st_area(.),
    city_perc_area = leftover_area / original_area
  ) %>% 
  st_drop_geometry() %>% 
  select(CBG, city_perc_area)

cbgs_walk_intersect <-
  cbgs %>% 
  st_intersection(
    vaccine_access %>% 
      st_set_geometry("walk") %>% 
      st_union() %>% 
      st_transform(26910)
  ) %>% 
  mutate(
    leftover_area = st_area(.),
    walk_perc_area = leftover_area / original_area
  ) %>% 
  st_drop_geometry() %>% 
  select(CBG, walk_perc_area)

cbgs_walk_intersect_city <-
  cbgs %>% 
  st_intersection(boundary) %>% 
  st_intersection(
    vaccine_access %>% 
      st_set_geometry("walk") %>% 
      st_union() %>% 
      st_transform(26910)
  ) %>% 
  mutate(
    leftover_area = st_area(.),
    walk_perc_area = leftover_area / original_area
  ) %>% 
  st_drop_geometry() %>% 
  select(CBG, walk_perc_area)

cbgs_bike_intersect <-
  cbgs %>% 
  st_intersection(
    vaccine_access %>% 
      st_set_geometry("bike") %>% 
      st_union() %>% 
      st_transform(26910)
  ) %>% 
  mutate(
    leftover_area = st_area(.),
    bike_perc_area = leftover_area / original_area
  ) %>% 
  st_drop_geometry() %>% 
  select(CBG, bike_perc_area)

cbgs_bike_intersect_city <-
  cbgs %>% 
  st_intersection(boundary) %>% 
  st_intersection(
    vaccine_access %>% 
      st_set_geometry("bike") %>% 
      st_union() %>% 
      st_transform(26910)
  ) %>% 
  mutate(
    leftover_area = st_area(.),
    bike_perc_area = leftover_area / original_area
  ) %>% 
  st_drop_geometry() %>% 
  select(CBG, bike_perc_area)

cbgs_drive_intersect <-
  cbgs %>% 
  st_intersection(
    vaccine_access %>% 
      st_set_geometry("drive") %>% 
      st_union() %>% 
      st_transform(26910)
  ) %>% 
  mutate(
    leftover_area = st_area(.),
    drive_perc_area = leftover_area / original_area
  ) %>% 
  st_drop_geometry() %>% 
  select(CBG, drive_perc_area)

cbgs_drive_intersect_city <-
  cbgs %>% 
  st_intersection(boundary) %>% 
  st_intersection(
    vaccine_access %>% 
      st_set_geometry("drive") %>% 
      st_union() %>% 
      st_transform(26910)
  ) %>% 
  mutate(
    leftover_area = st_area(.),
    drive_perc_area = leftover_area / original_area
  ) %>% 
  st_drop_geometry() %>% 
  select(CBG, drive_perc_area)

cbgs_isochrone_intersect <-
  cbgs_city_intersect %>% 
  left_join(cbgs_walk_intersect_city) %>% 
  left_join(cbgs_bike_intersect_city) %>% 
  left_join(cbgs_drive_intersect_city)

cbgs_access <- 
  cbgs_city_intersect %>% 
  left_join(cbgs_walk_intersect) %>% 
  left_join(cbgs_bike_intersect) %>% 
  left_join(cbgs_drive_intersect) %>% 
  transmute(
    CBG,
    WALK = walk_perc_area,
    BIKE = bike_perc_area,
    DRIVE = drive_perc_area
  ) %>% 
  pivot_longer(
    -CBG,
    names_to = "Names",
    values_to = "Values"
  ) %>% 
  mutate(
    Values = ifelse(
      is.na(Values),
      0,
      Values * 100
    ),
    year = 2019
  )
```


```{r}
cbgs_acs_2019 <-
  getCensus(
    name = "acs/acs5",
    vintage = 2019,
    region = "block group:*",
    regionin = paste0("state:",state,"+county:",county),
    vars = c(
      "B17010_001E", # Families
      "B17010_002E", # Income in the past 12 months below poverty level
      "B23025_001E", # Population 16 years and over
      "B23025_005E", # Unemployed
      "B19301_001E", # Per capita income in the past 12 months (in 2018 inflation-adjusted dollars)
      "B15003_001E", # Population 25 years and over
      "B15003_002E", # Less than high school graduate
      "B15003_003E",
      "B15003_004E",
      "B15003_005E",
      "B15003_006E",
      "B15003_007E",
      "B15003_008E",
      "B15003_009E",
      "B15003_010E",
      "B15003_011E",
      "B15003_012E",
      "B15003_013E",
      "B15003_014E",
      "B15003_015E",
      "B15003_016E",
      "B01001_001E", # Population
      "B01001_020E", # 65 years and over
      "B01001_021E", 
      "B01001_022E", 
      "B01001_023E", 
      "B01001_024E", 
      "B01001_025E", 
      "B01001_044E", 
      "B01001_045E", 
      "B01001_046E", 
      "B01001_047E", 
      "B01001_048E", 
      "B01001_049E", 
      "B01001_003E", # Under 18 years
      "B01001_004E", 
      "B01001_005E", 
      "B01001_006E", 
      "B01001_027E", 
      "B01001_028E", 
      "B01001_029E", 
      "B01001_030E", 
      "C21007_001E", # Civilian population 18 years and over for whom poverty status is determined
      "C21007_005E", # With a disability
      "C21007_008E", 
      "C21007_012E", 
      "C21007_015E",
      "C21007_020E", 
      "C21007_023E",
      "C21007_027E", 
      "C21007_030E",
      "B11003_001E", # Families
      "B11003_010E", # One parent, children under 18 years
      "B11003_016E",
      "B03002_003E", # Not Hispanic or Latino + White alone
      "B16004_001E", # Population 5 years and over
      "B16004_007E", # Speak English less than well
      "B16004_008E", 
      "B16004_012E",
      "B16004_013E",
      "B16004_017E",
      "B16004_018E",
      "B16004_022E",
      "B16004_023E",
      "B16004_029E",
      "B16004_030E",
      "B16004_034E",
      "B16004_035E",
      "B16004_039E",
      "B16004_040E",
      "B16004_044E",
      "B16004_045E",
      "B16004_051E",
      "B16004_052E",
      "B16004_056E",
      "B16004_057E",
      "B16004_061E",
      "B16004_062E",
      "B16004_066E",
      "B16004_067E",
      "B25024_001E", # Housing units
      "B25024_007E", # 10 or more
      "B25024_008E", 
      "B25024_009E", 
      "B25024_010E", # Mobile homes
      "B25014_001E", # Occupied housing units
      "B25014_005E", # Greater than 1 occupant per room
      "B25014_006E", 
      "B25014_007E", 
      "B25014_011E", 
      "B25014_012E", 
      "B25014_013E", 
      "B25044_003E", # No vehicle available
      "B25044_010E",
      "B09019_026E", # In group quarters
      "B27010_001E", # Civilian noninstitutionalized population
      "B27010_004E", # Health insurance coverage
      "B27010_005E",
      "B27010_006E",
      "B27010_007E",
      "B27010_011E",
      "B27010_012E",
      "B27010_013E",
      "B27010_017E", 
      "B27010_020E",
      "B27010_021E",
      "B27010_022E",
      "B27010_023E",
      "B27010_027E",
      "B27010_028E",
      "B27010_029E",
      "B27010_033E", 
      "B27010_036E",
      "B27010_037E",
      "B27010_038E",
      "B27010_039E",
      "B27010_043E",
      "B27010_044E",
      "B27010_045E",
      "B27010_046E",
      "B27010_050E", 
      "B27010_053E",
      "B27010_054E",
      "B27010_055E",
      "B27010_059E",
      "B27010_060E",
      "B27010_061E",
      "B27010_062E",
      "B27010_066E",
      "B19001_001E",
      "B19001_002E",
      "B19001_003E",
      "B19001_004E",
      "B19001_005E",
      "B28008_001E", # Population in households
      "B28008_004E", # With broadband
      "B22010_001E", # Households
      "B22010_002E", # Received SNAP in past 12 months
      "B25070_001E", # Renter-occupied housing units
      "B25070_007E", # Rent burden above 30%
      "B25070_008E",
      "B25070_009E",
      "B25070_010E",
      "B25091_001E", # Owner-occupied housing units
      "B25091_008E", # Owner burden above 30%
      "B25091_009E",
      "B25091_010E",
      "B25091_011E",
      "B25091_019E",
      "B25091_020E",
      "B25091_021E",
      "B25091_022E",
      "B19057_002E" # With public assistance income
    )
  ) %>% 
  mutate(
    CBG = paste0(state,county,tract,block_group)
  ) %>% 
  filter(CBG %in% cbgs$CBG) %>% 
  transmute(
    CBG,
    EP_POV = B17010_002E/B17010_001E*100,
    EP_UNEMP = B23025_005E/B23025_001E*100,
    EP_PCI = ifelse(
      B19301_001E > 0,
      B19301_001E,
      NA
    ),
    EP_NOHSDP = (
      B15003_002E +
      B15003_003E +
      B15003_004E +
      B15003_005E +
      B15003_006E +
      B15003_007E +
      B15003_008E +
      B15003_009E +
      B15003_010E +
      B15003_011E +
      B15003_012E +
      B15003_013E +
      B15003_014E +
      B15003_015E +
      B15003_016E
    )/B15003_001E*100,
    EP_AGE65 = (
      B01001_020E +
      B01001_021E +
      B01001_022E +
      B01001_023E +
      B01001_024E +
      B01001_025E +
      B01001_044E +
      B01001_045E +
      B01001_046E +
      B01001_047E +
      B01001_048E +
      B01001_049E
    )/B01001_001E*100,
    EP_AGE17 = (
      B01001_003E +
      B01001_004E +
      B01001_005E +
      B01001_006E +
      B01001_027E +
      B01001_028E +
      B01001_029E +
      B01001_030E
    )/B01001_001E*100,
    EP_DISABL = (
      C21007_005E +
      C21007_008E +
      C21007_012E +
      C21007_015E +
      C21007_020E +
      C21007_023E +
      C21007_027E +
      C21007_030E
    )/C21007_001E*100,
    EP_SNGPNT = (
      B11003_010E +
      B11003_016E
    )/B11003_001E*100,
    EP_MINRTY = (1-B03002_003E/B01001_001E)*100,
    EP_LIMENG = (
      B16004_007E +
      B16004_008E +
      B16004_012E +
      B16004_013E +
      B16004_017E +
      B16004_018E +
      B16004_022E +
      B16004_023E +
      B16004_029E +
      B16004_030E +
      B16004_034E +
      B16004_035E +
      B16004_039E +
      B16004_040E +
      B16004_044E +
      B16004_045E +
      B16004_051E +
      B16004_052E +
      B16004_056E +
      B16004_057E +
      B16004_061E +
      B16004_062E +
      B16004_066E +
      B16004_067E
    )/B16004_001E*100,
    EP_MUNIT = (
      B25024_007E +
      B25024_008E +
      B25024_009E
    )/B25024_001E*100,
    EP_MOBILE = B25024_010E/B25024_001E*100,
    EP_CROWD = (
      B25014_005E +
      B25014_006E +
      B25014_007E +
      B25014_011E +
      B25014_012E +
      B25014_013E
    )/B25014_001E*100,
    EP_NOVEH = (
      B25044_003E +
      B25044_010E
    )/B25014_001E*100,
    EP_GROUPQ = B09019_026E/B01001_001E,
    EP_MEDICARE = (
      B27010_006E +
      B27010_012E +
      B27010_013E +
      B27010_022E +
      B27010_028E +
      B27010_029E +
      B27010_038E +
      B27010_044E +
      B27010_045E +
      B27010_046E +
      B27010_055E +
      B27010_060E +
      B27010_061E +
      B27010_062E
    )/B27010_001E*100,
    EP_MEDICAID = (
      B27010_007E +
      B27010_013E +
      B27010_023E +
      B27010_029E +
      B27010_039E +
      B27010_046E +
      B27010_062E
    )/B27010_001E*100,
    EP_DIRECTINS = (
      B27010_005E +
      B27010_011E +
      B27010_021E +
      B27010_027E +
      B27010_037E +
      B27010_043E +
      B27010_045E +
      B27010_054E +
      B27010_059E +
      B27010_061E
    )/B27010_001E*100,
    EP_EMPLOYINS = (
      B27010_004E +
      B27010_011E +
      B27010_012E +
      B27010_020E +
      B27010_027E +
      B27010_028E +
      B27010_036E +
      B27010_043E +
      B27010_044E +
      B27010_053E +
      B27010_059E +
      B27010_060E
    )/B27010_001E*100,
    EP_UNINSUR = (
      B27010_017E +
      B27010_033E +
      B27010_050E +
      B27010_066E
    )/B27010_001E*100,
    EP_HHINC25 = (
      B19001_002E + 
      B19001_003E + 
      B19001_004E + 
      B19001_005E
    )/B19001_001E*100,
    EP_NOBROADBAND = (1-B28008_004E/B28008_001E)*100,
    EP_SNAP = B22010_002E/B22010_001E*100,
    EP_RENTBURDEN30 = (
      B25070_007E + 
      B25070_008E + 
      B25070_009E + 
      B25070_010E
    )/B25070_001E*100,
    EP_RENTBURDEN50 = B25070_010E/B25070_001E*100,
    EP_OWNBURDEN30 = (
      B25091_008E + 
      B25091_009E + 
      B25091_010E + 
      B25091_011E +
      B25091_019E + 
      B25091_020E + 
      B25091_021E + 
      B25091_022E
    )/B25091_001E*100,
    EP_OWNBURDEN50 = (
      B25091_011E +
      B25091_022E
    )/B25091_001E*100,
    EP_WELFARE = B19057_002E/B22010_001E*100
  ) %>% 
  mutate(
    EPL_POV = percent_rank(EP_POV),
    EPL_UNEMP = percent_rank(EP_UNEMP),
    EPL_PCI = percent_rank(EP_PCI),
    EPL_NOHSDP = percent_rank(EP_NOHSDP),
    EPL_AGE65 = percent_rank(EP_AGE65),
    EPL_AGE17 = percent_rank(EP_AGE17),
    EPL_DISABL = percent_rank(EP_DISABL),
    EPL_SNGPNT = percent_rank(EP_SNGPNT),
    EPL_MINRTY = percent_rank(EP_MINRTY),
    EPL_LIMENG = percent_rank(EP_LIMENG),
    EPL_MUNIT = percent_rank(EP_MUNIT),
    EPL_MOBILE = percent_rank(EP_MOBILE),
    EPL_CROWD = percent_rank(EP_CROWD),
    EPL_NOVEH = percent_rank(EP_NOVEH),
    EPL_GROUPQ = percent_rank(EP_GROUPQ),
    EPL_UNINSUR = percent_rank(EP_UNINSUR),
    EPL_SUM = 
      EPL_POV +
      EPL_UNEMP +
      EPL_PCI +
      EPL_NOHSDP +
      EPL_AGE65 +
      EPL_AGE17 +
      EPL_DISABL +
      EPL_SNGPNT +
      EPL_MINRTY +
      EPL_LIMENG +
      EPL_MUNIT +
      EPL_MOBILE +
      EPL_CROWD +
      EPL_NOVEH +
      EPL_GROUPQ +
      EPL_UNINSUR,
    SVI = percent_rank(EPL_SUM)*100
  ) %>% 
  select(!starts_with("EPL")) %>% 
  select(
    CBG,
    SVI,
    everything()
  ) %>% 
  pivot_longer(
    -CBG,
    names_to = "Names",
    values_to = "Values"
  ) %>% 
  mutate(year = 2019)
```

Broadband data not available in 2014 

```{r}
cbgs_acs_2014 <-
  getCensus(
    name = "acs/acs5",
    vintage = 2014,
    region = "block group:*",
    regionin = paste0("state:",state,"+county:",county),
    vars = c(
      "B17010_001E", # Families
      "B17010_002E", # Income in the past 12 months below poverty level
      "B23025_001E", # Population 16 years and over
      "B23025_005E", # Unemployed
      "B19301_001E", # Per capita income in the past 12 months (in 2018 inflation-adjusted dollars)
      "B15003_001E", # Population 25 years and over
      "B15003_002E", # Less than high school graduate
      "B15003_003E",
      "B15003_004E",
      "B15003_005E",
      "B15003_006E",
      "B15003_007E",
      "B15003_008E",
      "B15003_009E",
      "B15003_010E",
      "B15003_011E",
      "B15003_012E",
      "B15003_013E",
      "B15003_014E",
      "B15003_015E",
      "B15003_016E",
      "B01001_001E", # Population
      "B01001_020E", # 65 years and over
      "B01001_021E", 
      "B01001_022E", 
      "B01001_023E", 
      "B01001_024E", 
      "B01001_025E", 
      "B01001_044E", 
      "B01001_045E", 
      "B01001_046E", 
      "B01001_047E", 
      "B01001_048E", 
      "B01001_049E", 
      "B01001_003E", # Under 18 years
      "B01001_004E", 
      "B01001_005E", 
      "B01001_006E", 
      "B01001_027E", 
      "B01001_028E", 
      "B01001_029E", 
      "B01001_030E", 
      "C21007_001E", # Civilian population 18 years and over for whom poverty status is determined
      "C21007_005E", # With a disability
      "C21007_008E", 
      "C21007_012E", 
      "C21007_015E",
      "C21007_020E", 
      "C21007_023E",
      "C21007_027E", 
      "C21007_030E",
      "B11003_001E", # Families
      "B11003_010E", # One parent, children under 18 years
      "B11003_016E",
      "B03002_003E", # Not Hispanic or Latino + White alone
      "B16004_001E", # Population 5 years and over
      "B16004_007E", # Speak English less than well
      "B16004_008E", 
      "B16004_012E",
      "B16004_013E",
      "B16004_017E",
      "B16004_018E",
      "B16004_022E",
      "B16004_023E",
      "B16004_029E",
      "B16004_030E",
      "B16004_034E",
      "B16004_035E",
      "B16004_039E",
      "B16004_040E",
      "B16004_044E",
      "B16004_045E",
      "B16004_051E",
      "B16004_052E",
      "B16004_056E",
      "B16004_057E",
      "B16004_061E",
      "B16004_062E",
      "B16004_066E",
      "B16004_067E",
      "B25024_001E", # Housing units
      "B25024_007E", # 10 or more
      "B25024_008E", 
      "B25024_009E", 
      "B25024_010E", # Mobile homes
      "B25014_001E", # Occupied housing units
      "B25014_005E", # Greater than 1 occupant per room
      "B25014_006E", 
      "B25014_007E", 
      "B25014_011E", 
      "B25014_012E", 
      "B25014_013E", 
      "B25044_003E", # No vehicle available
      "B25044_010E",
      "B09019_038E", # In group quarters
      "B27010_001E", # Civilian noninstitutionalized population
      "B27010_004E", # Health insurance coverage
      "B27010_005E",
      "B27010_006E",
      "B27010_007E",
      "B27010_011E",
      "B27010_012E",
      "B27010_013E",
      "B27010_017E", 
      "B27010_020E",
      "B27010_021E",
      "B27010_022E",
      "B27010_023E",
      "B27010_027E",
      "B27010_028E",
      "B27010_029E",
      "B27010_033E", 
      "B27010_036E",
      "B27010_037E",
      "B27010_038E",
      "B27010_039E",
      "B27010_043E",
      "B27010_044E",
      "B27010_045E",
      "B27010_046E",
      "B27010_050E", 
      "B27010_053E",
      "B27010_054E",
      "B27010_055E",
      "B27010_059E",
      "B27010_060E",
      "B27010_061E",
      "B27010_062E",
      "B27010_066E",
      "B19001_001E",
      "B19001_002E",
      "B19001_003E",
      "B19001_004E",
      "B19001_005E",
      "B22010_001E", # Households
      "B22010_002E", # Received SNAP in past 12 months
      "B25070_001E", # Renter-occupied housing units
      "B25070_007E", # Rent burden above 30%
      "B25070_008E",
      "B25070_009E",
      "B25070_010E",
      "B25091_001E", # Owner-occupied housing units
      "B25091_008E", # Owner burden above 30%
      "B25091_009E",
      "B25091_010E",
      "B25091_011E",
      "B25091_019E",
      "B25091_020E",
      "B25091_021E",
      "B25091_022E",
      "B19057_002E" # With public assistance income
    )
  ) %>% 
  mutate(
    CBG = paste0(state,county,tract,block_group)
  ) %>% 
  filter(CBG %in% cbgs$CBG) %>% 
  transmute(
    CBG,
    EP_POV = B17010_002E/B17010_001E*100,
    EP_UNEMP = B23025_005E/B23025_001E*100,
    EP_PCI = ifelse(
      B19301_001E > 0,
      B19301_001E,
      NA
    ),
    EP_NOHSDP = (
      B15003_002E +
      B15003_003E +
      B15003_004E +
      B15003_005E +
      B15003_006E +
      B15003_007E +
      B15003_008E +
      B15003_009E +
      B15003_010E +
      B15003_011E +
      B15003_012E +
      B15003_013E +
      B15003_014E +
      B15003_015E +
      B15003_016E
    )/B15003_001E*100,
    EP_AGE65 = (
      B01001_020E +
      B01001_021E +
      B01001_022E +
      B01001_023E +
      B01001_024E +
      B01001_025E +
      B01001_044E +
      B01001_045E +
      B01001_046E +
      B01001_047E +
      B01001_048E +
      B01001_049E
    )/B01001_001E*100,
    EP_AGE17 = (
      B01001_003E +
      B01001_004E +
      B01001_005E +
      B01001_006E +
      B01001_027E +
      B01001_028E +
      B01001_029E +
      B01001_030E
    )/B01001_001E*100,
    EP_DISABL = (
      C21007_005E +
      C21007_008E +
      C21007_012E +
      C21007_015E +
      C21007_020E +
      C21007_023E +
      C21007_027E +
      C21007_030E
    )/C21007_001E*100,
    EP_SNGPNT = (
      B11003_010E +
      B11003_016E
    )/B11003_001E*100,
    EP_MINRTY = (1-B03002_003E/B01001_001E)*100,
    EP_LIMENG = (
      B16004_007E +
      B16004_008E +
      B16004_012E +
      B16004_013E +
      B16004_017E +
      B16004_018E +
      B16004_022E +
      B16004_023E +
      B16004_029E +
      B16004_030E +
      B16004_034E +
      B16004_035E +
      B16004_039E +
      B16004_040E +
      B16004_044E +
      B16004_045E +
      B16004_051E +
      B16004_052E +
      B16004_056E +
      B16004_057E +
      B16004_061E +
      B16004_062E +
      B16004_066E +
      B16004_067E
    )/B16004_001E*100,
    EP_MUNIT = (
      B25024_007E +
      B25024_008E +
      B25024_009E
    )/B25024_001E*100,
    EP_MOBILE = B25024_010E/B25024_001E*100,
    EP_CROWD = (
      B25014_005E +
      B25014_006E +
      B25014_007E +
      B25014_011E +
      B25014_012E +
      B25014_013E
    )/B25014_001E*100,
    EP_NOVEH = (
      B25044_003E +
      B25044_010E
    )/B25014_001E*100,
    EP_GROUPQ = B09019_038E/B01001_001E,
    EP_MEDICARE = (
      B27010_006E +
      B27010_012E +
      B27010_013E +
      B27010_022E +
      B27010_028E +
      B27010_029E +
      B27010_038E +
      B27010_044E +
      B27010_045E +
      B27010_046E +
      B27010_055E +
      B27010_060E +
      B27010_061E +
      B27010_062E
    )/B27010_001E*100,
    EP_MEDICAID = (
      B27010_007E +
      B27010_013E +
      B27010_023E +
      B27010_029E +
      B27010_039E +
      B27010_046E +
      B27010_062E
    )/B27010_001E*100,
    EP_DIRECTINS = (
      B27010_005E +
      B27010_011E +
      B27010_021E +
      B27010_027E +
      B27010_037E +
      B27010_043E +
      B27010_045E +
      B27010_054E +
      B27010_059E +
      B27010_061E
    )/B27010_001E*100,
    EP_EMPLOYINS = (
      B27010_004E +
      B27010_011E +
      B27010_012E +
      B27010_020E +
      B27010_027E +
      B27010_028E +
      B27010_036E +
      B27010_043E +
      B27010_044E +
      B27010_053E +
      B27010_059E +
      B27010_060E
    )/B27010_001E*100,
    EP_UNINSUR = (
      B27010_017E +
      B27010_033E +
      B27010_050E +
      B27010_066E
    )/B27010_001E*100,
    EP_HHINC25 = (
      B19001_002E + 
      B19001_003E + 
      B19001_004E + 
      B19001_005E
    )/B19001_001E*100,
    EP_SNAP = B22010_002E/B22010_001E*100,
    EP_RENTBURDEN30 = (
      B25070_007E + 
      B25070_008E + 
      B25070_009E + 
      B25070_010E
    )/B25070_001E*100,
    EP_RENTBURDEN50 = B25070_010E/B25070_001E*100,
    EP_OWNBURDEN30 = (
      B25091_008E + 
      B25091_009E + 
      B25091_010E + 
      B25091_011E +
      B25091_019E + 
      B25091_020E + 
      B25091_021E + 
      B25091_022E
    )/B25091_001E*100,
    EP_OWNBURDEN50 = (
      B25091_011E +
      B25091_022E
    )/B25091_001E*100,
    EP_WELFARE = B19057_002E/B22010_001E*100
  ) %>% 
  mutate(
    EPL_POV = percent_rank(EP_POV),
    EPL_UNEMP = percent_rank(EP_UNEMP),
    EPL_PCI = percent_rank(EP_PCI),
    EPL_NOHSDP = percent_rank(EP_NOHSDP),
    EPL_AGE65 = percent_rank(EP_AGE65),
    EPL_AGE17 = percent_rank(EP_AGE17),
    EPL_DISABL = percent_rank(EP_DISABL),
    EPL_SNGPNT = percent_rank(EP_SNGPNT),
    EPL_MINRTY = percent_rank(EP_MINRTY),
    EPL_LIMENG = percent_rank(EP_LIMENG),
    EPL_MUNIT = percent_rank(EP_MUNIT),
    EPL_MOBILE = percent_rank(EP_MOBILE),
    EPL_CROWD = percent_rank(EP_CROWD),
    EPL_NOVEH = percent_rank(EP_NOVEH),
    EPL_GROUPQ = percent_rank(EP_GROUPQ),
    EPL_UNINSUR = percent_rank(EP_UNINSUR),
    EPL_SUM = 
      EPL_POV +
      EPL_UNEMP +
      EPL_PCI +
      EPL_NOHSDP +
      EPL_AGE65 +
      EPL_AGE17 +
      EPL_DISABL +
      EPL_SNGPNT +
      EPL_MINRTY +
      EPL_LIMENG +
      EPL_MUNIT +
      EPL_MOBILE +
      EPL_CROWD +
      EPL_NOVEH +
      EPL_GROUPQ +
      EPL_UNINSUR,
    SVI = percent_rank(EPL_SUM)*100
  ) %>% 
  select(!starts_with("EPL")) %>% 
  select(
    CBG,
    SVI,
    everything()
  ) %>% 
  pivot_longer(
    -CBG,
    names_to = "Names",
    values_to = "Values"
  ) %>% 
  mutate(year = 2014)
```

Eviction lab data

```{r}
eviction <- read_csv(paste0("https://eviction-lab-data-downloads.s3.amazonaws.com/",state_abbr,"/block-groups.csv")) %>% 
  select(CBG = GEOID, year, evictions, `eviction-filings`) %>% 
  filter(CBG %in% cbgs$CBG) %>% 
  pivot_longer(
    -c(CBG, year),
    names_to = "Names",
    values_to = "Values"
  )
```

```{r}
data <- rbind(
  cbgs_acs_2014,
  cbgs_acs_2019,
  eviction,
  cbgs_access
)

cbgs <- cbgs %>% st_transform(4326)
boundary <- boundary %>% st_transform(4326)
```

```{r}
save(
  cbgs,
  boundary,
  data,
  vaccine_access,
  vaccine_walk,
  vaccine_bike,
  vaccine_drive,
  cbgs_isochrone_intersect,
  file = paste0("scc_svi.rda")
)
```