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){
@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)
library(ggplot)
library(grid)
library(devtools)
library(dplyr)
library(lubridate)
source_gist('cc60bbb3cbadf0e72619') # ggplot theme
# Data is stored in a data frame with the date, flow and quality code
# You'll need to provide your own data for this to work.
# Geometric Distribution of 1% floods
# probability of 4 or fewer safe years
pgeom(4, 0.01)
# [1] 0.04900995
# probability of exactly 4 safe years and then a flood in the 5th
dgeom(4, 0.01)
#[1] 0.00960596