Skip to content

Instantly share code, notes, and snippets.

View TonyLadson's full-sized avatar

Tony Ladson TonyLadson

View GitHub Profile
@TonyLadson
TonyLadson / Qcode
Last active August 29, 2015 14:15
Code to summarise and visualise quality codes in hydrologic data
# see https://tonyladson.wordpress.com/2015/02/17/quality-codes-in-hydrologic-data/
library(treemap)
library(RColorBrewer)
library(ggplot2)
library(grid)
library(lubridate)
# Generate some data
@TonyLadson
TonyLadson / LeadingNA
Last active August 29, 2015 14:16
Count the number of leading (and trailing) NAs in each column of a data frame
# Usage
# LeadingNA(x)
#
# x is a data frame
# Value
# A data frame, with two rows, leading NAs and trailing NAs
LeadingNA <- function(x){
num.col <- ncol(x)
trailing.na <- rep(0, times = num.col)
@TonyLadson
TonyLadson / MissingMap
Created March 4, 2015 23:18
Map missing values using a tile plot
# Usage
# MissingMap(df, date.column, var.column)
# df = data.frame that contains a column of dates and column of variables that may have missing values
# date.column = quoted name of the column cotaining dates
# var.column = quoted name of the column containing the variable
#
# Produces a tile plot showing here missing values occur in the sequence of dates
# https://tonyladson.wordpress.com/2014/12/21/mapping-missing-data/
MissingMap <- function(df, date.column, var.column){
BwTheme = theme_bw() +
theme(
panel.background = element_rect(fill="gray98"),
axis.title.x = element_text(colour="grey20", size=20, margin=margin(20,0,0,0)),
axis.text.x = element_text(colour="grey20",size=14),
axis.title.y = element_text(colour="grey20",size=20, margin = margin(0,20,0,0)),
axis.text.y = element_text(colour="grey20",size=14),
legend.title = element_text(colour="grey20",size=12),
plot.margin = unit(c(0.5, 0.5, 1, 0.5), "cm")) # top, right, bottom, left
@TonyLadson
TonyLadson / Delta.r
Created March 25, 2015 03:07
Function to return delta as used to estimate confidence intervals for flood frequency analysis
# Function to return delta as used to estimate
# confidence intervals for flood frequency analysis
# Usage
# Delta(g, aep)
#
# g = skewness
# aep = Annual exceedance probability
#
@TonyLadson
TonyLadson / DeltaValues.r
Created March 25, 2015 03:09
The table of delta values is taken from Table 2.4, Book IV, Section 2 of Australian Rainfall and Runoff third edition
##########################################################################################################################
#
# Delta values
#
# tony.ladson at gmail.com
# 11 March 2015
#
# Used to determine confidence intervals for the Log Pearson III distribution
# The table of delta values is taken from Table 2.4, Book IV, Section 2 of Australian Rainfall and Runoff third edition.
# Confidence interval calculations are based on
@TonyLadson
TonyLadson / isGaugeNum.R
Last active August 29, 2015 14:20
Test if a string is a valid gauge number
isGaugeNum <- function(x) {
# returns true if value is missing, or a gauge number i.e. 6 digits followed by a capital letter
# otherwise returns false
if(!is.character(x)) return(FALSE)
if(is.na(x)) return(TRUE)
if(str_count(x) != 7) return(FALSE)
str_detect(x, "^[0-9]{6}[A-Z]{1}") # 6 digits followed by a capital letter
@TonyLadson
TonyLadson / isNewSiteNum.R
Created May 11, 2015 01:51
Is a value a valid new site number? i.e. 5 capital letters followed by 4 digits
isNewSiteNum <- function(x) {
# returns true if value is missing, or a site number i.e. 5 capitals follows by 4 digits
# otherwise returns false
if(is.na(x)) return(TRUE)
if(!is.character(x)) return(FALSE)
if(str_count(x) != 9) return(FALSE)
str_detect(x, "^[A-Z]{5}[:digit:]{4}") # 5 capitals followed by 4 digits
dbinom(0,100,0.01) # zero 1% floods in 100 years
dbinom(1,100,0.01) # one 1% floods in 100 years
dbinom(2,100,0.01) # two 1% floods in 100 years
dbinom(3,100,0.01) # three 1% floods in 100 years
dbinom(4,100,0.01) # four 1% floods in 100 years
dbinom(5,100,0.01) # five 1% floods in 100 years
# at least 1 1% flood in 100 years
1 - pbinom(0,100, 0.01)
@TonyLadson
TonyLadson / OneHundredYearPoisson.R
Last active November 15, 2015 21:58
Examples: Poission distribution as applied to the 100-year flood. https://tonyladson.wordpress.com/2015/06/29/100-year-flood-poisson-distribution/
dpois(0,1) # prob of zero 100-year floods in 100 years
dpois(0,1) # prob of zero 100-year floods in 100 years
[1] 0.3678794
dpois(1,1) # prob of one 100-year floods in 100 years
[1] 0.3678794
dpois(2,1) # prob of two 100-year floods in 100 years
[1] 0.1839397
# simulation