patData_aggregate: Aggregate PurpleAir Timeseries Data

Description Usage Arguments Details Value Examples

View source: R/patData_aggregate.R

Description

Aggregate a dataframe into temporal bins and apply a function. Temporal aggregation involves splitting a dataframe into separate bins along its datetime axis. FUN is mapped to the df dataframe records in each bin which are then recombined into an aggregated dataframe.

Usage

1
2
3
4
5
6
patData_aggregate(
  df,
  FUN = function(df) {     mean(df$pm25_A + df$pm25_B, na.rm = TRUE) },
  unit = "minutes",
  count = 60
)

Arguments

df

Timeseries pat data, or timeseries data.frame with valid datetime column.

FUN

The function to be applied to each vector of numeric df.

unit

Character string specifying temporal units for binning.

count

Number of units per bin.

Details

This function is intended for advanced users who wish to have more flexibility than the standard pat_aggregate() while aggregating timeseries data. FUN can operate and access all numeric vectors within the data frame df and must return a matrix or tibble of numeric values. Any errors generated during application of FUN on subsets of df must be handled as in the example.

Value

Returns an aggregated data.frame 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
library(AirSensor)

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

# Two Sample Student T-Test (advanced users only - see details.)
FUN_ttest <- function(x) {
  result <- try({
    hourly_ttest <- stats::t.test(x$pm25_A, x$pm25_B, paired = FALSE)
    tbl <- dplyr::tibble(
      t_score = as.numeric(hourly_ttest$statistic),
      p_value = as.numeric(hourly_ttest$p.value),
      df_value = as.numeric(hourly_ttest$parameter)
    )
  }, silent = TRUE)
  if ( "try-error" %in% class(result) ) {
    tbl <- dplyr::tibble(
      t_score = as.numeric(NA),
      p_value = as.numeric(NA),
      df_value = as.numeric(NA)
    )
  }
  return(tbl)
}

t.testStats <-
  pat %>%
  pat_extractData() %>% # Note: Extract the timeseries data.frame
  patData_aggregate(FUN_ttest)
  
head(t.testStats)

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