Skip to content

Instantly share code, notes, and snippets.

View johnDorian's full-sized avatar

John Dorian johnDorian

View GitHub Profile
### simulate some dates
dates <- seq.Date(as.Date("2014-01-01"), as.Date("2014-12-31"), by = "1 day")
### simulate some rainfall
rain <- sample(c(0,0,0,2,3), length(dates), replace=T)
data <- data.frame(date = dates,
daily_precip = rain)
# Use a rolling apply function to check the rainfall of the last 3 days including the day in question.
library(zoo)
@johnDorian
johnDorian / rbind_ggplot_example.R
Created November 7, 2014 10:27
An example of using rbind_ggplot
library(ggplot2)
library(lubridate)
libarry(ggthemes)
# Create a series of dates (hourly)
hourly_date <- seq.POSIXt(dmy("01012012"), dmy("31122012"), by = "1 hour")
# Create some fictional fdom data.
fdom <- data.frame(date = hourly_date, fdom = sin(1:length(hourly_date)/300)+10)
# Create a series of dates (daily)
daily_date <- seq.POSIXt(dmy("01012012"), dmy("31122012"), by = "1 day")
@johnDorian
johnDorian / modify_hour.r
Created January 19, 2015 13:46
modify hour of a subset using lubridate
library(lubridate)
## A synthetic data set.
doc <- data.frame(date=ymd_hm("2014-01-01 14:00") + (1:360)* 24*3600)
# Find all the dates before a certain time period.
index <- doc$date < ymd("2014-06-01")
## use the hour function to extract the hour of each day e.g
hour(doc$date)
## Now using the index from above you can subtract an hour from each of the times
# using the index vector and reassiging the modified hour back to the time stamp.
hour(doc$date[index]) <- hour(doc$date[index]) - 1
@johnDorian
johnDorian / rolling_mean.r
Last active August 29, 2015 14:14
Example of aggregating a variable through time.
set.seed(1082)
# Create two weeks of 15 minute data
library(lubridate)
dates <- seq.POSIXt(dmy_hm("01-01-2013 00:00"), dmy_hm("31-12-2013 11:45"), by = "15 mins")
# Add a column with a random variable with mean = 10
temp <- data.frame(date = dates, temp = rnorm(length(dates))+10)
plot(temp, type='l')
# aggreagate the data to daily by converting the POSIX date to date format (i.e drop the timestamp)
daily_temp <- aggregate(temp$temp, list(date=as.Date(temp$date)), mean)
# fix the names up
@johnDorian
johnDorian / load_and_save_multiple.r
Created February 2, 2015 09:42
An example to create, load, manipulate and save multiple csv files with R
### create some fictional data
dir.create("tmp")
setwd("tmp")
for(i in 1:10){
tmp_file <- data.frame(x=1:100, y=rnorm(100), z = rnorm(100))
tmp_file_name <- paste0(paste(letters[sample(1:24, 10)], collapse= ""), ".csv")
write.csv(tmp_file, tmp_file_name, row.names= FALSE)
}
@johnDorian
johnDorian / qsu_standard_conversions.r
Created March 9, 2015 12:57
QSU - Fdom conversions
## The limits of the instrument (milivolts) and the sensor (QSU)
mv_conv <- data.frame(mv_limits = c(10.85, 4965),qsu_limits = c(0,200))
## Plot the raw data to get an idea of what is hapenning
plot(qsu_limits~mv_limits, mv_conv, xlab="mV (datalogger)",
ylab = "Sensor limits (QSU)")
## Fit a model of the relationship
mv_conv_mod <- lm(qsu_limits~mv_limits, mv_conv)
## Add the trend model to the plot
abline(mv_conv_mod)
## An example to estimate the mV (Should be close to 100 QSU.)
@johnDorian
johnDorian / example.r
Last active August 29, 2015 14:17
A fast (Rcpp) version of the Nash–Sutcliffe model efficiency coefficient
# Load the libraries
library(Rcpp)
library(hydroGOF)
# Load the source cpp file with a few gof values.
sourceCpp("gof.cpp")
# Create some data to test and compare the two functions.
obs <- rnorm(10)
sim <- rnorm(10)
# Compare the two functions
NSE_fast(sim,obs)
setwd("~/Documents/code/francesca")
library(TSAgg)
quality<-read.csv("Burke_worked.csv",header=T)
head(quality)
q2<-timeSeries(quality$date,"%d/%m/%Y %H:%M")
#####################################################################################################
### Aim: To test the significance of randomly allocating flow to 'midnight' water quality samples.
### Date Created: Thursday 2nd September 2010
### Author: Jason Lessels
### Packages required: TSAgg_0.2-1,geoR
### Notes: The script can easily be modified for the other water quality parameters, and the amount of simulations. Things to check:
###Both WQ and discharge must have the same initial time stamp (hours) before aggregation.
###WQ variable modify lines 52,60,146,186
###line 99 changes the amount of simulations to run.
###line 156 determines when the bushfire occurred.
@johnDorian
johnDorian / piper.R
Created May 1, 2013 09:53
piper diagrams from the hydrogeo package
piper <-
function (data, group = NULL, colours = NULL, pch = NULL, numbersymbols = FALSE,
X = 300, ...)
{
p <- (X/11)
q <- (X/22)
over100 <- data[data$Ca + data$Mg > 100 | data$Cl + data$SO4 >
100, ]
if (length(over100[, 1]) != 0) {
print("ERROR")