Last active
September 18, 2023 15:41
-
-
Save ericrobskyhuntley/ad27f8327f6f14924a9e9e3a44383443 to your computer and use it in GitHub Desktop.
R script to approximate the number of units in owner-occupied 2-, 3-, and 4- unit buildings in Somerville.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df <- sf::st_read("M274_parcels_CY22_FY23_sde.gdb", layer = "M274Assess") |> | |
dplyr::mutate( | |
simp = dplyr::case_when( | |
stringr::str_detect(USE_CODE, "^0?101") ~ "101", | |
stringr::str_detect(USE_CODE, "^0?104") ~ "104", | |
stringr::str_detect(USE_CODE, "^0?105") ~ "105", | |
stringr::str_detect(USE_CODE, "^0?109") ~ "109", | |
stringr::str_detect(USE_CODE, "^0?111") ~ "111", | |
stringr::str_detect(USE_CODE, "^0?112") ~ "112", | |
), | |
own_occ = SITE_ADDR == OWN_ADDR, | |
units_est = dplyr::case_when( | |
simp == "101" ~ 1, | |
simp == "104" ~ 2 - own_occ, | |
simp == "105" ~ 3 - own_occ, | |
(UNITS == 0) & (simp == "109") ~ ceiling(RES_AREA / 1020) - own_occ, | |
simp == "109" ~ UNITS, | |
(UNITS == 0) & (simp == "111") ~ ceiling(RES_AREA / 1020), | |
simp == "111" ~ UNITS, | |
(UNITS == 0) & (simp == "112") ~ ceiling(RES_AREA / 1020), | |
simp == "112" ~ UNITS, | |
), | |
own_occ_2fewer = own_occ & (units_est < 2), | |
own_occ_3fewer = own_occ & (units_est < 3), | |
own_occ_4fewer = own_occ & (units_est < 4) | |
) |> | |
dplyr::filter(!is.na(simp), !(simp == "101" & own_occ), units_est > 0) | |
units_est <- sum(df$units_est, na.rm = TRUE) | |
own_occ_2fewer <- df |> | |
dplyr::group_by(own_occ_2fewer) |> | |
dplyr::summarize( | |
units_est = sum(units_est) | |
) |> | |
dplyr::filter( | |
own_occ_2fewer | |
) |> | |
dplyr::pull(units_est) | |
own_occ_3fewer <- df |> | |
dplyr::group_by(own_occ_3fewer) |> | |
dplyr::summarize( | |
units_est = sum(units_est) | |
) |> | |
dplyr::filter( | |
own_occ_3fewer | |
) |> | |
dplyr::pull(units_est) | |
own_occ_4fewer <- df |> | |
dplyr::group_by(own_occ_4fewer) |> | |
dplyr::summarize( | |
units_est = sum(units_est) | |
) |> | |
dplyr::filter( | |
own_occ_4fewer | |
) |> | |
dplyr::pull(units_est) | |
results <- data.frame( | |
`Category` = c( | |
"Owner-Occupied, 2 Units or Less", | |
"Owner-Occupied, 3 Units or Less", | |
"Owner-Occupied, 4 Units or Less"), | |
`Units` = c(own_occ_2fewer, own_occ_3fewer, own_occ_4fewer), | |
`Percent of Rentals` = c( | |
round(own_occ_2fewer / units_est * 100, 2), | |
round(own_occ_3fewer / units_est * 100, 2), | |
round(own_occ_4fewer / units_est * 100, 2) | |
) | |
) | |
results |> | |
gt::gt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment