R/conTemporal.R

Defines functions conTemporal

Documented in conTemporal

# ---- roxygen documentation ----
#
#' @title conTemporal
#'
#' @description
#' Create a summary dataframe of the timing and and duration of contact phases.
#'
#' @details
#' This function is used to calculate the start and end times of contact phases, and their duration following use of the \code{conPhase} function.

#' @param traj an object of the class \code{ltraj} which is output from the function \code{conPhase}.
#' @param units units of duration e.g., \code{'mins'} (see \code{difftime}). 
#'
#' @return
#' A data frame, with the time and duration attributes associated with contact phases.
#'
#' @references
#'  Long, JA, Webb, SL, Harju, SM, Gee, KL (2022) Analyzing Contacts and Behavior from High Frequency 
#'  Tracking Data Using the wildlifeDI R Package. \emph{Geographical Analysis}. \bold{54}, 648--663.
#'
#' @keywords contacts
#' @seealso conPhase
#' 
#' @examples 
#' \dontrun{
#' data(does)
#' doecons <- conProcess(does,tc=15*60,dc=50)
#' doephas <- conPhase(doecons,pc=60*60)
#' conTemporal(doephas)
#' }
#' 
#' @export
#
# ---- End of roxygen documentation ----

conTemporal <- function(traj,units='auto'){
  dfr <- ld(traj)
  dfr <- dfr[dfr$contacts>0,]
  phas <- unique(dfr$contact_pha)
  outdf <- data.frame(contact_pha = phas, start_time = dfr$date[1], end_time = dfr$date[1])

  for (i in 1:length(phas)){
    temp <- dfr[dfr$contact_pha==phas[i],]
    outdf$start_time[i] <- as.POSIXct(min(temp$date,na.rm=TRUE)) 
    outdf$end_time[i] <- as.POSIXct(max(temp$date,na.rm=TRUE)) 
    
  }
  outdf$duration <- difftime(outdf$end_time,outdf$start_time,units=units)
  return(outdf)
}

Try the wildlifeDI package in your browser

Any scripts or data that you put into this service are public.

wildlifeDI documentation built on Nov. 14, 2023, 1:09 a.m.