-
Notifications
You must be signed in to change notification settings - Fork 0
/
epi_microdata.qmd
87 lines (55 loc) · 2.76 KB
/
epi_microdata.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
---
title: "Load EPI CPS Extracts via epiextractr"
---
**Note:** Users will need to install `epiextractr` for this example. Refer to [EPI packages for R](epi_libraries.qmd) for installation instructions.
```{r Internal load libraries, include=FALSE, message=FALSE}
library(tidyverse)
library(epiextractr)
library(here)
```
```{r download cps, eval=FALSE, include=FALSE, message=FALSE}
# download CPS ORG files
download_cps(sample = 'org', extracts_dir = here('data/cps'))
# Users can also download CPS Basic (4085.3 MB) and CPS May (38.8 MB) files
download_cps(sample='basic', extracts_dir = here('data/cps'))
download_cps(sample='may', extracts_dir = here('data/cps'))
```
### Load required libraries
```{r Load libraries, eval=FALSE, message=FALSE}
library(tidyverse)
library(epiextractr)
```
### Download CPS files
```{r Download CPS files from microdata site, eval=FALSE, message=FALSE}
# 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](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:
```{r Load ORG, eval=FALSE, message=FALSE}
org <- load_cps("org", 2010:2022, year, orgwgt, wage, age, statefips, wbho,
.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.
```{r Access your .Renviron file, eval=FALSE}
# Find the .Renviron file
renviron_path <- file.path(Sys.getenv("HOME"), ".Renviron")
# 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
EPIEXTRACTS_CPSBASIC_DIR=C:/YOUR_PATH/cps
EPIEXTRACTS_CPSMAY_DIR=C:/YOUR_PATH/cps
EPIEXTRACTS_CPSORG_DIR=C:/YOUR_PATH/cps
```
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
```{r eval=FALSE, message=FALSE}
org <- load_cps("org", 2010:2022, year, orgwgt, wage, age, statefips, wbho)
```