-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdistance_to_leader.Rd
109 lines (94 loc) · 3.44 KB
/
distance_to_leader.Rd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/distance_to_leader.R
\name{distance_to_leader}
\alias{distance_to_leader}
\title{Distance to group leader}
\usage{
distance_to_leader(DT = NULL, coords = NULL, group = "group")
}
\arguments{
\item{DT}{input data.table with 'rank_position_group_direction' column
generated by \code{leader_direction_group} and group column generated by
\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{group}{group column name, generated by \code{group_pts}, default
'group'}
}
\value{
\code{distance_to_leader} returns the input \code{DT} appended with
a \code{distance_leader} column indicating the distance to the group leader.
A message is returned when the \code{distance_leader} column is already exist in the input \code{DT}
because it will be overwritten.
}
\description{
\code{distance_to_leader} calculates the distance to the leader of each
spatiotemporal group. The function accepts 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 planar coordinates provided in two columns representing the X and Y
coordinates.
}
\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{coords} and \code{group} arguments
expect the names of columns in \code{DT} which correspond to the X and Y
coordinates and group columns.
}
\examples{
# Load data.table
library(data.table)
\dontshow{data.table::setDTthreads(1)}
# Read example data
DT <- fread(system.file("extdata", "DT.csv", package = "spatsoc"))
# Cast the character column to POSIXct
DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]
# (Subset example data to reduce example run time)
DT <- DT[year(datetime) == 2016]
# 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'),
projection = 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'),
return_rank = TRUE
)
# Calculate distance to leader
distance_to_leader(DT, coords = c('X', 'Y'))
}
\references{
See examples of using distance to leader and position within group:
\itemize{
\item \url{https://doi.org/10.1111/jfb.15315}
\item \url{https://doi.org/10.1098/rspb.2017.2629}
\item \url{https://doi.org/10.1016/j.anbehav.2023.09.009}
}
}
\seealso{
\link{direction_to_leader}, \link{leader_direction_group}, \link{group_pts}
Other Distance functions:
\code{\link{direction_to_centroid}()},
\code{\link{distance_to_centroid}()}
}
\concept{Distance functions}