R/activity_aggregate_tRackIT.R

Defines functions activity_aggregate_tRackIT

Documented in activity_aggregate_tRackIT

#' activity aggregatio
#'
#' @description aggregates the activity classification to user specified intervalls of "avalue" minutes by using the class with max entries (mode) in interval
#'
#'
#' @author Jannis Gottwald
#'
#' @param animal list, list generated by initAnimal function
#' @param avalue numeric, aggregation interval in minutes
#' @param tzone string, Timezone of time data
#' @param get_data logical, if TRUE classification will be returned as data.frame and stored in the classification folder. If FALSE classification will only be stored
#'
#' @export
#'
#' @examples
#' #projroot<-paste0(getwd(),"/tRackIT_test_data/")
#' #anml<-getAnimal(projroot =projroot, animalID = "woodpecker")
#' #activity_aggregate_tRackIT(animal = anml, avalue = 1, tzone = "CET")
#'


activity_aggregate_tRackIT <- function(animal, avalue, tzone, get_data=FALSE) {

  # calculates the most frequent value
  Mode <- function(x) {
    ux <- unique(x)
    ux <- ux[!is.na(ux)]
    ux[which.max(tabulate(match(x, ux)))]
  }

  if (is.null(animal)) {
    stop("No animal file provided. Please see ?initAnimal or ?getAnimal")
  }


  # list files per id and get data

  fls <- list.files(animal$path$classification, full.names = TRUE, pattern = "classified")

  if (length(fls) == 0) {
    stop(paste0("No files found in ", animal$path$classification))
  }

  data <- plyr::ldply(fls, function(x) {
    data.table::fread(x)
  })

  # check data frame
  nms_actual <- colnames(data)
  nms_expected <- c("timestamp", "prediction", "max")
  if (!all(nms_expected %in% nms_actual)) {
    idx <- nms_expected %in% nms_actual

    stop(paste0("Required column ", nms_expected[!idx], " not found! "))
  }

  # aggregate do 1 Minute bins
  df1 <- aggregate(cbind(prediction) ~ cut(timestamp, breaks = paste0(avalue, " mins")), data, Mode)
  df2 <- aggregate(cbind(max) ~ cut(timestamp, breaks = paste0(avalue, " mins")), data, mean)
  df <- merge(df1, df2)
  
  colnames(df) <- c("timestamp", "prediction", "max")
  df$timestamp <- as.POSIXct(df$timestamp, tz = tzone)

  data.table::fwrite(df, paste0(animal$path$classification, animal$meta$animalID, "_aggregated_", avalue, "_Minute.csv"))
  
  if(get_data){return(df)}
}
Nature40/tRackIT documentation built on Nov. 21, 2023, 3:43 a.m.