Variable Hunt

Harold Nelson

2022-10-29

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6      ✔ purrr   0.3.4 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.4.1 
## ✔ readr   2.1.2      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(tidycensus)
library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
#census_api_key("Your Census API  Key",install = TRUE)

Get Variable List

v20 = load_variables(year = 2020,
                    "acs5", 
                    cache = TRUE)
nrow(v20)
## [1] 27850
head(v20)
## # A tibble: 6 × 4
##   name       label                                   concept    geography  
##   <chr>      <chr>                                   <chr>      <chr>      
## 1 B01001_001 Estimate!!Total:                        SEX BY AGE block group
## 2 B01001_002 Estimate!!Total:!!Male:                 SEX BY AGE block group
## 3 B01001_003 Estimate!!Total:!!Male:!!Under 5 years  SEX BY AGE block group
## 4 B01001_004 Estimate!!Total:!!Male:!!5 to 9 years   SEX BY AGE block group
## 5 B01001_005 Estimate!!Total:!!Male:!!10 to 14 years SEX BY AGE block group
## 6 B01001_006 Estimate!!Total:!!Male:!!15 to 17 years SEX BY AGE block group

Clean Up

Eliminate the extra blank spaces.

v20 = v20 %>% 
  select(-geography) %>% 
  mutate(label = str_replace(label,"  ",""),
         concept = str_replace(concept,"  ",""))

head(v20)
## # A tibble: 6 × 3
##   name       label                                   concept   
##   <chr>      <chr>                                   <chr>     
## 1 B01001_001 Estimate!!Total:                        SEX BY AGE
## 2 B01001_002 Estimate!!Total:!!Male:                 SEX BY AGE
## 3 B01001_003 Estimate!!Total:!!Male:!!Under 5 years  SEX BY AGE
## 4 B01001_004 Estimate!!Total:!!Male:!!5 to 9 years   SEX BY AGE
## 5 B01001_005 Estimate!!Total:!!Male:!!10 to 14 years SEX BY AGE
## 6 B01001_006 Estimate!!Total:!!Male:!!15 to 17 years SEX BY AGE

Search for “BACHELOR’S”

ba = v20 %>% 
  filter(str_detect(concept,"BACHELOR'S"))

nrow(ba)
## [1] 115
head(ba)
## # A tibble: 6 × 3
##   name       label                                                       concept
##   <chr>      <chr>                                                       <chr>  
## 1 B15011_001 Estimate!!Total:                                            SEX BY…
## 2 B15011_002 Estimate!!Total:!!Male:                                     SEX BY…
## 3 B15011_003 Estimate!!Total:!!Male:!!25 to 39 years:                    SEX BY…
## 4 B15011_004 Estimate!!Total:!!Male:!!25 to 39 years:!!Science and Engi… SEX BY…
## 5 B15011_005 Estimate!!Total:!!Male:!!25 to 39 years:!!Science and Engi… SEX BY…
## 6 B15011_006 Estimate!!Total:!!Male:!!25 to 39 years:!!Business          SEX BY…