Calculate Median Household Income for Albany Wards using R 🗺

Calculate Median Household Income for Albany Wards using R 🗺

A while back I posted similar code using Python, but with R the code is cleaner, simpler.

library(tidycensus)
library(tidyverse)
library(sf)

# shapefile of Albany Wards, project to web mercator 3857
# calculate area of each ward, load Ward field as numeric
# hide extra fields in shapefile
wards <- read_sf('/tmp/Albany Wards.gpkg') %>%
  st_transform(3857) %>%
  mutate(ward_area = st_area(.),
         Ward = as.numeric(Ward)) %>%
  dplyr::select(Ward, ward_area)

# get median household income, re-project
# to web mercator 3857
income<- get_acs(
  geography = "tract",
  state='ny',
  county='Albany',
  variables = "B19013_001",
  year = 2020,
  survey = "acs5",
  geometry = T
) %>%
  st_transform(3857)


# intersect income against wards
# calculate percent of ward's area in each census tract
# group back together as a single ward
# create a weighted-mean of median household inco
# rejoin back together with original ward shapefile
income %>%
  st_intersection(wards) %>%
  mutate(percent_of_ward = (st_area(.) / ward_area) %>% units::drop_units() ) %>%
  st_drop_geometry() %>%
  group_by(Ward) %>%
  summarise(estimate = weighted.mean(estimate, percent_of_ward, na.rm=T)) %>%
inner_join(wards, ., by=c('Ward'))

Here is what is produced, with geometry columns suppressed (exclude the last line above when you don’t need the other information for mapping).

    Ward estimate
<dbl> <dbl>
1 1 57500.
2 2 46544.
3 3 37274.
4 4 39713.
5 5 38127.
6 6 47070.
7 7 47915.
8 8 86490.
9 9 57688.
10 10 44964.
11 11 33290.
12 12 69262.
13 13 67333.
14 14 78489.
15 15 72368.
SVGZ Graphic: 2000 and 2020 Decennial Census Population
SVGZ Graphic: Albany Median Household Income by Ward
SVGZ Graphic: Areas Developed in the City of Albany, 1986-2021
SVGZ Graphic: Capital Region Population Density
SVGZ Graphic: Municipalities with a Similiar Population Density to Albany
SVGZ Graphic: Percent of Albany County Homeowners Who Moved in Before 1990
Terrain Map: Albany Plan 1695
Terrain Map: Artery Art, Albany
Terrain Map: Beaver Kill in Albany
Terrain Map: Buildings Constructed in Capital Region, 2015-2020
Terrain Map: Clarksville 1859
Terrain Map: Proposed Downtown Retail District - Plan for the Capital City
Thematic Map: 2015-2020 New Buildings Constructed in New York State
Thematic Map: African Americans By Election District in Albany County
Thematic Map: Albany City Wards – Redistricting Winners and Losers
Thematic Map: Albany City Wards 2024
Thematic Map: Albany Zip Codes
Thematic Map: Albany's South End, Historic Dumping Grounds
Thematic Map: Bernie Sanders Plan A
Thematic Map: Bernie Sanders Plan B
Thematic Map: Court Street 1787
Thematic Map: Distance and Population to State Capitol (50 mile increments)
Thematic Map: Downtown Albany - Non-Tax Exempt Properties
Thematic Map: Drive Time to Downtown Albany
Thematic Map: Fort Orange Today
Thematic Map: Gerald Jennings 2009 Primary Campaign Contributions
Thematic Map: Median Year Building Built
Thematic Map: Thompson Lake Nature Trail LiDAR
Thematic Map: Year Building - Constructed in Albany
Photo: Closed off South Mall Arterial
Photo: Not too bad
Photo: Burst of Color
Photo: Bright
Photo: Frame 121
Photo: Transparency
Photo: Corner of 80 State Street
Photo: Sun Sets Over Heldebergs
Photo: Columbia Turnpike
Photo: Tulips

Leave a Reply

Your email address will not be published. Required fields are marked *