There’s a Package for That?

R Packages that Support Analysis of 

Common Federal Datasets

Kelsey Farson Gray

Senior Data Scientist, Insight Policy Research 
2

readr ggplot2

dplyr stringr

forcats tidyr

purrr tibble

Tidyverse

www.tidyverse.org

• What is an R package? 

– Collection of functions 

structured to support a process

– Well-documented

– Available for download

• Accessing packages

install.packages(“<package>”)

library(<package>)

• Popular packages
3

POP QUIZ: 

How many R packages 

are there?

130 1,300 13,000
4

13,200
How can R packages 

support analyses of 

common federal 

data sets? 

5
Today’s examples

 Leverage Application Program Interfaces 

(APIs) to access data

 Allows communication between software, not

the Graphical User Interface (GUI)

 Example: OpenTable

 Expedite routine analyses 

 Enhance reproducibility / documentation

6
How do I get started? 

 Know what you’re looking for (e.g., data set, 

data year, table and variable names)

 Search available packages 

 How do I find new packages? (CRAN, GitHub)

 How do I interpret R packages?

 Explore available APIs for commonly used data

 Request API keys, if needed

7
8

Examples
9
10

NOTE: Additional arguments are accepted
How does mode of 

transportation to 

work vary within 

the United States? 

11
Example: Code for fetching data 

through ‘acs’ package

12
13

# Install 'acs' package

install.packages("acs") 

library(acs) 

# Key install (request from Census) 

api.key.install("fb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Set geography 

us <- geo.make(us = "*") # Entire United States

nc <- geo.make(state = "NC") # State

unc <- geo.make(zip.code = 27514) # Zip code

# Fetch table B08101 - "Means of Transportation to Work by Age”

us.commute <- acs.fetch(endyear = 2015, 

span = 5, 

geography = us, 

table.number = "B08101")

Let’s break it down... 
# Install 'acs' package

install.packages("acs") 

library(acs) 

# Key install (request from Census) 

api.key.install("fb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Set geography 

us <- geo.make(us = "*") # Entire United States

nc <- geo.make(state = "NC") # State

unc <- geo.make(zip.code = 27514) # Zip code

# Fetch table B08101 - "Means of Transportation to Work by Age”

us.commute <- acs.fetch(endyear = 2015, 

span = 5, 

geography = us, 

table.number = "B08101")

14

Let’s break it down... 
# Install 'acs' package

install.packages("acs") 

library(acs) 

# Key install (request from Census) 

api.key.install("fb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Set geography 

us <- geo.make(us = "*") # Entire United States

nc <- geo.make(state = "NC") # State

unc <- geo.make(zip.code = 27514) # Zip code

# Fetch table B08101 - "Means of Transportation to Work by Age”

us.commute <- acs.fetch(endyear = 2015, 

span = 5, 

geography = us, 

table.number = "B08101")

15

Let’s break it down... 
# Install 'acs' package

install.packages("acs") 

library(acs) 

# Key install (request from Census) 

api.key.install("fb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Set geography 

us <- geo.make(us = "*") # Entire United States

nc <- geo.make(state = "NC") # State

unc <- geo.make(zip.code = 27514) # Zip code

# Fetch table B08101 - "Means of Transportation to Work by Age”

us.commute <- acs.fetch(endyear = 2015, 

span = 5, 

geography = us, 

table.number = "B08101")

16

Let’s break it down... 
17

Figure 1. Means of Transportation to Work by Location

Source: 2011-2015 American Community Survey 5-Year Estimates
18
19

NOTE: Additional arguments are accepted
How many housing 

units are occupied 

by county? 

20
21

Example: Code for fetching data 

through ‘tidycensus’ package
22

Let’s break it down... 

# Install 'tidycensus' package

install.packages("tidycensus")

library(tidycensus)

# Key install

census_api_key("ffb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Call housing unit variables from decennial census

housing <- c(Occupied = "H003002", 

Vacant = "H003003")

# Fetch table on "Housing Unit"

nc.housing.map <- get_decennial(geography = "county", 

variables = housing, 

year = 2010, 

summary_var = "H003001", 

state = "NC", 

geometry = TRUE)
23

Let’s break it down... 

# Install 'tidycensus' package

install.packages("tidycensus")

library(tidycensus)

# Key install

census_api_key("ffb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Call housing unit variables from decennial census

housing <- c(Occupied = "H003002", 

Vacant = "H003003")

# Fetch table on "Housing Unit"

nc.housing.map <- get_decennial(geography = "county", 

variables = housing, 

year = 2010, 

summary_var = "H003001", 

state = "NC", 

geometry = TRUE)
24

Let’s break it down... 

# Install 'tidycensus' package

install.packages("tidycensus")

library(tidycensus)

# Key install

census_api_key("ffb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Call housing unit variables from decennial census

housing <- c(Occupied = "H003002", 

Vacant = "H003003")

# Fetch table on "Housing Unit"

nc.housing.map <- get_decennial(geography = "county", 

variables = housing, 

year = 2010, 

summary_var = "H003001", 

state = "NC", 

geometry = TRUE)
Let’s break it down... 

# Install 'tidycensus' package

install.packages("tidycensus")

library(tidycensus)

# Key install

census_api_key("ffb9db49ee4b4563c2e5q5h922ei4ale89d3eafe6")

# Call housing unit variables from decennial census

housing <- c(Occupied = "H003002", 

Vacant = "H003003")

# Fetch table on "Housing Unit"

nc.housing.map <- get_decennial(geography = "county", 

variables = housing, 

year = 2010, 

summary_var = "H003001", 

state = "NC", 

geometry = TRUE)
26

Figure 2. Percent of Occupied Housing Units in North Carolina, 2010 Census

Source: 2010 Decennial Census, U.S. Census Bureau
27

Figure 3. Percent of Occupied Housing Units in the Continental U.S., 2010 Census

Source: 2010 Decennial Census, U.S. Census Bureau
Additional R Packages

 blsAPI: Allows users to request data for one or multiple series 

through the BLS API. https://cran.rproject.org/web/packages/blsAPI/blsAPI.pdf

 rHealthDataGov: Access and select subsets of data from 

HealthData.gov. https://cran.rproject.org/web/packages/rHealthDataGov/rHealthDataGov.pdf

 medicare: Package for obtaining and cleaning Medicare public use 

files. https://cran.rproject.org/web/packages/medicare/medicare.pdf

 iPUMSR: Import census, survey, and geographic data from IPUMS

https://cran.r-project.org/web/packages/ipumsr/ipumsr.pdf 28
GitHub

 CDC Vital Statistics:

https://github.com/Mikuana/vitalstatistics

 Department of Education: 

https://github.com/UrbanInstitute/education-data- package-r

 Integrated Postsecondary Education Data System (IPEDS)

 Common Core of Data (CCD)

 Bureau of Labor Statistics: 

https://github.com/keberwein/blscrapeR

29
Questions? 

Kelsey Farson Gray

kgray@insightpolicyresearch.com

30