pat_aggregate: Aggregate PurpleAir Timeseries Object

Description Usage Arguments Details Value Examples

View source: R/pat_aggregate.R

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

1
2
3
4
5
6
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
library(AirSensor)

# Single day subset
pat <- 
  example_pat %>% 
  pat_filterDate(20180813, 20180814)

# 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)

AirSensor documentation built on March 13, 2021, 1:07 a.m.