Skip to content

Instantly share code, notes, and snippets.

View nacnudus's full-sized avatar

Duncan Garmonsway nacnudus

View GitHub Profile
@nacnudus
nacnudus / gist:6723265
Last active December 24, 2015 01:19
ggplot axis time format HH:mm:ss
timeHMS_formatter <- function(x) {
x[is.na(x)] <- 0
h <- trunc(x * 24)
m <- trunc(x * 24 * 60)
s <- x * 24 * 60 * 60 %% 60
s <- round(s, 2)
lab <- sprintf('%02d:%02d:%02d', h, m, s) # Format the strings as HH:MM:SS
lab <- gsub('^00:', '', lab) # Remove leading 00: if present
lab <- gsub('^0', '', lab) # Remove leading 0 if present
}
@nacnudus
nacnudus / uk-civil-service-staff-survey.R
Created November 16, 2017 17:57
Bind the UK Civil Service Staff Survey 2017 to the GOV.UK register of government organisations
#' Match the Civil Service People Survey to the government-organisation register
library(tidyverse)
library(RegistersClientR)
#' Download the survey and the register
survey_path <- "https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/659571/Civil_Service_People_Survey_2017_All_Organisation_Scores__CSV_.csv"
survey <- read_csv(survey_path)[, 1:5]
govorg <-
@nacnudus
nacnudus / participation.R
Last active November 19, 2017 23:14
Import the Australian Marriage Law Postal Survey, 2017 into R
# Import the participation data
library(tidyverse)
library(tidyxl) # You'll need the dev versions devtools::install_github("nacnudus/tidyxl")
library(unpivotr) # You'll need the dev versions devtools::install_github("nacnudus/unpivotr")
library(here)
path <- here("inst", "extdata", "australian_marriage_law_postal_survey_2017_-_participation_final.xlsx")
book <- xlsx_cells(path)
formats <- xlsx_formats(path)
@nacnudus
nacnudus / register-creation-dates.R
Created April 19, 2018 10:33
When each register was 'ready to use'
# To install dependencies
# install.packages(c("devtools", "tidyverse"))
# install_github("openregister/RegistersClientR")
library(tidyverse)
library(RegistersClientR)
register <- rr_register("register")
register %>%

Keybase proof

I hereby claim:

  • I am nacnudus on github.
  • I am nacnudus (https://keybase.io/nacnudus) on keybase.
  • I have a public key ASCRo_YM6aOXHQOlSQTyw8plq2ubGwakYHS_kdQ07xiQ9wo

To claim this, I am signing this object:

@nacnudus
nacnudus / register-to-database.R
Last active June 26, 2018 09:56
Various ways to download a GOV.UK register and load it into a database
# Load all registers into various databases
library(tidyverse)
library(DBI)
library(registr) # https://github.com/nacnudus/registr
library(stringr)
library(getPass)
library(here)
beta <- rr_registers()
library(tidyxl)
library(tidyverse)
library(unpivotr)
library(rvest)
library(stringr)
library(ggrepel)
download.file("https://smartstart.services.govt.nz/assets/files/Top-baby-names-1954-2016.xlsx",
destfile = "babynames.xlsx", mode = "wb")
babies <- tidy_xlsx("babynames.xlsx")
@nacnudus
nacnudus / luxembourg-time-use-unpivotr.R
Created September 15, 2018 10:02
Tidy a spreadsheet of the Luxembourg Time Use Survey with unpivotr
# Inspired by http://www.brodrigues.co/blog/2018-09-11-human_to_machine/
# https://twitter.com/brodriguesco/status/1039604517287931904
# "You can find the data I will use here. Click on the “Time use” folder and you can download the workbook."
# http://statistiques.public.lu/stat/ReportFolders/ReportFolder.aspx?IF_Language=eng&MainTheme=3&FldrName=1&RFPath=14306
library(tidyverse)
library(tidyxl)
library(unpivotr)
library(lubridate)
@nacnudus
nacnudus / luxembourg-time-use-unpivotr-experimental.R
Created September 15, 2018 10:08
Tidy a spreadsheet of the Luxembourg Time Use Survey with experimental unpivotr branch
# Inspired by http://www.brodrigues.co/blog/2018-09-11-human_to_machine/
# https://twitter.com/brodriguesco/status/1039604517287931904
# "You can find the data I will use here. Click on the “Time use” folder and you can download the workbook."
# http://statistiques.public.lu/stat/ReportFolders/ReportFolder.aspx?IF_Language=eng&MainTheme=3&FldrName=1&RFPath=14306
# This time using experimental unpivotr code to allow custom filtering of header cells, rather than having to reposition them.
# https://github.com/nacnudus/unpivotr/commit/0961ec3c3e17b34755f0fce94db7f5bf380d43ce
library(tidyverse)
library(tidyxl)
library(tidyverse)
library(ompr)
library(ompr.roi)
library(ROI.plugin.glpk)
M <- 3 # Volunteers (rows)
N <- 4 # Jobs (combination of role at given time and location) (columnss)
# Jobs are:
# 1. Greet (8am-9am)