A short description of the post.
The Australian Bureau of Statistics 2017 Marriage Law Postal survey contains data about a nationwide vote that took place by mail. The data has a few things making reading into a tibble difficult:
To make this data usable we must transform its structure in both its column layout and its rows.
We read in the Excel file’s third sheet, cut it down to only the needed variables and rows, rename the columns, and remove NA values. We also remove rows that contain section totals.
library(tidyverse)
library(readxl)
votes <- read_excel("../../_data/australian_marriage_law_postal_survey_2017_-_response_final.xls",
sheet="Table 2",
skip=7,
col_names = c("Town", "Yes", "d", "No", rep("d", 6), "Illegible", "d", "No Response", rep("d", 3)))%>%
select(!starts_with("d"))%>%
drop_na(Town)%>%
filter(!str_detect(Town, "(Total)"))%>%
filter(!str_starts(Town, "\\("))
The last step is more complicated. Each observation needs a variable for is administrative “division”, but this is displayed at the top of each block. These junk rows listing the parent division names must be turned into a variable for each row.
We get the number of each row that contains " Divisions"
.
votes<- votes%>%
mutate(Divisions = case_when(
str_ends(Town, "Divisions") ~ Town,
TRUE ~ NA_character_
))
for(i in 1:length(votes$Divisions)){
votes$Divisions[i]<-ifelse(is.na(votes$Divisions[i]),votes$Divisions[i-1], votes$Divisions[i])
}
votes<- filter(votes,!str_detect(Town, "Divisions|Australia"))
Text and figures are licensed under Creative Commons Attribution CC BY-NC 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".
For attribution, please cite this work as
Tacheron (2021, Aug. 12). DACSS 601 August 2021: Wrangling the Australian marriage law dataset. Retrieved from https://mrolfe.github.io/DACSS601August2021/posts/2021-08-12-wrangling-the-australian-marriage-law-dataset/
BibTeX citation
@misc{tacheron2021wrangling, author = {Tacheron, Karl}, title = {DACSS 601 August 2021: Wrangling the Australian marriage law dataset}, url = {https://mrolfe.github.io/DACSS601August2021/posts/2021-08-12-wrangling-the-australian-marriage-law-dataset/}, year = {2021} }