R code - Handy routines for hydrologists


What is R?

R is an open source programming language and environment for data analysis. It has rich functionality for data processing, analysis and graphing. See the R home page for more information and to download the package. Be forewarned that R has a steep learning curve. However, once you gain proficiency in R, the time invested in learning R will be paid back many times over.


R and hydrology

Data analysis is an integral part of hydrology. Hydrologists frequently use techniques, such as regression analysis, which are incorporated into conventional statistical packages and spreadsheet software. However, many hydrological analyses are not, including intensity-duration-frequency analysis and flood frequency analysis. These analyses are relatively simple to code in R.


CSHShydRology

CSHShydRology is an R package of functions developed by Canadian hydrologists. The name is in recognition of the support provided by the Canadian Society for Hydrological Sciences (CSHS), which is an affiliated society of the Canadian Water Resources Association (CWRA). The project's website is https://cshs.cwra.org/hydrology/.

Many of my sample scripts will be superceded by functions within CSHShydRology.


R primers and background material

In the following essay, Dave and I outline the advantages of using R for hydrologic analyses, particularly the fact you can generate integrated workflows that encompass all steps, from initial data cleaning to analysis to reporting.

R scripts for specific hydrologic analyses

What I have included here are bits of R code that hydrologists may find useful. If you have written some code that you would like to share, please send it along and I'll consider posting it here.

You are free to use, distribute and modify the code below. If you do so, please send me an email to let me know which scripts you have used.

Disclaimer: All of the code presented on this page is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. The code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

Note: I am an old dog who generally avoids learning new tricks. I began my programming career with WATFIV and punch cards and then progressed through FORTRAN 77, Turbo Pascal and Fortran 90/95 before discovering R, which satisfies (almost) all of my current programming/data analysis requirements. I have recently learned how to incorporate Fortran subroutines into R code to improve the performance of a time-stepping model that uses nested `for` loops.

Most of my older code uses base R, with more recent code making increasing use of tidyverse packages, especially dplyr. Although I use ggplot graphics occasionally for plots that involve grouping or conditioning, I tend to use base graphics for complex plots that require subtantial customization. I also prefer the clean layout of base graphics, even compared to the more stripped-down themes available for ggplot.

The code for catchment and channel delineation is an early working version of a script that is currently being upgraded to the standards required for inclusion in the CSHShydRology package. However, the script has been tested on a range of data sets, and could be incorporated into other users' workflow with relatively minor edits.