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.
patData_aggregate(
df,
FUN = function(df) {
mean(df$pm25_A + df$pm25_B, na.rm = TRUE)
},
unit = "minutes",
count = 60
)
Timeseries pat data, or timeseries data.frame with valid datetime column.
The function to be applied to each vector of numeric df
.
Character string specifying temporal units for binning.
Number of units per bin.
Returns an aggregated data.frame object.
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.
library(AirSensor)
# Single day subset
pat <-
example_pat %>%
pat_filterDate(20220702, 20220703)
# 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)
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in array-vector arithmetic is deprecated.
#> Use c() or as.vector() instead.
#> Warning: Recycling array of length 1 in vector-array arithmetic is deprecated.
#> Use c() or as.vector() instead.
head(t.testStats)
#> # A tibble: 6 × 6
#> datetime t_score p_value df_value datetime_A
#> <dttm> <dbl> <dbl> <dbl> <dttm>
#> 1 2022-07-02 07:00:00 -10.4 7.33e-11 26.5 2022-07-02 07:00:00
#> 2 2022-07-02 08:00:00 -7.31 2.47e- 7 22.1 2022-07-02 08:00:00
#> 3 2022-07-02 09:00:00 -5.08 3.74e- 3 5.05 2022-07-02 09:00:00
#> 4 2022-07-02 10:00:00 -10.4 2.29e- 6 9.11 2022-07-02 10:00:00
#> 5 2022-07-02 11:00:00 -7.51 2.18e- 7 21.1 2022-07-02 11:00:00
#> 6 2022-07-02 12:00:00 -12.9 3.08e-13 27.6 2022-07-02 12:00:00
#> # ℹ 1 more variable: datetime_B <dttm>