/
cces_recode-outcome.R
45 lines (43 loc) · 1.55 KB
/
cces_recode-outcome.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
#' Translate a Yes/No variable into 1/0
#'
#' Operationalizes outcomes of type yes/no to binary variables. It is internally
#' given the values to count as a "Yes" by regular expression, e.g. not only "Yes"
#' but also "Support" and "For"
#'
#' @param vec A vector of CCES response values, in character
#' @param DK_to_NA Whether don't knows should be treated as NA. Defaults to TRUE.
#'
#' @return A vector of integers
#'
#' @importFrom stringr regex str_detect str_replace_all
#' @importFrom dplyr recode
#' @importFrom magrittr `%>%`
#'
#' @examples
#' yesno_to_binary(
#' c("Yes",
#' "No",
#' "Support",
#' "Oppose",
#' "Somewhat support",
#' "Somewhat oppose",
#' "For this bill",
#' "Against this bill",
#' "Not sure",
#' "Don't Know",
#' NA_character_)
#' )
#' @export
yesno_to_binary <- function(vec, DK_to_NA = TRUE) {
. <- NULL
# Yes
vec_char <- as.character(vec) %>%
replace(str_detect(., regex("^Against", ignore_case = TRUE)), "Against") %>%
replace(str_detect(., regex("^For", ignore_case = TRUE)), "For") %>%
str_replace_all(regex("somewhat\\s+", ignore_case = TRUE), "") %>%
replace(. %in% c("Against", "Oppose", "oppose", "against", "no", "No"), "N") %>%
replace(. %in% c("For", "for", "Support", "support", "yes", "Yes"), "Y") %>%
replace(. %in% c("Don't Know", "Don't know", "dk", "don't know", "Not Sure", "not sure", "not sure "), "DK") %>%
replace(. %in% c("Not Asked", "Skipped", "skipped"), NA)
recode(vec_char, Y = 1, N = 0, DK = ifelse(DK_to_NA, NA_real_, 0), .default = NA_real_)
}