Get startedGet started for free

Getting data for multiple states

In the previous exercise, you learned how to combine datasets with the rbind_tigris() function. If you need data for more than two states, however, this process can get tedious. In this exercise, you'll learn how to generate a list of datasets for multiple states with the tidyverse map() function, and combine those datasets with rbind_tigris().

This exercise is part of the course

Analyzing US Census Data in R

View Course

Exercise instructions

  • Generate a vector of state codes for Maine, New Hampshire, Vermont, and Massachusetts named new_england.
  • Use the tidyverse map() function to iterate through the vector of state codes.
  • Set the local variable x to request tract data for each state inside the function used by map().

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Generate a vector of state codes and assign to new_england
___ <- c("ME", "NH", "VT", "MA")

# Iterate through the states and request tract data for state
ne_tracts <- ___(new_england, function(x) {
  ___(state = ___, cb = TRUE)
}) %>%
  rbind_tigris()

plot(ne_tracts$geometry)
Edit and Run Code