% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/group_pts.R
\name{group_pts}
\alias{group_pts}
\title{Group points}
\usage{
group_pts(
  DT = NULL,
  threshold = NULL,
  id = NULL,
  coords = NULL,
  timegroup,
  crs = NULL,
  splitBy = NULL,
  geometry = "geometry"
)
}
\arguments{
\item{DT}{input data.table}

\item{threshold}{distance for grouping points, either numeric or units, in
the units of the crs / the coordinates or geometry}

\item{id}{character string of ID column name}

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

\item{timegroup}{timegroup field in the DT within which the output will be
calculated}

\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{splitBy}{(optional) character string or vector of grouping column
name(s) upon which the output will be calculated}

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

This column represents the spatialtemporal group. As with the other
grouping functions,  the actual value of \code{group} is arbitrary and
represents the identity of a given group where 1 or more individuals are
assigned to a group. If the data was reordered, the \code{group} may
change, but the contents of each group would not.

A message is returned when a column named \code{group} already exists 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{group_pts} groups rows into spatial groups. The function expects a
\code{data.table} with relocation data, individual identifiers and a
threshold argument. The threshold argument is used to specify the criteria
for distance between points which defines a group. 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{id}, \code{timegroup}  (and optional \code{splitBy})
arguments expect the names of columns in \code{DT} which correspond to the
individual identifier, and timegroup (generated by
\code{group_times}) and additional grouping columns.

The \code{threshold} provided should match the units of the coordinates. The
\code{threshold} can be provided with units specified using the units package (eg.
\code{threshold = units::set_units(10, m)}) which will be checked against the
units of the coordinates using the \code{crs}. If units are not specified, the
\code{threshold} is assumed to be in the units of the coordinates.

The \code{timegroup} argument is required to define the temporal groups
within which spatial groups are calculated. The intended framework is to
group rows temporally with \code{group_times} then spatially with
\code{group_pts} (or \code{group_lines}, \code{group_polys}).
If you have already calculated temporal groups without
\code{group_times}, you can pass this column to the \code{timegroup}
argument. Note that the expectation is that each individual will be observed
only once per timegroup. Caution that accidentally including huge numbers of
rows within timegroups can overload your machine since all pairwise distances
are calculated within each timegroup.

The \code{splitBy} argument offers further control over grouping. If within
your \code{DT}, you have multiple populations, subgroups or other distinct
parts, you can provide the name of the column which identifies them to
\code{splitBy}. The grouping performed by \code{group_pts} will only consider
rows within each \code{splitBy} subgroup.

See below under "Interface" for details on providing coordinates and under
"Distance function" for details on underlying distance 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{Distance function}{


The underlying distance 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()}}), the distance function is \code{\link[sf:geos_measures]{sf::st_distance()}} which
passes to \code{\link[s2:s2_is_collection]{s2::s2_distance()}} if \code{\link[sf:s2]{sf::sf_use_s2()}} is TRUE and
\code{\link[lwgeom:geod]{lwgeom::st_geod_distance()}} if \code{\link[sf:s2]{sf::sf_use_s2()}} is FALSE. The distance
returned has units set according to the crs.
\item If the crs is not longlat degrees (eg. NULL, NA_crs_, or projected), the
distance function used is \code{\link[stats:dist]{stats::dist()}}, maintaining expected behaviour
from previous versions. The distance returned does not have units set.
}

Note: in both cases, if the coordinates are NA then the result will be NA.
}

\examples{
# Load data.table
library(data.table)
\dontshow{data.table::setDTthreads(1)}

# Read example data
DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))

# Select only individuals A, B, C for this example
DT <- DT[ID \%in\% c('A', 'B', 'C')]

# 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')

# Spatial grouping with timegroup and splitBy on population
group_pts(DT, threshold = 5, id = 'ID', coords = c('X', 'Y'),
         timegroup = 'timegroup', splitBy = 'population')

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

Other Spatial grouping: 
\code{\link{group_lines}()},
\code{\link{group_polys}()}
}
\concept{Spatial grouping}
