-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.R
executable file
·56 lines (50 loc) · 1.75 KB
/
process.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
#!/usr/bin/env Rscript
library(prefio)
preflib_series_number <- "00058"
preflib_abbreviation <- "nswla"
la <- read.csv("nsw_legislative_assembly_elections.csv")
# Check whether or not the preflib output directory exists:
outdir <- paste(preflib_series_number, "-", preflib_abbreviation)
if (!outdir %in% list.files()) {
dir.create(outdir)
}
# This function reads an LA election file to a preferences object
read_la <- function(rowdata) {
zipfile <- file.path("raw_data",
rowdata["year"],
paste0(rowdata["district"], ".zip"))
exdir <- tempdir()
datafile <- unzip(zipfile, exdir = exdir)
df <- read.csv(datafile, sep = "\t")
df_filtered <- df[!is.na(df$PrefCounted), ]
return(preferences(df_filtered,
format = "long",
id = "BPNumber",
item = "CandidateName",
rank = "PrefCounted",
aggregate = TRUE))
}
# This function will write each election to the required preflib dataset formats
write_la <- function(row) {
cat("Processing ",
row["district"],
" ",
row["year"],
".\n",
sep = "")
prefs <- read_la(row)
write_preflib(prefs,
file.path(outdir,
paste0(preflib_series_number,
"-",
sprintf("%08d", as.integer(row["index"])),
".",
attr(prefs$preferences, "preftype"))),
title = row["title"],
modification_date = format(Sys.time(), "%Y-%m-%d"),
modification_type = "original",
publication_date = row["published"])
}
apply(la,
1L,
write_la)