/
tidy_response.R
63 lines (52 loc) · 1.73 KB
/
tidy_response.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
library(readxl)
library(tidyverse)
library(rvest)
file_name = "australian_marriage_law_postal_survey_2017_-_response_final.xls"
response_counts <-
readxl::read_excel(
path = file_name,
range = "Table 2!A8:P183",
col_name = c( "area",
"Yes",
"Yes pct",
"No",
"No pct",
"Response Total",
"Response Total pct",
"blank",
"Response clear",
"Response clear pct",
"Response not clear(b)",
"Response not clear(b) pct",
"Non-responding",
"Non-responding pct",
"Eligible Total",
"Eligible Total pct")
) %>%
select(-blank) %>%
tidyr::drop_na() %>%
mutate(area = gsub("\\([a-z]\\)","",area)) %>%
filter(!grepl("Total", area))
# Append electoral data
# Fetch the current and old electorates to append to table
current_electorates <-
read_html("http://www.aec.gov.au/profiles/") %>%
rvest::html_nodes("table") %>%
rvest::html_table() %>%
first() %>%
select(`Electoral division`, State, `Area (sq km)`)
old_electorates <-
read_html("http://www.aec.gov.au/Electorates/abolished.htm") %>%
rvest::html_nodes("table") %>%
rvest::html_table() %>%
first() %>%
select(`Electoral division` = Division, State)
electorates <- bind_rows(current_electorates, old_electorates)
response_data <-
response_counts %>%
left_join(electorates, by = c("area" = "Electoral division"))
write_csv(response_data, "SSM_AUS_Response.csv")
# Example State Summary
# response_data %>%
# group_by(State) %>%
# summarise(yes_count = sum(Yes), total_count = sum(`Response Total`) )