average_ts: Summarise a time series in bins

View source: R/average_ts.R

average_tsR Documentation

Summarise a time series in bins

Description

This function averages a time series (i.e., all of the values of a response variable collected through time) in each of a series of user-specified bins. The function can handle independent time series (e.g., for multiple individuals), average numeric explanatory variables alongside the response, incorporate other 'static' variables (e.g., the sex of each individual) and implement different types of averages.

Usage

average_ts(
  dat,
  split = NULL,
  timestamp,
  response,
  breaks,
  fun = mean,
  first = NULL,
  average = NULL,
  as_time = as.POSIXct,
  order = c(timestamp, response, first, average),
  verbose = TRUE,
  ...
)

Arguments

dat

A dataframe which includes observations collected over time.

split

(optional) A character that defines the name of the column in dat that distinguishes independent time series.

timestamp

A character that defines the name of the column in dat that contains time stamps, to be grouped into bins.

response

A character that defines the name of the column in dat that contains the values of the response to be averaged in each bin.

breaks

A number or vector which defines the number of unique cut points or the unique cut points themselves at which each independent time series is cut into bins. This is passed to cut.

fun

A function used to calculate the average response and the average value of any other other numeric variable specified in average in each bin.

first

(optional) A character vector that defines the name(s) of the column(s) in dat for which the first observation for each independent time series should be retained in the averaged dataframe. This is useful for 'static' variables which do not change through time (e.g., the sex of each individual).

average

(optional) A character vector that defines the name(s) of any other column(s) in dat that need to be averaged in each bin, like response.

as_time

A function that converts a character vector of breaks into a vector of times. The default function is as.POSIXct.

order

A character vector of column names that defines the order of desired columns in the returned dataframe.

verbose

A logical input that defines whether or not to return messages to the console to monitor function progress.

...

Additional arguments passed to cut.

Value

The function returns a dataframe in which time stamps have been aggregated into a sequence of bins and the value of the response and any other numeric columns requested have been averaged over all the data in each bin. For static variables (e.g., the sex of an individual), the first observation for each independent time series may also be included.

Author(s)

Edward Lavender

See Also

summarise_in_bins

Examples

dat_flapper_av <- average_ts(dat = dat_flapper,
                             split = "id",
                             timestamp = "timestamp",
                             response = "depth",
                             breaks = "hours",
                             first = "id",
                             average = "temp"
                             )
utils::head(dat_flapper_av)

edwardlavender/Tools4ETS documentation built on Nov. 29, 2022, 7:41 a.m.