pat_aggregate: Aggregate PurpleAir Timeseries Object

View source: R/pat_aggregate.R

pat_aggregateR Documentation

Aggregate PurpleAir Timeseries Object

Description

Aggregate PurpleAir timeseries (pat) object along its datetime axis. Temporal aggregation involves splitting a pat object into separate bins along its datetime axis. FUN is mapped to the pat numeric variables in each bin, which are then recombined into an aggregated pat object containing the same metadata as the incoming pat.

Usage

pat_aggregate(
  pat,
  FUN = function(x) {
     mean(x, na.rm = TRUE)
 },
  unit = "minutes",
  count = 60
)

Arguments

pat

PurpleAir Timeseries pat object.

FUN

The function to be applied to each vector of numeric pat data.

unit

Character string specifying temporal units for binning.

count

Number of units per bin.

Details

FUN must operate on univariate numeric vectors and return a scalar value. Besides the data variable, no additional arguments will be provided to this function. This means that functions like mean and max will need to be wrapped in a function that specifies na.rm = TRUE. See the examples below.

Value

Returns an aggregated pat object.

Examples

library(AirSensor)

# Single day subset
pat <- 
  example_pat %>% 
  pat_filterDate(20220702, 22020703)

# Create aggregation functions
FUN_mean <- function(x) mean(x, na.rm = TRUE)
FUN_max <- function(x) max(x, na.rm = TRUE)
FUN_count <- function(x) length(na.omit(x))

# Hourly means
pat %>%
  pat_aggregate(FUN_mean) %>% 
  pat_extractData() %>% 
  dplyr::select(1:9)

# Hourly maxes
pat %>%
  pat_aggregate(FUN_max) %>% 
  pat_extractData() %>% 
  dplyr::select(1:9)

# Hourly counts
pat %>%
  pat_aggregate(FUN_count) %>% 
  pat_extractData() %>% 
  dplyr::select(1:9)

# Alternative 10 minute aggregation (advanced users only - see details.)
pat %>%
  pat_aggregate(FUN_max, unit = "minutes", count = 10) %>%
  pat_extractData() %>%
  dplyr::select(1:9) %>%
  dplyr::slice(1:6)

MazamaScience/AirSensor documentation built on April 28, 2023, 11:16 a.m.