~hrbrmstr/epidata

ref: e12c13d229ba5f2ca9a4300ed38971676e68b20f epidata/R/employment.r -rw-r--r-- 9.1 KiB
e12c13d2hrbrmstr fixed CRAN checks; added 4 new functions; added UA 1 year, 7 months ago
                                                                                
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#' Retreive the share of the labor force without a job
#'
#' @param by \code{NULL} or character string with any combination of \code{g} (Gender),
#'   \code{r} (Race), \code{a} (Age), \code{e} (Education).  i.e. if you want to retrieve
#'   unemployment data by gender, race and education, you would set this parameter to "\code{gre}".
#' @return \code{tbl_df} with data filtered by the selected criteria.
#' @note See \code{get_unemployment_by_state()} for information on retrieving unemployment by state+race.
#' @references \href{https://www.epi.org/data/}{Economic Policy Institute Data Library}
#' @export
#' @examples
#' get_unemployment()
#'
#' get_unemployment("r")
#'
#' get_unemployment("grae")
get_unemployment <- function(by=NULL) {

  params <- list(subject="unemp")

  if (!is.null(by)) params <- make_params(params, by, c("g", "r", "a", "e"))

  res <- epi_query(params)

  cols <- stringi::stri_trans_tolower(res$columns$name)
  cols <- stringi::stri_replace_all_regex(cols, "[\\('\\)]", "")
  cols <- stringi::stri_replace_all_regex(cols, "[[:space:]" %s+%
                                            rawToChar(as.raw(c(0xe2, 0x80, 0x93))) %s+% "-]+",
                                          "_")
  out <- setNames(as_data_frame(res$data), cols)
  out <- dplyr::mutate_all(out, "clean_cols")
  out <- suppressMessages(readr::type_convert(out))

  cite <- html_text(read_html(res$meta$source %||% "<p>Economic Policy Institute</p>"))
  message(sprintf('Note: %s\nCitation: "%s"', res$meta$notes %||% "None", cite))

  out

}

#' Retreive the share of the labor force without a job (by state)
#'
#' @param by \code{NULL} or \code{r} for a partition by race.
#' @return \code{tbl_df} with data filtered by the selected criteria.
#' @note See \code{get_unemployment()} for other unemployment extracts..
#' @references \href{https://www.epi.org/data/}{Economic Policy Institute Data Library}
#' @export
#' @examples
#' get_unemployment_by_state()
#'
#' get_unemployment_by_state("r")
get_unemployment_by_state <- function(by=NULL) {

  params <- list(subject="unempstate")

  if (!is.null(by)) params <- make_params(params, by, c("r"))

  res <- epi_query(params)

  cols <- stringi::stri_trans_tolower(res$columns$name)
  cols <- stringi::stri_replace_all_regex(cols, "[\\('\\)]", "")
  cols <- stringi::stri_replace_all_regex(cols, "[[:space:]" %s+%
                                            rawToChar(as.raw(c(0xe2, 0x80, 0x93))) %s+% "-]+",
                                          "_")
  out <- setNames(as_data_frame(res$data), cols)
  out <- dplyr::mutate_all(out, "clean_cols")
  out <- suppressMessages(readr::type_convert(out))
  out <- tidyr::gather(out, region, value, -date)

  cite <- html_text(read_html(res$meta$source %||% "<p>Economic Policy Institute</p>"))
  message(sprintf('Note: %s\nCitation: "%s"', res$meta$notes %||% "None", cite))

  out

}

#' Retreive  the share of the labor force that has been unemployed for six months or longer
#'
#' @param by \code{NULL} or character string with any combination of \code{g} (Gender),
#'   \code{r} (Race), \code{a} (Age), \code{e} (Education). i.e. if you want to retrieve
#'   unemployment data by gender, race and education, you would set this parameter to "\code{gre}".
#' @return \code{tbl_df} with data filtered by the selected criteria.
#' @references \href{https://www.epi.org/data/}{Economic Policy Institute Data Library}
#' @export
#' @examples
#' get_long_term_unemployment()
#'
#' get_long_term_unemployment("r")
#'
#' get_long_term_unemployment("grae")
get_long_term_unemployment <- function(by=NULL) {

  params <- list(subject="ltunemp")

  if (!is.null(by)) params <- make_params(params, by, c("g", "r", "a", "e"))

  res <- epi_query(params)

  cols <- stringi::stri_trans_tolower(res$columns$name)
  cols <- stringi::stri_replace_all_regex(cols, "[\\('\\)]", "")
  cols <- stringi::stri_replace_all_regex(cols, "[[:space:]" %s+%
                                            rawToChar(as.raw(c(0xe2, 0x80, 0x93))) %s+% "-]+",
                                          "_")
  out <- setNames(as_data_frame(res$data), cols)
  out <- dplyr::mutate_all(out, "clean_cols")
  out <- suppressMessages(readr::type_convert(out))

  cite <- html_text(read_html(res$meta$source %||% "<p>Economic Policy Institute</p>"))
  message(sprintf('Note: %s\nCitation: "%s"', res$meta$notes %||% "None", cite))

  out

}

#' Retreive the share of the labor force that is "underemployed"
#'
#' Underemployment is the share of the labor force that either 1) is unemployed, 2) is
#' working part time but wants and is available to work full time (an "involuntary" part
#' timer), or 3) wants and is available to work and has looked for work in the last year
#' but has given up actively seeking work in the last four weeks ("marginally attached"
#' worker).
#'
#' @param by \code{NULL} or character string with any combination of \code{g} (Gender),
#'   \code{r} (Race), \code{a} (Age), \code{e} (Education). i.e. if you want to retrieve
#'   unemployment data by gender, race and education, you would set this parameter to "\code{gre}".
#' @return \code{tbl_df} with data filtered by the selected criteria.
#' @references \href{https://www.epi.org/data/}{Economic Policy Institute Data Library}
#' @export
#' @examples
#' get_underemployment()
#'
#' get_underemployment("r")
#'
#' get_underemployment("grae")
get_underemployment <- function(by=NULL) {

  params <- list(subject="underemp")

  if (!is.null(by)) params <- make_params(params, by, c("g", "r", "a", "e"))

  res <- epi_query(params)

  cols <- stringi::stri_trans_tolower(res$columns$name)
  cols <- stringi::stri_replace_all_regex(cols, "[\\('\\)]", "")
  cols <- stringi::stri_replace_all_regex(cols, "[[:space:]" %s+%
                                            rawToChar(as.raw(c(0xe2, 0x80, 0x93))) %s+% "-]+",
                                          "_")
  out <- setNames(as_data_frame(res$data), cols)
  out <- dplyr::mutate_all(out, "clean_cols")
  out <- suppressMessages(readr::type_convert(out))

  cite <- html_text(read_html(res$meta$source %||% "<p>Economic Policy Institute</p>"))
  message(sprintf('Note: %s\nCitation: "%s"', res$meta$notes %||% "None", cite))

  out

}

#' Retreive the share of the civilian noninstitutional population that is in the labor force
#'
#' (i.e., working or looking for work)
#'
#' @param by \code{NULL} or character string with any combination of \code{g} (Gender),
#'   \code{r} (Race), \code{a} (Age), \code{e} (Education). i.e. if you want to retrieve
#'   unemployment data by gender, race and education, you would set this parameter to "\code{gre}".
#' @return \code{tbl_df} with data filtered by the selected criteria.
#' @references \href{https://www.epi.org/data/}{Economic Policy Institute Data Library}
#' @export
#' @examples
#' get_labor_force_participation_rate()
#'
#' get_labor_force_participation_rate("r")
#'
#' get_labor_force_participation_rate("grae")
get_labor_force_participation_rate <- function(by=NULL) {

  params <- list(subject="lfpr")

  if (!is.null(by)) params <- make_params(params, by, c("g", "r", "a", "e"))

  res <- epi_query(params)

  cols <- stringi::stri_trans_tolower(res$columns$name)
  cols <- stringi::stri_replace_all_regex(cols, "[\\('\\)]", "")
  cols <- stringi::stri_replace_all_regex(cols, "[[:space:]" %s+%
                                            rawToChar(as.raw(c(0xe2, 0x80, 0x93))) %s+% "-]+",
                                          "_")
  out <- setNames(as_data_frame(res$data), cols)
  out <- dplyr::mutate_all(out, "clean_cols")
  out <- suppressMessages(readr::type_convert(out))

  cite <- html_text(read_html(res$meta$source %||% "<p>Economic Policy Institute</p>"))
  message(sprintf('Note: %s\nCitation: "%s"', res$meta$notes %||% "None", cite))

  out

}

#' Retreive the share of the civilian noninstitutional population that is employed
#'
#' @param by \code{NULL} or character string with any combination of \code{g} (Gender),
#'   \code{r} (Race), \code{a} (Age), \code{e} (Education). i.e. if you want to retrieve
#'   unemployment data by gender, race and education, you would set this parameter to "\code{gre}".
#' @return \code{tbl_df} with data filtered by the selected criteria.
#' @export
#' @references \href{https://www.epi.org/data/}{Economic Policy Institute Data Library}
#' @examples
#' get_employment_to_population_ratio()
#'
#' get_employment_to_population_ratio("r")
#'
#' get_employment_to_population_ratio("grae")
get_employment_to_population_ratio <- function(by=NULL) {

  params <- list(subject="lfpr")

  if (!is.null(by)) params <- make_params(params, by, c("g", "r", "a", "e"))

  res <- epi_query(params)

  cols <- stringi::stri_trans_tolower(res$columns$name)
  cols <- stringi::stri_replace_all_regex(cols, "[\\('\\)]", "")
  cols <- stringi::stri_replace_all_regex(cols, "[[:space:]" %s+%
                                            rawToChar(as.raw(c(0xe2, 0x80, 0x93))) %s+% "-]+",
                                          "_")
  out <- setNames(as_data_frame(res$data), cols)
  out <- dplyr::mutate_all(out, "clean_cols")
  out <- suppressMessages(readr::type_convert(out))

  cite <- html_text(read_html(res$meta$source %||% "<p>Economic Policy Institute</p>"))
  message(sprintf('Note: %s\nCitation: "%s"', res$meta$notes %||% "None", cite))

  out

}