% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/direction_to_leader.R
\name{direction_to_leader}
\alias{direction_to_leader}
\title{Direction to group leader}
\usage{
direction_to_leader(
  DT = NULL,
  coords = NULL,
  group = "group",
  crs = NULL,
  geometry = "geometry"
)
}
\arguments{
\item{DT}{input data.table}

\item{coords}{character vector of X coordinate and Y coordinate column names.
Note: the order is assumed X followed by Y column names}

\item{group}{group column name, generated by \code{group_pts}, default
'group'}

\item{crs}{numeric or character defining the coordinate reference
system to be passed to \link[sf:st_crs]{sf::st_crs}. For example, either
\code{crs = "EPSG:32736"} or \code{crs = 32736}. Used only if coords are provided,
see details under Interface}

\item{geometry}{simple feature geometry list column name, generated by
\code{\link[=get_geometry]{get_geometry()}}. Default 'geometry', see details under Interface}
}
\value{
\code{direction_to_leader} returns the input \code{DT} appended with a
\code{direction_leader} column indicating the direction to the group leader in
radians. A value of NaN is returned when the coordinates of the focal
individual equal the coordinates of the leader.

Missing values in coordinates / geometry are ignored and NA is returned.

A message is returned when the \code{direction_leader} column already exist in
the input \code{DT} because it will be overwritten.

See details for appending outputs using modify-by-reference in the
\href{https://docs.ropensci.org/spatsoc/articles/faq.html}{FAQ}.
}
\description{
\code{direction_to_leader} calculates the direction to the leader of each
spatiotemporal group. The function expects a \code{data.table} with relocation
data appended with a \code{rank_position_group_direction} column indicating the
ranked position along the group direction generated with
\code{leader_direction_group(return_rank = TRUE)}. Relocation data should be in
two columns representing the X and Y coordinates, or in a geometry column
prepared by the helper function \code{\link[=get_geometry]{get_geometry()}}.
}
\details{
The \code{DT} must be a \code{data.table}. If your data is a \code{data.frame}, you can
convert it by reference using \code{\link[data.table:setDT]{data.table::setDT()}} or by reassigning using
\code{\link[data.table:data.table]{data.table::data.table()}}.

This function expects a \code{rank_position_group_direction} column generated with
\code{leader_direction_group(return_rank = TRUE)}, a \code{group} column generated with
the \code{group_pts} function. The \code{group} argument expects the name of the column
in \code{DT} which correspond to the group column.

See below under "Interface" for details on providing coordinates and under
"Direction function" for details on the underlying direction function used.
}
\section{Interface}{

Two interfaces are available for providing coordinates:
\enumerate{
\item Provide \code{coords} and \code{crs}. The \code{coords} argument expects the names of
the X and Y coordinate columns. The \code{crs} argument expects a character
string or numeric defining the coordinate reference system to be passed to
\link[sf:st_crs]{sf::st_crs}. For example, for UTM zone 36S (EPSG 32736), the crs argument
is \code{crs = "EPSG:32736"} or \code{crs = 32736}. See \url{https://spatialreference.org}
for a list of EPSG codes.
\item (New!) Provide \code{geometry}. The \code{geometry} argument allows the user to
supply a \code{geometry} column that represents the coordinates as a simple
feature geometry list column. This interface expects the user to prepare
their input DT with \code{\link[=get_geometry]{get_geometry()}}. To use this interface, leave the
\code{coords} and \code{crs} arguments \code{NULL}, and the default argument for \code{geometry}
('geometry') will be used directly.
}
}

\section{Direction function}{


The underlying distance function used depends on the crs of the coordinates
or geometry provided.
\itemize{
\item If the crs is provided and longlat degrees (as determined by
\code{\link[sf:st_is_longlat]{sf::st_is_longlat()}}), the distance function is
\code{\link[lwgeom:st_geod_azimuth]{lwgeom::st_geod_azimuth()}}.
\item If the crs is provided and not longlat degrees (eg. a projected UTM),
the coordinates or geometry are transformed to \code{sf::st_crs(4326)} before the
distance is measured using \code{\link[lwgeom:st_geod_azimuth]{lwgeom::st_geod_azimuth()}}.
\item If the crs is NULL or NA_crs_, the distance function cannot be used
and an error is returned.
}
}

\examples{
# Load data.table
library(data.table)
\dontshow{data.table::setDTthreads(1)}
# Read example data
DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))
\dontshow{
# (Subset example data to reduce runtime on CRAN)
if (isFALSE(rlang::is_interactive())) {
  DT <- DT[as.Date(datetime) == '2017-01-17']
}
}
# Cast the character column to POSIXct
DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]

# Temporal grouping
group_times(DT, datetime = 'datetime', threshold = '20 minutes')

# Spatial grouping with timegroup
group_pts(DT, threshold = 50, id = 'ID',
          coords = c('X', 'Y'), timegroup = 'timegroup')

# Calculate direction at each step
direction_step(
  DT = DT,
  id = 'ID',
  coords = c('X', 'Y'),
  crs = 32736
)

# Calculate group centroid
centroid_group(DT, coords = c('X', 'Y'))

# Calculate group direction
direction_group(DT)

# Calculate leader in terms of position along group direction
leader_direction_group(
  DT,
  coords = c('X', 'Y'),
  crs = 32736
)

# Or, using the new geometry interface
get_geometry(DT, coords = c('X', 'Y'), crs = 32736)
group_pts(DT, threshold = 5, id = 'ID', timegroup = 'timegroup')
direction_step(
  DT = DT,
  id = 'ID'
)
centroid_group(DT)
direction_group(DT)
leader_direction_group(
  DT
)
direction_to_leader(DT)
}
\references{
See examples of using direction to leader and position within group:
\itemize{
\item \doi{doi:10.1016/j.anbehav.2023.09.009}
\item \doi{doi:10.1016/j.beproc.2013.10.007}
\item \doi{doi:10.1371/journal.pone.0036567}
}
}
\seealso{
\link{distance_to_leader}, \link{leader_direction_group}, \link{group_pts},
\code{\link[lwgeom:st_geod_azimuth]{lwgeom::st_geod_azimuth()}}

Other Direction functions: 
\code{\link{direction_group}()},
\code{\link{direction_polarization}()},
\code{\link{direction_step}()},
\code{\link{direction_to_centroid}()},
\code{\link{edge_alignment}()},
\code{\link{edge_delay}()},
\code{\link{edge_direction}()},
\code{\link{edge_zones}()},
\code{\link{leader_direction_group}()},
\code{\link{leader_edge_delay}()}

Other Leadership functions: 
\code{\link{leader_direction_group}()},
\code{\link{leader_edge_delay}()}
}
\concept{Direction functions}
\concept{Leadership functions}
