library(tidyverse)
library(epiextractr)
Load EPI CPS Extracts via epiextractr
Note: Users will need to install epiextractr
for this example. Refer to EPI packages for R for installation instructions.
Load required libraries
Download CPS files
# download CPS ORG files
download_cps( sample = 'org', extracts_dir = 'C:/YOUR_PATH/cps', overwrite = TRUE)
# download CPS Basic (4085.3 MB)
download_cps(sample='basic', extracts_dir ='C:/YOUR_PATH/cps', overwrite = TRUE)
# download CPS May (38.8 MB)
download_cps(sample='may', extracts_dir = 'C:/YOUR_PATH/cps', overwrite = TRUE)
This will download the latest EPI CPS ORG extracts in .feather format from https://microdata.epi.org and place them in the directory C:\data\cps
.
Load your CPS extracts!
After the data is downloaded, load a selection of CPS data for your analysis:
<- load_cps("org", 2010:2022, year, orgwgt, wage, age, statefips, wbho,
org .extracts_dir = 'C:/YOUR_PATH/cps')
(Optional) Set and forget CPS data files by creating/editing an .Renviron file
To simplify usage, you can omit the .extracts_dir
argument by setting the environment variables to your extracts directory. This allows you to call CPS extracts from a single, dedicated folder, eliminating redundant downloads of CPS files.
# Find the .Renviron file
<- file.path(Sys.getenv("HOME"), ".Renviron")
renviron_path
# Open the file for editing
file.edit(renviron_path)
# Paste the environment variable settings into your .Renviron file,
# and make sure the paths are set to the location of your CPS files
=C:/YOUR_PATH/cps
EPIEXTRACTS_CPSBASIC_DIR=C:/YOUR_PATH/cps
EPIEXTRACTS_CPSMAY_DIR=C:/YOUR_PATH/cps EPIEXTRACTS_CPSORG_DIR
After editing your .Renviron
file, save your changes and restart R to apply them.
If you’ve set your .Renviron file paths to point to the folder containing your CPS files, you can omit the .extracts_dir()
command
<- load_cps("org", 2010:2022, year, orgwgt, wage, age, statefips, wbho) org