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 / xml_to_df.R
Last active March 22, 2023 05:01
Convert xml to a nested data frame
``` r
library(xml2)
library(dplyr)
library(purrr)
library(stringr)
# From the root node:
# If has_children, then recurse.
# Otherwise, attributes, value and children (nested) to data frame.
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 / data.table-joins.R
Created August 21, 2017 09:20
How to do joins with data.table
library(data.table)
?`[.data.table`
DT <- data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
X <- data.table(x=c("c","b"), v=8:7, foo=c(4,2))
colnames(DT)
# [1] "x" "y" "v"
@nacnudus
nacnudus / rcpp-na-vs-std-vector.R
Created September 15, 2017 17:26
Rcpp NA values vs std::vector<something>
# Rcpp NAs vs std::vector
library(Rcpp)
cppFunction("
List test1() {
return List::create(
NumericVector::create(NA_REAL),
IntegerVector::create(NA_INTEGER),
LogicalVector::create(NA_LOGICAL),
CharacterVector::create(NA_STRING));
@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()
@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: