distance_to_leader calculates the distance to the leader of each
spatiotemporal group. The function expects a data.table with relocation
data appended with a rank_position_group_direction column indicating the
ranked position along the group direction generated with
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 get_geometry().
Usage
distance_to_leader(
DT = NULL,
coords = NULL,
group = "group",
crs = NULL,
geometry = "geometry"
)Arguments
- DT
input data.table with 'rank_position_group_direction' column generated by
leader_direction_groupand group column generated bygroup_pts- coords
character vector of X coordinate and Y coordinate column names. Note: the order is assumed X followed by Y column names
- group
group column name, generated by
group_pts, default 'group'- crs
numeric or character defining the coordinate reference system to be passed to sf::st_crs. For example, either
crs = "EPSG:32736"orcrs = 32736. Used only if coords are provided, see details under Interface- geometry
simple feature geometry list column name, generated by
get_geometry(). Default 'geometry', see details under Interface
Value
distance_to_leader returns the input DT appended with a
distance_leader column indicating the distance to the group leader.
A message is returned when the distance_leader column already exist in
the input DT because it will be overwritten.
See details for appending outputs using modify-by-reference in the FAQ.
Details
The DT must be a data.table. If your data is a data.frame, you can
convert it by reference using data.table::setDT() or by reassigning using
data.table::data.table().
This function expects a rank_position_group_direction column generated with
leader_direction_group(return_rank = TRUE), a group column generated with
the group_pts function. The group argument expect the names of the column
in DT which corresponds to the group column.
See below under "Interface" for details on providing coordinates and under "Distance function" for details on underlying distance function used.
Interface
Two interfaces are available for providing coordinates:
Provide
coordsandcrs. Thecoordsargument expects the names of the X and Y coordinate columns. Thecrsargument expects a character string or numeric defining the coordinate reference system to be passed to sf::st_crs. For example, for UTM zone 36S (EPSG 32736), the crs argument iscrs = "EPSG:32736"orcrs = 32736. See https://spatialreference.org for a list of EPSG codes.(New!) Provide
geometry. Thegeometryargument allows the user to supply ageometrycolumn that represents the coordinates as a simple feature geometry list column. This interface expects the user to prepare their input DT withget_geometry(). To use this interface, leave thecoordsandcrsargumentsNULL, and the default argument forgeometry('geometry') will be used directly.
Distance function
The underlying distance function used depends on the crs of the coordinates or geometry provided.
If the crs is longlat degrees (as determined by
sf::st_is_longlat()), the distance function issf::st_distance()which passes tos2::s2_distance()ifsf::sf_use_s2()is TRUE andlwgeom::st_geod_distance()ifsf::sf_use_s2()is FALSE. The distance returned has units set according to the crs.If the crs is not longlat degrees (eg. NULL, NA_crs_, or projected), the distance function used is
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.
See also
direction_to_leader, leader_direction_group, group_pts,
sf::st_distance()
Other Distance functions:
distance_to_centroid(),
edge_dist(),
edge_nn(),
edge_zones()
Examples
# Load data.table
library(data.table)
# 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')]
#> ID X Y datetime population
#> <char> <num> <num> <POSc> <int>
#> 1: A 715851.4 5505340 2016-11-01 00:00:54 1
#> 2: A 715822.8 5505289 2016-11-01 02:01:22 1
#> 3: A 715872.9 5505252 2016-11-01 04:01:24 1
#> 4: A 715820.5 5505231 2016-11-01 06:01:05 1
#> 5: A 715830.6 5505227 2016-11-01 08:01:11 1
#> ---
#> 14293: J 700616.5 5509069 2017-02-28 14:00:54 1
#> 14294: J 700622.6 5509065 2017-02-28 16:00:11 1
#> 14295: J 700657.5 5509277 2017-02-28 18:00:55 1
#> 14296: J 700610.3 5509269 2017-02-28 20:00:48 1
#> 14297: J 700744.0 5508782 2017-02-28 22:00:39 1
# Temporal grouping
group_times(DT, datetime = 'datetime', threshold = '20 minutes')
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
# Spatial grouping with timegroup
group_pts(DT, threshold = 50, id = 'ID',
coords = c('X', 'Y'), timegroup = 'timegroup')
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> group
#> <int>
#> 1: 1
#> 2: 2
#> 3: 3
#> 4: 4
#> 5: 5
#> ---
#> 116: 8
#> 117: 9
#> 118: 10
#> 119: 11
#> 120: 40
# Calculate direction at each step
direction_step(
DT = DT,
id = 'ID',
coords = c('X', 'Y'),
crs = 32736
)
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> group direction
#> <int> <units>
#> 1: 1 1.4903118 [rad]
#> 2: 2 1.7942728 [rad]
#> 3: 3 0.9220273 [rad]
#> 4: 4 -1.7726867 [rad]
#> 5: 5 -2.3533764 [rad]
#> ---
#> 116: 8 2.6583955 [rad]
#> 117: 9 -1.4177958 [rad]
#> 118: 10 0.5336646 [rad]
#> 119: 11 -1.0819257 [rad]
#> 120: 40 NA [rad]
# Calculate group centroid
centroid_group(DT, coords = c('X', 'Y'))
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> group direction centroid_X centroid_Y
#> <int> <units> <num> <num>
#> 1: 1 1.4903118 [rad] 696214.3 5508342
#> 2: 2 1.7942728 [rad] 696228.1 5508344
#> 3: 3 0.9220273 [rad] 696760.4 5508245
#> 4: 4 -1.7726867 [rad] 696965.7 5508380
#> 5: 5 -2.3533764 [rad] 696074.7 5508214
#> ---
#> 116: 8 2.6583955 [rad] 696952.5 5508004
#> 117: 9 -1.4177958 [rad] 697014.8 5507923
#> 118: 10 0.5336646 [rad] 697015.8 5507916
#> 119: 11 -1.0819257 [rad] 697285.0 5508368
#> 120: 40 NA [rad] 696620.8 5508731
# Calculate group direction
direction_group(DT)
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> group direction centroid_X centroid_Y group_direction
#> <int> <units> <num> <num> <units>
#> 1: 1 1.4903118 [rad] 696214.3 5508342 0.5997289 [rad]
#> 2: 2 1.7942728 [rad] 696228.1 5508344 1.7285069 [rad]
#> 3: 3 0.9220273 [rad] 696760.4 5508245 0.9679820 [rad]
#> 4: 4 -1.7726867 [rad] 696965.7 5508380 -1.7070067 [rad]
#> 5: 5 -2.3533764 [rad] 696074.7 5508214 -0.8348445 [rad]
#> ---
#> 116: 8 2.6583955 [rad] 696952.5 5508004 2.4662821 [rad]
#> 117: 9 -1.4177958 [rad] 697014.8 5507923 -2.9981952 [rad]
#> 118: 10 0.5336646 [rad] 697015.8 5507916 0.5103765 [rad]
#> 119: 11 -1.0819257 [rad] 697285.0 5508368 -1.1444566 [rad]
#> 120: 40 NA [rad] 696620.8 5508731 NA [rad]
# Calculate leader in terms of position along group direction
leader_direction_group(
DT,
coords = c('X', 'Y'),
crs = 32736,
return_rank = TRUE
)
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> group direction centroid_X centroid_Y group_direction
#> <int> <units> <num> <num> <units>
#> 1: 1 1.4903118 [rad] 696214.3 5508342 0.5997289 [rad]
#> 2: 2 1.7942728 [rad] 696228.1 5508344 1.7285069 [rad]
#> 3: 3 0.9220273 [rad] 696760.4 5508245 0.9679820 [rad]
#> 4: 4 -1.7726867 [rad] 696965.7 5508380 -1.7070067 [rad]
#> 5: 5 -2.3533764 [rad] 696074.7 5508214 -0.8348445 [rad]
#> ---
#> 116: 8 2.6583955 [rad] 696952.5 5508004 2.4662821 [rad]
#> 117: 9 -1.4177958 [rad] 697014.8 5507923 -2.9981952 [rad]
#> 118: 10 0.5336646 [rad] 697015.8 5507916 0.5103765 [rad]
#> 119: 11 -1.0819257 [rad] 697285.0 5508368 -1.1444566 [rad]
#> 120: 40 NA [rad] 696620.8 5508731 NA [rad]
#> position_group_direction rank_position_group_direction
#> <num> <num>
#> 1: -7.7690645 5
#> 2: 22.0744878 1
#> 3: -24.7063051 5
#> 4: 8.9025813 3
#> 5: -0.2458479 2
#> ---
#> 116: -21.9798928 6
#> 117: -31.1746472 6
#> 118: 22.8303115 1
#> 119: 26.1390093 1
#> 120: NA NA
# Calculate distance to leader
distance_to_leader(DT, coords = c('X', 'Y'), crs = 32736)
#> Warning: groups found missing leader (rank_position_group_direction == 1):
#> 12, 24, 37, 40, 52
#> Index: <group>
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> group direction centroid_X centroid_Y group_direction
#> <int> <units> <num> <num> <units>
#> 1: 1 1.4903118 [rad] 696214.3 5508342 0.5997289 [rad]
#> 2: 2 1.7942728 [rad] 696228.1 5508344 1.7285069 [rad]
#> 3: 3 0.9220273 [rad] 696760.4 5508245 0.9679820 [rad]
#> 4: 4 -1.7726867 [rad] 696965.7 5508380 -1.7070067 [rad]
#> 5: 5 -2.3533764 [rad] 696074.7 5508214 -0.8348445 [rad]
#> ---
#> 116: 8 2.6583955 [rad] 696952.5 5508004 2.4662821 [rad]
#> 117: 9 -1.4177958 [rad] 697014.8 5507923 -2.9981952 [rad]
#> 118: 10 0.5336646 [rad] 697015.8 5507916 0.5103765 [rad]
#> 119: 11 -1.0819257 [rad] 697285.0 5508368 -1.1444566 [rad]
#> 120: 40 NA [rad] 696620.8 5508731 NA [rad]
#> position_group_direction rank_position_group_direction distance_leader
#> <num> <num> <num>
#> 1: -7.7690645 5 73.474248
#> 2: 22.0744878 1 0.000000
#> 3: -24.7063051 5 53.106427
#> 4: 8.9025813 3 49.172349
#> 5: -0.2458479 2 8.148133
#> ---
#> 116: -21.9798928 6 51.968262
#> 117: -31.1746472 6 69.264245
#> 118: 22.8303115 1 0.000000
#> 119: 26.1390093 1 0.000000
#> 120: NA NA NA
# Or, using the new geometry interface
get_geometry(DT, coords = c('X', 'Y'), crs = 32736)
#> Index: <group>
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> group direction centroid_X centroid_Y group_direction
#> <int> <units> <num> <num> <units>
#> 1: 1 1.4903118 [rad] 696214.3 5508342 0.5997289 [rad]
#> 2: 2 1.7942728 [rad] 696228.1 5508344 1.7285069 [rad]
#> 3: 3 0.9220273 [rad] 696760.4 5508245 0.9679820 [rad]
#> 4: 4 -1.7726867 [rad] 696965.7 5508380 -1.7070067 [rad]
#> 5: 5 -2.3533764 [rad] 696074.7 5508214 -0.8348445 [rad]
#> ---
#> 116: 8 2.6583955 [rad] 696952.5 5508004 2.4662821 [rad]
#> 117: 9 -1.4177958 [rad] 697014.8 5507923 -2.9981952 [rad]
#> 118: 10 0.5336646 [rad] 697015.8 5507916 0.5103765 [rad]
#> 119: 11 -1.0819257 [rad] 697285.0 5508368 -1.1444566 [rad]
#> 120: 40 NA [rad] 696620.8 5508731 NA [rad]
#> position_group_direction rank_position_group_direction distance_leader
#> <num> <num> <num>
#> 1: -7.7690645 5 73.474248
#> 2: 22.0744878 1 0.000000
#> 3: -24.7063051 5 53.106427
#> 4: 8.9025813 3 49.172349
#> 5: -0.2458479 2 8.148133
#> ---
#> 116: -21.9798928 6 51.968262
#> 117: -31.1746472 6 69.264245
#> 118: 22.8303115 1 0.000000
#> 119: 26.1390093 1 0.000000
#> 120: NA NA NA
#> geometry
#> <sfc_POINT>
#> 1: POINT (696191.5 5508362)
#> 2: POINT (696205.2 5508363)
#> 3: POINT (696745.8 5508225)
#> 4: POINT (696952 5508373)
#> 5: POINT (696079 5508218)
#> ---
#> 116: POINT (696996.5 5508024)
#> 117: POINT (697046.4 5507922)
#> 118: POINT (697037.5 5507924)
#> 119: POINT (697303 5508347)
#> 120: POINT (696616.7 5508736)
group_pts(DT, threshold = 5, id = 'ID', timegroup = 'timegroup')
#> group column will be overwritten by this function
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> direction centroid_X centroid_Y group_direction
#> <units> <num> <num> <units>
#> 1: 1.4903118 [rad] 696214.3 5508342 0.5997289 [rad]
#> 2: 1.7942728 [rad] 696228.1 5508344 1.7285069 [rad]
#> 3: 0.9220273 [rad] 696760.4 5508245 0.9679820 [rad]
#> 4: -1.7726867 [rad] 696965.7 5508380 -1.7070067 [rad]
#> 5: -2.3533764 [rad] 696074.7 5508214 -0.8348445 [rad]
#> ---
#> 116: 2.6583955 [rad] 696952.5 5508004 2.4662821 [rad]
#> 117: -1.4177958 [rad] 697014.8 5507923 -2.9981952 [rad]
#> 118: 0.5336646 [rad] 697015.8 5507916 0.5103765 [rad]
#> 119: -1.0819257 [rad] 697285.0 5508368 -1.1444566 [rad]
#> 120: NA [rad] 696620.8 5508731 NA [rad]
#> position_group_direction rank_position_group_direction distance_leader
#> <num> <num> <num>
#> 1: -7.7690645 5 73.474248
#> 2: 22.0744878 1 0.000000
#> 3: -24.7063051 5 53.106427
#> 4: 8.9025813 3 49.172349
#> 5: -0.2458479 2 8.148133
#> ---
#> 116: -21.9798928 6 51.968262
#> 117: -31.1746472 6 69.264245
#> 118: 22.8303115 1 0.000000
#> 119: 26.1390093 1 0.000000
#> 120: NA NA NA
#> geometry group
#> <sfc_POINT> <int>
#> 1: POINT (696191.5 5508362) 1
#> 2: POINT (696205.2 5508363) 2
#> 3: POINT (696745.8 5508225) 3
#> 4: POINT (696952 5508373) 4
#> 5: POINT (696079 5508218) 5
#> ---
#> 116: POINT (696996.5 5508024) 109
#> 117: POINT (697046.4 5507922) 110
#> 118: POINT (697037.5 5507924) 111
#> 119: POINT (697303 5508347) 112
#> 120: POINT (696616.7 5508736) 59
direction_step(
DT = DT,
id = 'ID'
)
#> direction column will be overwritten by this function
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> centroid_X centroid_Y group_direction position_group_direction
#> <num> <num> <units> <num>
#> 1: 696214.3 5508342 0.5997289 [rad] -7.7690645
#> 2: 696228.1 5508344 1.7285069 [rad] 22.0744878
#> 3: 696760.4 5508245 0.9679820 [rad] -24.7063051
#> 4: 696965.7 5508380 -1.7070067 [rad] 8.9025813
#> 5: 696074.7 5508214 -0.8348445 [rad] -0.2458479
#> ---
#> 116: 696952.5 5508004 2.4662821 [rad] -21.9798928
#> 117: 697014.8 5507923 -2.9981952 [rad] -31.1746472
#> 118: 697015.8 5507916 0.5103765 [rad] 22.8303115
#> 119: 697285.0 5508368 -1.1444566 [rad] 26.1390093
#> 120: 696620.8 5508731 NA [rad] NA
#> rank_position_group_direction distance_leader geometry
#> <num> <num> <sfc_POINT>
#> 1: 5 73.474248 POINT (696191.5 5508362)
#> 2: 1 0.000000 POINT (696205.2 5508363)
#> 3: 5 53.106427 POINT (696745.8 5508225)
#> 4: 3 49.172349 POINT (696952 5508373)
#> 5: 2 8.148133 POINT (696079 5508218)
#> ---
#> 116: 6 51.968262 POINT (696996.5 5508024)
#> 117: 6 69.264245 POINT (697046.4 5507922)
#> 118: 1 0.000000 POINT (697037.5 5507924)
#> 119: 1 0.000000 POINT (697303 5508347)
#> 120: NA NA POINT (696616.7 5508736)
#> group direction
#> <int> <units>
#> 1: 1 1.4903118 [rad]
#> 2: 2 1.7942728 [rad]
#> 3: 3 0.9220273 [rad]
#> 4: 4 -1.7726867 [rad]
#> 5: 5 -2.3533764 [rad]
#> ---
#> 116: 109 2.6583955 [rad]
#> 117: 110 -1.4177958 [rad]
#> 118: 111 0.5336646 [rad]
#> 119: 112 -1.0819257 [rad]
#> 120: 59 NA [rad]
centroid_group(DT)
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> centroid_X centroid_Y group_direction position_group_direction
#> <num> <num> <units> <num>
#> 1: 696214.3 5508342 0.5997289 [rad] -7.7690645
#> 2: 696228.1 5508344 1.7285069 [rad] 22.0744878
#> 3: 696760.4 5508245 0.9679820 [rad] -24.7063051
#> 4: 696965.7 5508380 -1.7070067 [rad] 8.9025813
#> 5: 696074.7 5508214 -0.8348445 [rad] -0.2458479
#> ---
#> 116: 696952.5 5508004 2.4662821 [rad] -21.9798928
#> 117: 697014.8 5507923 -2.9981952 [rad] -31.1746472
#> 118: 697015.8 5507916 0.5103765 [rad] 22.8303115
#> 119: 697285.0 5508368 -1.1444566 [rad] 26.1390093
#> 120: 696620.8 5508731 NA [rad] NA
#> rank_position_group_direction distance_leader geometry
#> <num> <num> <sfc_POINT>
#> 1: 5 73.474248 POINT (696191.5 5508362)
#> 2: 1 0.000000 POINT (696205.2 5508363)
#> 3: 5 53.106427 POINT (696745.8 5508225)
#> 4: 3 49.172349 POINT (696952 5508373)
#> 5: 2 8.148133 POINT (696079 5508218)
#> ---
#> 116: 6 51.968262 POINT (696996.5 5508024)
#> 117: 6 69.264245 POINT (697046.4 5507922)
#> 118: 1 0.000000 POINT (697037.5 5507924)
#> 119: 1 0.000000 POINT (697303 5508347)
#> 120: NA NA POINT (696616.7 5508736)
#> group direction centroid
#> <int> <units> <sfc_POINT>
#> 1: 1 1.4903118 [rad] POINT (696191.5 5508362)
#> 2: 2 1.7942728 [rad] POINT (696205.2 5508363)
#> 3: 3 0.9220273 [rad] POINT (696745.8 5508225)
#> 4: 4 -1.7726867 [rad] POINT (696952 5508373)
#> 5: 5 -2.3533764 [rad] POINT (696074.7 5508214)
#> ---
#> 116: 109 2.6583955 [rad] POINT (696996.5 5508024)
#> 117: 110 -1.4177958 [rad] POINT (697046.4 5507922)
#> 118: 111 0.5336646 [rad] POINT (697037.5 5507924)
#> 119: 112 -1.0819257 [rad] POINT (697303 5508347)
#> 120: 59 NA [rad] POINT (696617.8 5508734)
direction_group(DT)
#> group_direction column will be overwritten by this function
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> centroid_X centroid_Y position_group_direction
#> <num> <num> <num>
#> 1: 696214.3 5508342 -7.7690645
#> 2: 696228.1 5508344 22.0744878
#> 3: 696760.4 5508245 -24.7063051
#> 4: 696965.7 5508380 8.9025813
#> 5: 696074.7 5508214 -0.2458479
#> ---
#> 116: 696952.5 5508004 -21.9798928
#> 117: 697014.8 5507923 -31.1746472
#> 118: 697015.8 5507916 22.8303115
#> 119: 697285.0 5508368 26.1390093
#> 120: 696620.8 5508731 NA
#> rank_position_group_direction distance_leader geometry
#> <num> <num> <sfc_POINT>
#> 1: 5 73.474248 POINT (696191.5 5508362)
#> 2: 1 0.000000 POINT (696205.2 5508363)
#> 3: 5 53.106427 POINT (696745.8 5508225)
#> 4: 3 49.172349 POINT (696952 5508373)
#> 5: 2 8.148133 POINT (696079 5508218)
#> ---
#> 116: 6 51.968262 POINT (696996.5 5508024)
#> 117: 6 69.264245 POINT (697046.4 5507922)
#> 118: 1 0.000000 POINT (697037.5 5507924)
#> 119: 1 0.000000 POINT (697303 5508347)
#> 120: NA NA POINT (696616.7 5508736)
#> group direction centroid group_direction
#> <int> <units> <sfc_POINT> <units>
#> 1: 1 1.4903118 [rad] POINT (696191.5 5508362) 1.4903118 [rad]
#> 2: 2 1.7942728 [rad] POINT (696205.2 5508363) 1.7942728 [rad]
#> 3: 3 0.9220273 [rad] POINT (696745.8 5508225) 0.9220273 [rad]
#> 4: 4 -1.7726867 [rad] POINT (696952 5508373) -1.7726867 [rad]
#> 5: 5 -2.3533764 [rad] POINT (696074.7 5508214) -0.8348445 [rad]
#> ---
#> 116: 109 2.6583955 [rad] POINT (696996.5 5508024) 2.6583955 [rad]
#> 117: 110 -1.4177958 [rad] POINT (697046.4 5507922) -1.4177958 [rad]
#> 118: 111 0.5336646 [rad] POINT (697037.5 5507924) 0.5336646 [rad]
#> 119: 112 -1.0819257 [rad] POINT (697303 5508347) -1.0819257 [rad]
#> 120: 59 NA [rad] POINT (696617.8 5508734) NA [rad]
leader_direction_group(
DT
)
#> position_group_direction column will be overwritten by this function
#> rank_position_group_direction column will be overwritten by this function
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> centroid_X centroid_Y distance_leader geometry group
#> <num> <num> <num> <sfc_POINT> <int>
#> 1: 696214.3 5508342 73.474248 POINT (696191.5 5508362) 1
#> 2: 696228.1 5508344 0.000000 POINT (696205.2 5508363) 2
#> 3: 696760.4 5508245 53.106427 POINT (696745.8 5508225) 3
#> 4: 696965.7 5508380 49.172349 POINT (696952 5508373) 4
#> 5: 696074.7 5508214 8.148133 POINT (696079 5508218) 5
#> ---
#> 116: 696952.5 5508004 51.968262 POINT (696996.5 5508024) 109
#> 117: 697014.8 5507923 69.264245 POINT (697046.4 5507922) 110
#> 118: 697015.8 5507916 0.000000 POINT (697037.5 5507924) 111
#> 119: 697285.0 5508368 0.000000 POINT (697303 5508347) 112
#> 120: 696620.8 5508731 NA POINT (696616.7 5508736) 59
#> direction centroid group_direction
#> <units> <sfc_POINT> <units>
#> 1: 1.4903118 [rad] POINT (696191.5 5508362) 1.4903118 [rad]
#> 2: 1.7942728 [rad] POINT (696205.2 5508363) 1.7942728 [rad]
#> 3: 0.9220273 [rad] POINT (696745.8 5508225) 0.9220273 [rad]
#> 4: -1.7726867 [rad] POINT (696952 5508373) -1.7726867 [rad]
#> 5: -2.3533764 [rad] POINT (696074.7 5508214) -0.8348445 [rad]
#> ---
#> 116: 2.6583955 [rad] POINT (696996.5 5508024) 2.6583955 [rad]
#> 117: -1.4177958 [rad] POINT (697046.4 5507922) -1.4177958 [rad]
#> 118: 0.5336646 [rad] POINT (697037.5 5507924) 0.5336646 [rad]
#> 119: -1.0819257 [rad] POINT (697303 5508347) -1.0819257 [rad]
#> 120: NA [rad] POINT (696617.8 5508734) NA [rad]
#> position_group_direction rank_position_group_direction
#> <num> <num>
#> 1: 0.0000000 1
#> 2: 0.0000000 1
#> 3: 0.0000000 1
#> 4: 0.0000000 1
#> 5: -0.2458479 2
#> ---
#> 116: 0.0000000 1
#> 117: 0.0000000 1
#> 118: 0.0000000 1
#> 119: 0.0000000 1
#> 120: NA NA
distance_to_leader(DT)
#> Warning: groups found missing leader (rank_position_group_direction == 1):
#> 12, 24, 35, 47, 59, 70, 80, 92, 103
#> distance_leader column will be overwritten by this function
#> Index: <group>
#> ID X Y datetime population minutes timegroup
#> <char> <num> <num> <POSc> <int> <int> <int>
#> 1: A 696191.5 5508362 2017-01-17 00:00:47 1 0 1
#> 2: A 696205.2 5508363 2017-01-17 02:00:48 1 0 2
#> 3: A 696745.8 5508225 2017-01-17 04:00:48 1 0 3
#> 4: A 696952.0 5508373 2017-01-17 06:00:54 1 0 4
#> 5: A 696079.0 5508218 2017-01-17 08:00:54 1 0 5
#> ---
#> 116: J 696996.5 5508024 2017-01-17 14:00:42 1 0 8
#> 117: J 697046.4 5507922 2017-01-17 16:00:47 1 0 9
#> 118: J 697037.5 5507924 2017-01-17 18:00:54 1 0 10
#> 119: J 697303.0 5508347 2017-01-17 20:00:24 1 0 11
#> 120: J 696616.7 5508736 2017-01-17 22:00:42 1 0 12
#> centroid_X centroid_Y geometry group direction
#> <num> <num> <sfc_POINT> <int> <units>
#> 1: 696214.3 5508342 POINT (696191.5 5508362) 1 1.4903118 [rad]
#> 2: 696228.1 5508344 POINT (696205.2 5508363) 2 1.7942728 [rad]
#> 3: 696760.4 5508245 POINT (696745.8 5508225) 3 0.9220273 [rad]
#> 4: 696965.7 5508380 POINT (696952 5508373) 4 -1.7726867 [rad]
#> 5: 696074.7 5508214 POINT (696079 5508218) 5 -2.3533764 [rad]
#> ---
#> 116: 696952.5 5508004 POINT (696996.5 5508024) 109 2.6583955 [rad]
#> 117: 697014.8 5507923 POINT (697046.4 5507922) 110 -1.4177958 [rad]
#> 118: 697015.8 5507916 POINT (697037.5 5507924) 111 0.5336646 [rad]
#> 119: 697285.0 5508368 POINT (697303 5508347) 112 -1.0819257 [rad]
#> 120: 696620.8 5508731 POINT (696616.7 5508736) 59 NA [rad]
#> centroid group_direction position_group_direction
#> <sfc_POINT> <units> <num>
#> 1: POINT (696191.5 5508362) 1.4903118 [rad] 0.0000000
#> 2: POINT (696205.2 5508363) 1.7942728 [rad] 0.0000000
#> 3: POINT (696745.8 5508225) 0.9220273 [rad] 0.0000000
#> 4: POINT (696952 5508373) -1.7726867 [rad] 0.0000000
#> 5: POINT (696074.7 5508214) -0.8348445 [rad] -0.2458479
#> ---
#> 116: POINT (696996.5 5508024) 2.6583955 [rad] 0.0000000
#> 117: POINT (697046.4 5507922) -1.4177958 [rad] 0.0000000
#> 118: POINT (697037.5 5507924) 0.5336646 [rad] 0.0000000
#> 119: POINT (697303 5508347) -1.0819257 [rad] 0.0000000
#> 120: POINT (696617.8 5508734) NA [rad] NA
#> rank_position_group_direction distance_leader
#> <num> <num>
#> 1: 1 0.000000
#> 2: 1 0.000000
#> 3: 1 0.000000
#> 4: 1 0.000000
#> 5: 2 8.148133
#> ---
#> 116: 1 0.000000
#> 117: 1 0.000000
#> 118: 1 0.000000
#> 119: 1 0.000000
#> 120: NA NA
