Skip to content

Instantly share code, notes, and snippets.

View TonyLadson's full-sized avatar

Tony Ladson TonyLadson

View GitHub Profile
@TonyLadson
TonyLadson / Count_censored.R
Created August 22, 2016 02:35
Function to count the number of censored values in columns of a data frame. Counts the occurrence of '<' and '>'
Count_censored <- function(my.df) {
# number left censored
.Count_leftCensored <- function(x) {
sum(str_detect(x, '[<]'), na.rm = TRUE)
}
.Count_rightCensored <- function(x) {
sum(str_detect(x, '[>]'), na.rm = TRUE)
}
@TonyLadson
TonyLadson / Check_col.R
Last active September 12, 2016 03:53
Function to check and summarise numeric data in the column of a data frame. Counts missing, censored etc.
# Arguements:
# my_df - name of a data frame
# my_col - name of column to check and summarise e.g. 'DO'
#
# Usage
# Check_col(my_df, my_col)
#
# Value
# Outputs a dataframe with summarised values
@TonyLadson
TonyLadson / CheckPlot_DO
Last active August 22, 2016 06:57
Function to plot DO as a function of temperature and overlay contours of percentage saturation
# my_df - data frame with DO and temperature data
# DO - (quoted) name of column of DO values (mg/L) e.g. 'DO'
# temp - (quoted) name of column of temperature values (degrees Celcius) e.g. 'temp'
# outputs a graph
CheckPlot_DO <- function(my_df, DO_col, temp_col) {
DO <- my_df[[DO_col]]
temp <- my_df[[temp_col]]
@TonyLadson
TonyLadson / Time_covert_excel.R
Last active September 9, 2016 01:23
Function to convert time values read in from excel into fractions of a day. Tries to deal with special cases. See https://tonyladson.wordpress.com/2016/08/08/data-cleaning-times/
# Function to convert a time value to a fraction of a day
#
# Intended to be used for time data from Excel
#
# x is the time
#
# if x is between 0 and 1 it is assumed to be a fraction of a day and returned
# if x is > 24 the decimal part is assumed to represent a fraction of a day and is returned
# # if x contains a colon it is assumed to be in the form HH:MM:SS and converted to
# a fraction of a day.
@TonyLadson
TonyLadson / Storms.R
Created August 28, 2016 23:54
Storm occurrence by day of the week
library(ggplot2)
library(dplyr)
library(lubridate)
library(cowplot)
# Storms on days of the week
# Storm archive
# http://www.bom.gov.au/australia/stormarchive/
# Victoria
# 1 Jan 2010 to present
library(plotrix)
library(RColorBrewer)
###################
# The slopegraphs use the plotrix::bumpchart function
# Here I've tweaked plotrix::bumpchart to increase the spacing between the labels
#
@TonyLadson
TonyLadson / Plot_col.R
Created September 9, 2016 05:56
Plot time series and histograms of columns of a data frame. Both non-logged and logged versions are produced
# my_col - name of column to plot, must be quoted
# datetime_col - name of column that contains datetime information
# my_df - name of dataframe
#
# Plot graphs for the whole time series
#
# pdf('fname.pdf')
# lapply(names(my_df), FUN = Plot_col, 'datetime_col', my_df)
# dev.off()
@TonyLadson
TonyLadson / Outlier.R
Created September 12, 2016 03:16
Functions to assist in identifying and investigating outliers in water quality data.
# function to calculate k of the max and min of a dataset
# k > 3 suggests data are 'far out'
Tukey_k <- function(x){
my.quantile <- quantile(x, na.rm = TRUE)
Q_25 <- my.quantile[2]
Q_75 <- my.quantile[4]
k_max <- as.vector((max(x, na.rm = TRUE) - Q_75)/(Q_75 - Q_25))
k_min <- as.vector((Q_25 - min(x, na.rm = TRUE))/(Q_75 - Q_25))
---
title: "Events_blog"
author: "Tony Ladson"
date: "3 October 2016"
output:
html_fragment:
fig_caption: yes
fig_height: 4
fig_width: 6
---
@TonyLadson
TonyLadson / IL.R
Created November 8, 2016 01:07
Initial loss: storm v burst. Code for the blog at https://tonyladson.wordpress.com/2016/11/08/initial-loss-storm-v-burst/
library(ggplot2)
library(RColorBrewer)
library(grid)
library(cowplot)
ILb_hill <- function(d, MAR = 700){
1 - 1/(1+142*sqrt(d)/MAR)
}