R/aggregate_thresholds.R

Defines functions aggregate_thresholds

Documented in aggregate_thresholds

#' Aggregate activity thresholds
#'
#' \code{aggregate_thresholds} aggregates the daily activity thresholds (return
#'   of \code{\link{activity2thresholds}} by \code{period}.
#'
#' @param thresholds A data.table with the activity thresholds (return of
#'   \code{\link{activity2thresholds}} with additional column
#'   \code{threshold_period)} by which to aggregate the threshold values.
#'
#' @examples
#' aggregate_tresholds(thresholds = activity_thresholds_final)
#'
#' @import data.table
#' @export

aggregate_thresholds <- function(thresholds) {
  if (!('threshold_period' %in% colnames(thresholds))){
    stop("You have to add a column <threshold_period> by which the thresholds
  will be aggregated.")
  } else {
  thresholds[!is.na(threshold_a),
            .(threshold_a = round(mean(threshold_a, na.rm = T)),
              threshold_a_se = round(sd(threshold_a, na.rm = T)/sqrt(.N),2),
              threshold_b = round(mean(threshold_b, na.rm = T)),
              threshold_b_se = round(sd(threshold_b, na.rm = T)/sqrt(.N),2),
              threshold_c = round(mean(threshold_c, na.rm = T)),
              threshold_c_se = round(sd(threshold_c, na.rm = T)/sqrt(.N),2)),
             by = .(animal_tag,
                    threshold_period)]
    }
}
maxkroeschel/activitytools documentation built on July 31, 2023, 1:34 p.m.