% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/centroid_group.R
\name{centroid_group}
\alias{centroid_group}
\title{Group centroid}
\usage{
centroid_group(
  DT = NULL,
  coords = NULL,
  crs = NULL,
  group = "group",
  geometry = "geometry"
)
}
\arguments{
\item{DT}{input data.table with group column generated with \code{group_pts}}

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

\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{group}{Character string of group column}

\item{geometry}{simple feature geometry list column name, generated by
\code{\link[=get_geometry]{get_geometry()}}. Default 'geometry', see details under Interface}
}
\value{
\code{centroid_group} returns the input \code{DT} appended with
centroid column(s) for each group.

If the \code{crs} for \code{coords} or \code{st_crs(geometry)} for \code{geometry} is long lat
(see \code{\link[sf:st_is_longlat]{sf::st_is_longlat()}}), centroids will be calculated using
\code{\link[s2:s2_boundary]{s2::s2_centroid()}} through \code{\link[sf:geos_unary]{sf::st_centroid()}}. If the \code{crs} for \code{coords}
or \code{st_crs(geometry)} for \code{geometry} is projected or NA, the centroids will
be calculated using a mean on the coordinates.

If \code{coords} are provided, the centroid columns will be named by prefixing
the coordinate column names with "centroid_" (eg. "X" = "centroid_X"). If
\code{geometry} is used, the centroid column will be named "centroid".

A message is returned when the centroid column(s) already exist in the
input because they 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{centroid_group} calculates the centroid of all individuals in each
spatiotemporal group identified by \code{group_pts}. The function expects a
\code{data.table} with relocation data appended with a \code{group} column from
\code{group_pts}. 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()}}.

The \code{group} argument expects the name of a column in
\code{DT} which correspond to the group column.

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

Two interfaces are available for providing coordinates:
\enumerate{
\item Provide \code{coords} and optionally \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. For centroid
calculations, if \code{crs} is NULL, it will be internally set to \code{NA_crs_}.
\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{Centroid function}{


The underlying centroid function used depends on the crs of the coordinates
or geometry provided.
\itemize{
\item If the crs is longlat degrees (as determined by
\code{\link[sf:st_is_longlat]{sf::st_is_longlat()}}) and \code{\link[sf:s2]{sf::sf_use_s2()}} is TRUE, the distance function
is \code{\link[sf:geos_unary]{sf::st_centroid()}} which passes to \code{\link[s2:s2_boundary]{s2::s2_centroid()}}.
\item If the crs is longlat degrees but \code{\link[sf:s2]{sf::sf_use_s2()}} is FALSE, the centroid
calculated will be incorrect. See \code{\link[sf:geos_unary]{sf::st_centroid()}}.
\item If the crs is not longlat degrees (eg. NULL, NA_crs_, or projected), the
centroid function used is mean.
}

Note: if the input is length 1, the input 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 = 5, id = 'ID',
          coords = c('X', 'Y'), timegroup = 'timegroup')

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

# Or, using the new geometry interface
get_geometry(DT, coords = c('X', 'Y'), crs = 32736)
group_pts(DT, threshold = 5, id = 'ID', timegroup = 'timegroup')
centroid_group(DT)
}
\seealso{
\code{group_pts}

Other Centroid functions: 
\code{\link{centroid_dyad}()},
\code{\link{centroid_fusion}()},
\code{\link{direction_to_centroid}()},
\code{\link{distance_to_centroid}()}
}
\concept{Centroid functions}
