knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

The wqbc R package calculates water quality limits for British Columbia.

Previously it also calculated the CCME Water Quality Index but that functionality has been moved to the wqindex package.

Fraser Data

The data used in this demonstration are from the Fraser River basin (data available here under the Candian Open Government License.

To load the wqbc package and the fraser data run

library(wqbc)
data(fraser)

The fraser data is organized so that each row corresponds to one observation.

library(tibble) # for prettier printing of tibbles
print(fraser)

As the fraser data is a large dataset (r nrow(fraser) rows) we'll just use the data from 2012.

data2012 <- dplyr::filter(fraser, lubridate::year(Date) == 2012)
data2012

This leaves us with a dataset with r nrow(data2012) rows. Before the data can be assigned the water quality limits provided by wqbc they first have to be standardized and then cleaned.

Standardizing Data

The standardize_wqdata() function converts any non-standard variable names, checks (and if possible) converts the units and removes any missing and negative values.

data2012 <- standardize_wqdata(data2012)

As a result of the standardization, the 2012 Fraser dataset has been reduced to r nrow(data2012) observations that can in principle be assigned water quality limits by the wqbc package. However, first it is necessary to deal with multiple observations by cleaning the data.

as_tibble(data2012)

Cleaning Data

After standardization, it is necessary to ensure that there are only single values for each date for a given variable and this is done using the function clean_wqdata().

data2012 <- clean_wqdata(data2012, by = "SiteID")

The end result is a data frame with r nrow(data2012) rows each of which represents the average value for a single variable on a particular date at an individual SiteID.

as_tibble(data2012)

Calculating Limits

Once the data have been standardized and cleansed the final task is to determine the water quality limits for each observation using the calc_limits() function.

data2012 <- calc_limits(data2012, by = "SiteID", term = "short")

The final result is a data frame with r nrow(data2012) rows each of which has an upper water quality limit.

as_tibble(data2012)

Calculating Water Quality Index

The resultant data can be used to calculate water quality indices using the calc_wqi() function of the wqindex package.



bcgov/wqbc documentation built on Feb. 11, 2023, 11:15 p.m.