Overview

Data on foreign-born status in the U.S.

Request a U.S. Census Data API Key

Go to this site to request your API key.

Save this key and do not share it with anyone.

Set up your work enviornment

Open up a new .Rmd file.

Use {r setup, include=F} in your first code chunk.

knitr::opts_chunk$set(echo = TRUE)

# Load necessary libraries
library(knitr)
library(kableExtra)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()     masks stats::filter()
## ✖ dplyr::group_rows() masks kableExtra::group_rows()
## ✖ dplyr::lag()        masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr) 
library(dplyr)
library(tidyr)
library(tidycensus)
# Set your Census API key
# census_api_key("YOUR_API_KEY_HERE")  # Replace with your actual API key
# Get data for the foreign-born population from the American Community Survey (ACS)
# Using ACS 5-Year Estimates (2021) for detailed demographic data
immigrant_data <- get_acs(
  geography = "state",
  variables = c(
    foreign_born = "B05006_001",  # Total Foreign-Born Population
    naturalized_citizen = "B05006_002",  # Naturalized Citizens
    non_citizen = "B05006_003",  # Non-Citizens
    median_income = "B19013_001",  # Median Household Income
    pct_below_poverty = "B17001_002"  # Population Below Poverty Level
  ),
  year = 2021,  # Change this to the desired year
  survey = "acs5"
)
## Getting data from the 2017-2021 5-year ACS
# View the first few rows of the data
head(immigrant_data)
## # A tibble: 6 × 5
##   GEOID NAME    variable            estimate   moe
##   <chr> <chr>   <chr>                  <dbl> <dbl>
## 1 01    Alabama foreign_born          173429  3378
## 2 01    Alabama naturalized_citizen    19914  1300
## 3 01    Alabama non_citizen             5080   602
## 4 01    Alabama pct_below_poverty     769819 12595
## 5 01    Alabama median_income          54943   377
## 6 02    Alaska  foreign_born           57925  1950
# Optionally, you can summarize or manipulate the data further
summary_data <- immigrant_data %>%
  group_by(GEOID) %>%
  summarise(
    total_foreign_born = sum(estimate[variable == "foreign_born"]),
    total_naturalized = sum(estimate[variable == "naturalized_citizen"]),
    total_non_citizen = sum(estimate[variable == "non_citizen"]),
    median_income = unique(estimate[variable == "median_income"]),
    pct_below_poverty = unique(estimate[variable == "pct_below_poverty"])
  )

# View summary data
print(summary_data)
## # A tibble: 52 × 6
##    GEOID total_foreign_born total_naturalized total_non_citizen median_income
##    <chr>              <dbl>             <dbl>             <dbl>         <dbl>
##  1 01                173429             19914              5080         54943
##  2 02                 57925              7050              1435         80287
##  3 04                922119             85768             22117         65913
##  4 05                148159              9781              3458         52123
##  5 06              10454934            678040            171885         84097
##  6 08                545464             72855             18071         80184
##  7 09                534209            120678             20157         83572
##  8 10                 94639              9604              2390         72724
##  9 11                 92191             16240              4067         93547
## 10 12               4478309            424824             99010         61777
## # ℹ 42 more rows
## # ℹ 1 more variable: pct_below_poverty <dbl>