-
Notifications
You must be signed in to change notification settings - Fork 1
/
prop_tables.R
96 lines (86 loc) · 3.16 KB
/
prop_tables.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
## Functions for survey analysis at the US Studies Centre
## Zoe Meers
## The United States Studies Centre at the University of Sydney
#' Grouped proportional tables for USSC cross-country surveys
#' @description
#' Calculates the weighted proportion for the specified questions and groups, both in the US and Australia.
#' @usage
#' prop_grouped_survey_question()
#' @examples
#' \dontrun{
#' survey_data %>%
#' prop_grouped_survey_question(questions = starts_with("climate_change"), partyid, gender)}
#' @author
#' Zoe Meers
#' @export
prop_grouped_survey_question <- function (.data, questions, ...){
quos <- rlang::enquos(...)
.data %>% dplyr::select(..., questions, weight, sample) %>%
tidyr::gather("question", "answer",
-c(!!!(quos), weight, sample), na.rm = TRUE) %>%
dplyr::left_join(variables_in_long_file %>%
select(description_us, description_au, value),
by = c(question = "value")) %>%
dplyr::mutate(answer = factor(answer)) %>%
tidyr::drop_na(..., answer) %>%
dplyr::count(sample, ...,
description_au,
description_us,
answer,
wt = as.numeric(as.character(weight))) %>%
dplyr::group_by(sample, ..., description_us, description_au) %>%
dplyr::mutate(proportion = round(n/sum(n) * 100, 0)) %>%
dplyr::select(-n) %>%
dplyr::ungroup() %>%
tidyr::unite(subgroup,
c(sample, !!!(quos)), sep = "_") %>%
tidyr::spread(subgroup, proportion)
}
#' Prop tables for USSC cross-country surveys
#' @description
#' Calculates the weighted proportion for the specified questions in the US and Australia.
#' @usage
#' prop_survey_question()
#' @examples
#' \dontrun{
#' survey_data %>%
#' prop_survey_question(questions = starts_with("importance_of"))
#' }
#' @author
#' Zoe Meers
#' @export
prop_survey_question <- function(.data, questions) {
.data %>%
dplyr::select(sample, weight, questions) %>%
tidyr::gather("question", "answer", -sample, -weight,
na.rm = TRUE) %>%
dplyr::left_join(variables_in_long_file %>%
select(description_us, description_au, value),
by = c("question" = "value")) %>%
dplyr::mutate(answer = factor(answer)) %>%
dplyr::count(sample, description_us, description_au,
answer, wt = as.numeric(as.character(weight))) %>%
dplyr::group_by(sample, description_us, description_au) %>%
dplyr::mutate(proportion = round(n/sum(n)*100, 0)) %>%
dplyr::select(-n) %>%
dplyr::ungroup() %>%
tidyr::spread(sample, proportion) %>%
dplyr::mutate(Difference = `United States` - `Australia`)
}
#' Relevel answer column as factors
#' @usage
#' relevel_survey_answer()
#' @examples
#' \dontrun{
#' survey_data %>%
#' prop_survey_question(questions = starts_with("importance_of")) %>%
#' relevel_survey_answer(levels)
#' }
#' @author
#' Zoe Meers
#' @export
relevel_survey_answer <- function(.data, levels) {
.data %>%
dplyr::mutate(answer = forcats::fct_relevel(answer, levels)) %>%
dplyr::arrange(description_us, answer)
}