knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>", 
  warning = T, 
  message = F
)

The MassWateR package includes several "utility" functions (using the util prefix) that are used inside other functions to accomplish routine tasks. Although these functions will typically not be used by themselves, they may be useful for custom analyses. The full list of utility functions can be viewed on the functions page. Some of these are described below.

Most of the utility functions require only the results data, data quality objectives file for accuracy, or the site metadata file. Note that all of the utility functions require data as input, whereas most of the primary MassWateR functions can accept a file path or the data. The fset argument also does not apply to the utility functions.

library(MassWateR)

# import results data
respth <- system.file("extdata/ExampleResults.xlsx", package = "MassWateR")
resdat <- readMWRresults(respth)

# import accuracy data
accpth <- system.file("extdata/ExampleDQOAccuracy.xlsx", package = "MassWateR")
accdat <- readMWRacc(accpth)

# import site metadata
sitpth <- system.file("extdata/ExampleSites.xlsx", package = "MassWateR")
sitdat <- readMWRsites(sitpth)

Fill data outside of detection or quantitation limit

The utilMWRlimits() function replaces any text qualifiers in the "Result Value" column of the results file that are outside of detection ("BDL" or "AQL") with appropriate numeric values. The values are replaced with those from the "Quanitation Limit" column, if present, otherwise the "MDL" or "UQL" columns from the data quality objectives file for accuracy are used. Values as "BDL" use one half of the appropriate limit. The output only includes rows with the activity type as "Field Msr/Obs" or "Sample-Routine" and is filtered by the chosen parameter. The function can be useful for analysis because the measurements are returned as numeric values (e.g., for plotting).

utilMWRlimits(resdat = resdat, accdat = accdat, param = 'TP')

Filter results data

The utilMWRfilter() function filters rows in the results data by parameter, date range, sites, result attributes, and/or location groups. No other changes to the data are made other than filtering the relevant rows based on the input. The function is designed to provide informative errors, e.g., the available result attributes will be shown in the error if the attribute provided is invalid. Filtering by location group requires the site metadata file.

# filter by parameter, date range
utilMWRfilter(resdat = resdat, param = 'DO', dtrng = c('2022-06-01', '2022-06-30'))

# filter by parameter, site
utilMWRfilter(resdat = resdat, param = 'DO', site = c('ABT-026', 'ABT-062', 'ABT-077'))

# filter by parameter, result attribute
utilMWRfilter(resdat = resdat, param = 'DO', resultatt = 'DRY')

# filter by parameter, location group, date range
utilMWRfilter(resdat = resdat, param = 'DO', sitdat = sitdat,
     locgroup = 'Assabet', dtrng = c('2022-06-01', '2022-06-30'))

Summarize parameters by group

The utilMWRsummary() function summarizes results data for a parameter, either across all rows or by arbitrary row groups, e.g., mean concentrations for sites across dates. The utilMWRlimits() function must be used first and a grouping variable can be specified using group_by() from the dplyr{target="_blank"} package (included with MassWateR). The default is to summarize as the mean or geometric mean based on information in the data quality objective file for accuracy.

library(dplyr)

# fill BDL, AQL
resdat <- utilMWRlimits(resdat = resdat, accdat = accdat, param = "DO")

# identify site as grouping variable
dat <- resdat %>% 
  group_by(`Monitoring Location ID`)

# arithmetic means   
utilMWRsummary(dat = dat, accdat = accdat, param = "DO", confint = TRUE)

# geometric means
utilMWRsummary(dat = dat, accdat = accdat, param = "E.coli", confint = TRUE)

Alternative summary options can be used with the sumfun argument and include "mean", "geomean", "median", "min", or "max". Using "mean" or "geomean" will override any information in the accuracy file. Additionally, confidence intervals can only be returned if sumfun is "auto", "mean", or "geomean". Here, the median is returned.

# median E. coli
utilMWRsummary(dat = dat, accdat = accdat, param = "E.coli", confint = FALSE, sumfun = 'median')

Get surface measurements only

The utilMWRfiltersurface() function returns only surface measurements from the results data. The function will filter the results where "Activity Depth/Height Measure" is less than or equal to 1 meter or 3.3 feet or the "Activity Relative Depth Name" is "Surface", where the latter takes precedent.

utilMWRfiltersurface(resdat = resdat)


massbays-tech/MassWateR documentation built on April 12, 2025, 7:53 p.m.