/
Code_Data_ESR_Total_DF_2011_2015.R
77 lines (55 loc) · 2.44 KB
/
Code_Data_ESR_Total_DF_2011_2015.R
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
library(survey)
# calculate svytotals for ESR variable from ACS 1-yr PUMS (2011 to --------
#read list of person files from 2011 to 2015
svy.files <- list.files("./Svy_Objects/")
#create empty df to store all ESR values
esr.df <- data.frame("Year" = factor(), "ESRLvl" = factor(), "Total" = numeric(), "SE" = numeric())
#!!!!2011 and 2012 files (MIL levels change in 2013)
#loop to run through files (svy objects), calculate ESR and store them in a list
for(i in 1:2){
#create a pathname variable to the object file
pathname <- paste("./Svy_Objects/", svy.files[i], sep = "")
#read the svy object
svy.obj <- readRDS(pathname)
#calculate svytotal for ESR, store ESR calculation as a list item
esr <- svytotal(~factor(ESR),
subset(svy.obj, MIL %in% c(2,3) & VPS %in% c(6:8)),
na.rm = TRUE)
#convert to df
esr <- data.frame(esr)
#create temporary df to store ESR calculations
tmp.df <- data.frame(Year = rep(paste("201", i, sep = ""), length(esr)),
EsrLvl = row.names(esr),
Total = esr$total,
SE = esr$SE)
esr.df <- rbind(esr.df, tmp.df)
}
#!!!!2013 and 2015 files
#loop to run through files (svy objects), calculate ESR and store them in a list
for(i in 3:5){
#create a pathname variable to the object file
pathname <- paste("./Svy_Objects/", svy.files[i], sep = "")
#read the svy object
svy.obj <- readRDS(pathname)
#calculate svytotal for ESR, store ESR calculation as a list item
esr <- svytotal(~factor(ESR),
subset(svy.obj, MIL == 2 & VPS %in% c(6:8)),
na.rm = TRUE)
#convert to df
esr <- data.frame(esr)
#create temporary df to store ESR calculations
tmp.df <- data.frame(Year = rep(paste("201", i, sep = ""), length(esr)),
EsrLvl = row.names(esr),
Total = esr$total,
SE = esr$SE)
esr.df <- rbind(esr.df, tmp.df)
}
#clean-up EsrLvl variable
esr.df$EsrLvl <- sub(x = esr.df$EsrLvl,
pattern = "factor(ESR)",
replacement = "",
fixed = TRUE)
esr.df$EsrLvl <- factor(x = esr.df$EsrLvl, levels = unique(esr.df$EsrLvl), labels = c("Civ @ Work", "Civ NOT @ Work", "Unemployed", "Not in Labor Force"))
#save df as Rds to be used elsewhere (i.e. making graphs)
saveRDS(esr.df, "Data_ESR_Total_DF_2011_2015.Rds")
# rm(list=ls())