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 )
df | Timeseries pat data, or timeseries data.frame with valid datetime column. |
---|---|
FUN | The function to be applied to each vector of numeric |
unit | Character string specifying temporal units for binning. |
count | 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(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)#> 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 x 6 #> datetime t_score p_value df_value datetime_A #> <dttm> <dbl> <dbl> <dbl> <dttm> #> 1 2018-08-13 07:00:00 -3.87 2.14e-4 84.0 2018-08-13 07:00:00 #> 2 2018-08-13 08:00:00 -4.79 6.91e-6 87.2 2018-08-13 08:00:00 #> 3 2018-08-13 09:00:00 -2.87 5.13e-3 87.6 2018-08-13 09:00:00 #> 4 2018-08-13 10:00:00 -3.43 9.32e-4 87.2 2018-08-13 10:00:00 #> 5 2018-08-13 11:00:00 -4.98 3.34e-6 84.1 2018-08-13 11:00:00 #> 6 2018-08-13 12:00:00 -4.53 1.84e-5 87.4 2018-08-13 12:00:00 #> # … with 1 more variable: datetime_B <dttm>