Joining data from an external data frame
When working with geographic data in R, you'll commonly want to join attribute information from an external dataset to it for mapping and spatial analysis. The sf package enables the use of the tidyverse *_join()
functions for simple features objects for this purpose. In this exercise, you'll learn how to join data to a spatial dataset of legislative boundaries for the Texas House of Representatives that you've obtained using tigris.
This exercise is part of the course
Analyzing US Census Data in R
Exercise instructions
- Get a dataset of legislative district boundaries for Texas using the
state_legislative_districts()
function. - Set the
house
parameter to"lower"
to request data for the Texas House of Representatives. - Use the
left_join()
function to merge a dataset of Texas House representatives to their legislative district boundaries.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Get boundaries for Texas and set the house parameter
tx_house <- ___(___ = "TX", ___ = "lower", cb = TRUE)
# Merge data on legislators to their corresponding boundaries
tx_joined <- ___(___, tx_members, by = c("NAME" = "District"))
head(tx_joined)