Skip to contents

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_group and group column generated by group_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" or crs = 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:

  1. Provide coords and crs. The coords argument expects the names of the X and Y coordinate columns. The crs argument 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 is crs = "EPSG:32736" or crs = 32736. See https://spatialreference.org for a list of EPSG codes.

  2. (New!) Provide geometry. The geometry argument allows the user to supply a geometry column that represents the coordinates as a simple feature geometry list column. This interface expects the user to prepare their input DT with get_geometry(). To use this interface, leave the coords and crs arguments NULL, and the default argument for geometry ('geometry') will be used directly.

Distance function

The underlying distance function used depends on the crs of the coordinates or geometry provided.

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

References

See examples of using distance to leader and position within group:

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

# (Subset example data to reduce example run time)
DT <- DT[year(datetime) == 2016]

# 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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732

# 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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       group
#>       <int>
#>    1:     1
#>    2:     2
#>    3:     3
#>    4:     4
#>    5:     5
#>   ---      
#> 7288:   728
#> 7289:   729
#> 7290:  5531
#> 7291:   731
#> 7292:  5532

# 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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       group         direction
#>       <int>           <units>
#>    1:     1 -2.65649015 [rad]
#>    2:     2  2.17592086 [rad]
#>    3:     3 -1.98432277 [rad]
#>    4:     4  1.90650150 [rad]
#>    5:     5 -0.04059949 [rad]
#>   ---                        
#> 7288:   728 -2.99285448 [rad]
#> 7289:   729 -1.59174536 [rad]
#> 7290:  5531  2.36456688 [rad]
#> 7291:   731 -2.67757174 [rad]
#> 7292:  5532          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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       group         direction centroid_X centroid_Y
#>       <int>           <units>      <num>      <num>
#>    1:     1 -2.65649015 [rad]   715851.4    5505340
#>    2:     2  2.17592086 [rad]   715822.8    5505289
#>    3:     3 -1.98432277 [rad]   715872.9    5505252
#>    4:     4  1.90650150 [rad]   715820.5    5505231
#>    5:     5 -0.04059949 [rad]   715830.6    5505227
#>   ---                                              
#> 7288:   728 -2.99285448 [rad]   700191.6    5509089
#> 7289:   729 -1.59174536 [rad]   700156.0    5508800
#> 7290:  5531  2.36456688 [rad]   700028.7    5508826
#> 7291:   731 -2.67757174 [rad]   700254.6    5508589
#> 7292:  5532          NA [rad]   700110.1    5508383

# Calculate group direction
direction_group(DT)
#>           ID        X       Y            datetime population minutes timegroup
#>       <char>    <num>   <num>              <POSc>      <int>   <int>     <int>
#>    1:      A 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       group         direction centroid_X centroid_Y   group_direction
#>       <int>           <units>      <num>      <num>           <units>
#>    1:     1 -2.65649015 [rad]   715851.4    5505340 -2.65649015 [rad]
#>    2:     2  2.17592086 [rad]   715822.8    5505289  2.17592086 [rad]
#>    3:     3 -1.98432277 [rad]   715872.9    5505252 -1.98432277 [rad]
#>    4:     4  1.90650150 [rad]   715820.5    5505231  1.90650150 [rad]
#>    5:     5 -0.04059949 [rad]   715830.6    5505227 -0.04059949 [rad]
#>   ---                                                                
#> 7288:   728 -2.99285448 [rad]   700191.6    5509089 -3.05184005 [rad]
#> 7289:   729 -1.59174536 [rad]   700156.0    5508800 -2.41173804 [rad]
#> 7290:  5531  2.36456688 [rad]   700028.7    5508826  2.36456688 [rad]
#> 7291:   731 -2.67757174 [rad]   700254.6    5508589 -1.70798015 [rad]
#> 7292:  5532          NA [rad]   700110.1    5508383          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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       group         direction centroid_X centroid_Y   group_direction
#>       <int>           <units>      <num>      <num>           <units>
#>    1:     1 -2.65649015 [rad]   715851.4    5505340 -2.65649015 [rad]
#>    2:     2  2.17592086 [rad]   715822.8    5505289  2.17592086 [rad]
#>    3:     3 -1.98432277 [rad]   715872.9    5505252 -1.98432277 [rad]
#>    4:     4  1.90650150 [rad]   715820.5    5505231  1.90650150 [rad]
#>    5:     5 -0.04059949 [rad]   715830.6    5505227 -0.04059949 [rad]
#>   ---                                                                
#> 7288:   728 -2.99285448 [rad]   700191.6    5509089 -3.05184005 [rad]
#> 7289:   729 -1.59174536 [rad]   700156.0    5508800 -2.41173804 [rad]
#> 7290:  5531  2.36456688 [rad]   700028.7    5508826  2.36456688 [rad]
#> 7291:   731 -2.67757174 [rad]   700254.6    5508589 -1.70798015 [rad]
#> 7292:  5532          NA [rad]   700110.1    5508383          NA [rad]
#>       position_group_direction rank_position_group_direction
#>                          <num>                         <num>
#>    1:                  0.00000                             1
#>    2:                  0.00000                             1
#>    3:                  0.00000                             1
#>    4:                  0.00000                             1
#>    5:                  0.00000                             1
#>   ---                                                       
#> 7288:                -19.72453                             2
#> 7289:                -23.62960                             5
#> 7290:                  0.00000                             1
#> 7291:                -16.87561                             5
#> 7292:                       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): 
#> 732, 1464, 2824, 3424, 4029, 4649, 5532
#> Index: <group>
#>           ID        X       Y            datetime population minutes timegroup
#>       <char>    <num>   <num>              <POSc>      <int>   <int>     <int>
#>    1:      A 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       group         direction centroid_X centroid_Y   group_direction
#>       <int>           <units>      <num>      <num>           <units>
#>    1:     1 -2.65649015 [rad]   715851.4    5505340 -2.65649015 [rad]
#>    2:     2  2.17592086 [rad]   715822.8    5505289  2.17592086 [rad]
#>    3:     3 -1.98432277 [rad]   715872.9    5505252 -1.98432277 [rad]
#>    4:     4  1.90650150 [rad]   715820.5    5505231  1.90650150 [rad]
#>    5:     5 -0.04059949 [rad]   715830.6    5505227 -0.04059949 [rad]
#>   ---                                                                
#> 7288:   728 -2.99285448 [rad]   700191.6    5509089 -3.05184005 [rad]
#> 7289:   729 -1.59174536 [rad]   700156.0    5508800 -2.41173804 [rad]
#> 7290:  5531  2.36456688 [rad]   700028.7    5508826  2.36456688 [rad]
#> 7291:   731 -2.67757174 [rad]   700254.6    5508589 -1.70798015 [rad]
#> 7292:  5532          NA [rad]   700110.1    5508383          NA [rad]
#>       position_group_direction rank_position_group_direction distance_leader
#>                          <num>                         <num>           <num>
#>    1:                  0.00000                             1         0.00000
#>    2:                  0.00000                             1         0.00000
#>    3:                  0.00000                             1         0.00000
#>    4:                  0.00000                             1         0.00000
#>    5:                  0.00000                             1         0.00000
#>   ---                                                                       
#> 7288:                -19.72453                             2        40.21812
#> 7289:                -23.62960                             5        76.13264
#> 7290:                  0.00000                             1         0.00000
#> 7291:                -16.87561                             5        67.74920
#> 7292:                       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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       group         direction centroid_X centroid_Y   group_direction
#>       <int>           <units>      <num>      <num>           <units>
#>    1:     1 -2.65649015 [rad]   715851.4    5505340 -2.65649015 [rad]
#>    2:     2  2.17592086 [rad]   715822.8    5505289  2.17592086 [rad]
#>    3:     3 -1.98432277 [rad]   715872.9    5505252 -1.98432277 [rad]
#>    4:     4  1.90650150 [rad]   715820.5    5505231  1.90650150 [rad]
#>    5:     5 -0.04059949 [rad]   715830.6    5505227 -0.04059949 [rad]
#>   ---                                                                
#> 7288:   728 -2.99285448 [rad]   700191.6    5509089 -3.05184005 [rad]
#> 7289:   729 -1.59174536 [rad]   700156.0    5508800 -2.41173804 [rad]
#> 7290:  5531  2.36456688 [rad]   700028.7    5508826  2.36456688 [rad]
#> 7291:   731 -2.67757174 [rad]   700254.6    5508589 -1.70798015 [rad]
#> 7292:  5532          NA [rad]   700110.1    5508383          NA [rad]
#>       position_group_direction rank_position_group_direction distance_leader
#>                          <num>                         <num>           <num>
#>    1:                  0.00000                             1         0.00000
#>    2:                  0.00000                             1         0.00000
#>    3:                  0.00000                             1         0.00000
#>    4:                  0.00000                             1         0.00000
#>    5:                  0.00000                             1         0.00000
#>   ---                                                                       
#> 7288:                -19.72453                             2        40.21812
#> 7289:                -23.62960                             5        76.13264
#> 7290:                  0.00000                             1         0.00000
#> 7291:                -16.87561                             5        67.74920
#> 7292:                       NA                            NA              NA
#>                       geometry
#>                    <sfc_POINT>
#>    1: POINT (715851.4 5505340)
#>    2: POINT (715822.8 5505289)
#>    3: POINT (715872.9 5505252)
#>    4: POINT (715820.5 5505231)
#>    5: POINT (715830.6 5505227)
#>   ---                         
#> 7288: POINT (700211.6 5509087)
#> 7289: POINT (700165.3 5508825)
#> 7290: POINT (700028.7 5508826)
#> 7291: POINT (700230.9 5508609)
#> 7292: POINT (700110.1 5508383)
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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>               direction centroid_X centroid_Y   group_direction
#>                 <units>      <num>      <num>           <units>
#>    1: -2.65649015 [rad]   715851.4    5505340 -2.65649015 [rad]
#>    2:  2.17592086 [rad]   715822.8    5505289  2.17592086 [rad]
#>    3: -1.98432277 [rad]   715872.9    5505252 -1.98432277 [rad]
#>    4:  1.90650150 [rad]   715820.5    5505231  1.90650150 [rad]
#>    5: -0.04059949 [rad]   715830.6    5505227 -0.04059949 [rad]
#>   ---                                                          
#> 7288: -2.99285448 [rad]   700191.6    5509089 -3.05184005 [rad]
#> 7289: -1.59174536 [rad]   700156.0    5508800 -2.41173804 [rad]
#> 7290:  2.36456688 [rad]   700028.7    5508826  2.36456688 [rad]
#> 7291: -2.67757174 [rad]   700254.6    5508589 -1.70798015 [rad]
#> 7292:          NA [rad]   700110.1    5508383          NA [rad]
#>       position_group_direction rank_position_group_direction distance_leader
#>                          <num>                         <num>           <num>
#>    1:                  0.00000                             1         0.00000
#>    2:                  0.00000                             1         0.00000
#>    3:                  0.00000                             1         0.00000
#>    4:                  0.00000                             1         0.00000
#>    5:                  0.00000                             1         0.00000
#>   ---                                                                       
#> 7288:                -19.72453                             2        40.21812
#> 7289:                -23.62960                             5        76.13264
#> 7290:                  0.00000                             1         0.00000
#> 7291:                -16.87561                             5        67.74920
#> 7292:                       NA                            NA              NA
#>                       geometry group
#>                    <sfc_POINT> <int>
#>    1: POINT (715851.4 5505340)     1
#>    2: POINT (715822.8 5505289)     2
#>    3: POINT (715872.9 5505252)     3
#>    4: POINT (715820.5 5505231)     4
#>    5: POINT (715830.6 5505227)     5
#>   ---                               
#> 7288: POINT (700211.6 5509087)  7135
#> 7289: POINT (700165.3 5508825)  7136
#> 7290: POINT (700028.7 5508826)  7137
#> 7291: POINT (700230.9 5508609)  7138
#> 7292: POINT (700110.1 5508383)  7139
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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       centroid_X centroid_Y   group_direction position_group_direction
#>            <num>      <num>           <units>                    <num>
#>    1:   715851.4    5505340 -2.65649015 [rad]                  0.00000
#>    2:   715822.8    5505289  2.17592086 [rad]                  0.00000
#>    3:   715872.9    5505252 -1.98432277 [rad]                  0.00000
#>    4:   715820.5    5505231  1.90650150 [rad]                  0.00000
#>    5:   715830.6    5505227 -0.04059949 [rad]                  0.00000
#>   ---                                                                 
#> 7288:   700191.6    5509089 -3.05184005 [rad]                -19.72453
#> 7289:   700156.0    5508800 -2.41173804 [rad]                -23.62960
#> 7290:   700028.7    5508826  2.36456688 [rad]                  0.00000
#> 7291:   700254.6    5508589 -1.70798015 [rad]                -16.87561
#> 7292:   700110.1    5508383          NA [rad]                       NA
#>       rank_position_group_direction distance_leader                 geometry
#>                               <num>           <num>              <sfc_POINT>
#>    1:                             1         0.00000 POINT (715851.4 5505340)
#>    2:                             1         0.00000 POINT (715822.8 5505289)
#>    3:                             1         0.00000 POINT (715872.9 5505252)
#>    4:                             1         0.00000 POINT (715820.5 5505231)
#>    5:                             1         0.00000 POINT (715830.6 5505227)
#>   ---                                                                       
#> 7288:                             2        40.21812 POINT (700211.6 5509087)
#> 7289:                             5        76.13264 POINT (700165.3 5508825)
#> 7290:                             1         0.00000 POINT (700028.7 5508826)
#> 7291:                             5        67.74920 POINT (700230.9 5508609)
#> 7292:                            NA              NA POINT (700110.1 5508383)
#>       group         direction
#>       <int>           <units>
#>    1:     1 -2.65649015 [rad]
#>    2:     2  2.17592086 [rad]
#>    3:     3 -1.98432277 [rad]
#>    4:     4  1.90650150 [rad]
#>    5:     5 -0.04059949 [rad]
#>   ---                        
#> 7288:  7135 -2.99285448 [rad]
#> 7289:  7136 -1.59174536 [rad]
#> 7290:  7137  2.36456688 [rad]
#> 7291:  7138 -2.67757174 [rad]
#> 7292:  7139          NA [rad]
centroid_group(DT)
#>           ID        X       Y            datetime population minutes timegroup
#>       <char>    <num>   <num>              <POSc>      <int>   <int>     <int>
#>    1:      A 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       centroid_X centroid_Y   group_direction position_group_direction
#>            <num>      <num>           <units>                    <num>
#>    1:   715851.4    5505340 -2.65649015 [rad]                  0.00000
#>    2:   715822.8    5505289  2.17592086 [rad]                  0.00000
#>    3:   715872.9    5505252 -1.98432277 [rad]                  0.00000
#>    4:   715820.5    5505231  1.90650150 [rad]                  0.00000
#>    5:   715830.6    5505227 -0.04059949 [rad]                  0.00000
#>   ---                                                                 
#> 7288:   700191.6    5509089 -3.05184005 [rad]                -19.72453
#> 7289:   700156.0    5508800 -2.41173804 [rad]                -23.62960
#> 7290:   700028.7    5508826  2.36456688 [rad]                  0.00000
#> 7291:   700254.6    5508589 -1.70798015 [rad]                -16.87561
#> 7292:   700110.1    5508383          NA [rad]                       NA
#>       rank_position_group_direction distance_leader                 geometry
#>                               <num>           <num>              <sfc_POINT>
#>    1:                             1         0.00000 POINT (715851.4 5505340)
#>    2:                             1         0.00000 POINT (715822.8 5505289)
#>    3:                             1         0.00000 POINT (715872.9 5505252)
#>    4:                             1         0.00000 POINT (715820.5 5505231)
#>    5:                             1         0.00000 POINT (715830.6 5505227)
#>   ---                                                                       
#> 7288:                             2        40.21812 POINT (700211.6 5509087)
#> 7289:                             5        76.13264 POINT (700165.3 5508825)
#> 7290:                             1         0.00000 POINT (700028.7 5508826)
#> 7291:                             5        67.74920 POINT (700230.9 5508609)
#> 7292:                            NA              NA POINT (700110.1 5508383)
#>       group         direction                 centroid
#>       <int>           <units>              <sfc_POINT>
#>    1:     1 -2.65649015 [rad] POINT (715851.4 5505340)
#>    2:     2  2.17592086 [rad] POINT (715822.8 5505289)
#>    3:     3 -1.98432277 [rad] POINT (715872.9 5505252)
#>    4:     4  1.90650150 [rad] POINT (715820.5 5505231)
#>    5:     5 -0.04059949 [rad] POINT (715830.6 5505227)
#>   ---                                                 
#> 7288:  7135 -2.99285448 [rad] POINT (700211.6 5509087)
#> 7289:  7136 -1.59174536 [rad] POINT (700165.3 5508825)
#> 7290:  7137  2.36456688 [rad] POINT (700028.7 5508826)
#> 7291:  7138 -2.67757174 [rad] POINT (700230.9 5508609)
#> 7292:  7139          NA [rad] POINT (700110.1 5508383)
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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       centroid_X centroid_Y position_group_direction
#>            <num>      <num>                    <num>
#>    1:   715851.4    5505340                  0.00000
#>    2:   715822.8    5505289                  0.00000
#>    3:   715872.9    5505252                  0.00000
#>    4:   715820.5    5505231                  0.00000
#>    5:   715830.6    5505227                  0.00000
#>   ---                                               
#> 7288:   700191.6    5509089                -19.72453
#> 7289:   700156.0    5508800                -23.62960
#> 7290:   700028.7    5508826                  0.00000
#> 7291:   700254.6    5508589                -16.87561
#> 7292:   700110.1    5508383                       NA
#>       rank_position_group_direction distance_leader                 geometry
#>                               <num>           <num>              <sfc_POINT>
#>    1:                             1         0.00000 POINT (715851.4 5505340)
#>    2:                             1         0.00000 POINT (715822.8 5505289)
#>    3:                             1         0.00000 POINT (715872.9 5505252)
#>    4:                             1         0.00000 POINT (715820.5 5505231)
#>    5:                             1         0.00000 POINT (715830.6 5505227)
#>   ---                                                                       
#> 7288:                             2        40.21812 POINT (700211.6 5509087)
#> 7289:                             5        76.13264 POINT (700165.3 5508825)
#> 7290:                             1         0.00000 POINT (700028.7 5508826)
#> 7291:                             5        67.74920 POINT (700230.9 5508609)
#> 7292:                            NA              NA POINT (700110.1 5508383)
#>       group         direction                 centroid   group_direction
#>       <int>           <units>              <sfc_POINT>           <units>
#>    1:     1 -2.65649015 [rad] POINT (715851.4 5505340) -2.65649015 [rad]
#>    2:     2  2.17592086 [rad] POINT (715822.8 5505289)  2.17592086 [rad]
#>    3:     3 -1.98432277 [rad] POINT (715872.9 5505252) -1.98432277 [rad]
#>    4:     4  1.90650150 [rad] POINT (715820.5 5505231)  1.90650150 [rad]
#>    5:     5 -0.04059949 [rad] POINT (715830.6 5505227) -0.04059949 [rad]
#>   ---                                                                   
#> 7288:  7135 -2.99285448 [rad] POINT (700211.6 5509087) -2.99285448 [rad]
#> 7289:  7136 -1.59174536 [rad] POINT (700165.3 5508825) -1.59174536 [rad]
#> 7290:  7137  2.36456688 [rad] POINT (700028.7 5508826)  2.36456688 [rad]
#> 7291:  7138 -2.67757174 [rad] POINT (700230.9 5508609) -2.67757174 [rad]
#> 7292:  7139          NA [rad] POINT (700110.1 5508383)          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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       centroid_X centroid_Y distance_leader                 geometry group
#>            <num>      <num>           <num>              <sfc_POINT> <int>
#>    1:   715851.4    5505340         0.00000 POINT (715851.4 5505340)     1
#>    2:   715822.8    5505289         0.00000 POINT (715822.8 5505289)     2
#>    3:   715872.9    5505252         0.00000 POINT (715872.9 5505252)     3
#>    4:   715820.5    5505231         0.00000 POINT (715820.5 5505231)     4
#>    5:   715830.6    5505227         0.00000 POINT (715830.6 5505227)     5
#>   ---                                                                     
#> 7288:   700191.6    5509089        40.21812 POINT (700211.6 5509087)  7135
#> 7289:   700156.0    5508800        76.13264 POINT (700165.3 5508825)  7136
#> 7290:   700028.7    5508826         0.00000 POINT (700028.7 5508826)  7137
#> 7291:   700254.6    5508589        67.74920 POINT (700230.9 5508609)  7138
#> 7292:   700110.1    5508383              NA POINT (700110.1 5508383)  7139
#>               direction                 centroid   group_direction
#>                 <units>              <sfc_POINT>           <units>
#>    1: -2.65649015 [rad] POINT (715851.4 5505340) -2.65649015 [rad]
#>    2:  2.17592086 [rad] POINT (715822.8 5505289)  2.17592086 [rad]
#>    3: -1.98432277 [rad] POINT (715872.9 5505252) -1.98432277 [rad]
#>    4:  1.90650150 [rad] POINT (715820.5 5505231)  1.90650150 [rad]
#>    5: -0.04059949 [rad] POINT (715830.6 5505227) -0.04059949 [rad]
#>   ---                                                             
#> 7288: -2.99285448 [rad] POINT (700211.6 5509087) -2.99285448 [rad]
#> 7289: -1.59174536 [rad] POINT (700165.3 5508825) -1.59174536 [rad]
#> 7290:  2.36456688 [rad] POINT (700028.7 5508826)  2.36456688 [rad]
#> 7291: -2.67757174 [rad] POINT (700230.9 5508609) -2.67757174 [rad]
#> 7292:          NA [rad] POINT (700110.1 5508383)          NA [rad]
#>       position_group_direction rank_position_group_direction
#>                          <num>                         <num>
#>    1:                        0                             1
#>    2:                        0                             1
#>    3:                        0                             1
#>    4:                        0                             1
#>    5:                        0                             1
#>   ---                                                       
#> 7288:                        0                             1
#> 7289:                        0                             1
#> 7290:                        0                             1
#> 7291:                        0                             1
#> 7292:                       NA                            NA
distance_to_leader(DT)
#> Warning: groups found missing leader (rank_position_group_direction == 1): 
#> 732, 1464, 2184, 2914, 3637, 4359, 5034, 5728, 6417, 7139
#> 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 715851.4 5505340 2016-11-01 00:00:54          1       0         1
#>    2:      A 715822.8 5505289 2016-11-01 02:01:22          1       0         2
#>    3:      A 715872.9 5505252 2016-11-01 04:01:24          1       0         3
#>    4:      A 715820.5 5505231 2016-11-01 06:01:05          1       0         4
#>    5:      A 715830.6 5505227 2016-11-01 08:01:11          1       0         5
#>   ---                                                                         
#> 7288:      J 700211.6 5509087 2016-12-31 14:00:49          1       0       728
#> 7289:      J 700165.3 5508825 2016-12-31 16:00:23          1       0       729
#> 7290:      J 700028.7 5508826 2016-12-31 18:00:53          1       0       730
#> 7291:      J 700230.9 5508609 2016-12-31 20:00:53          1       0       731
#> 7292:      J 700110.1 5508383 2016-12-31 22:00:54          1       0       732
#>       centroid_X centroid_Y                 geometry group         direction
#>            <num>      <num>              <sfc_POINT> <int>           <units>
#>    1:   715851.4    5505340 POINT (715851.4 5505340)     1 -2.65649015 [rad]
#>    2:   715822.8    5505289 POINT (715822.8 5505289)     2  2.17592086 [rad]
#>    3:   715872.9    5505252 POINT (715872.9 5505252)     3 -1.98432277 [rad]
#>    4:   715820.5    5505231 POINT (715820.5 5505231)     4  1.90650150 [rad]
#>    5:   715830.6    5505227 POINT (715830.6 5505227)     5 -0.04059949 [rad]
#>   ---                                                                       
#> 7288:   700191.6    5509089 POINT (700211.6 5509087)  7135 -2.99285448 [rad]
#> 7289:   700156.0    5508800 POINT (700165.3 5508825)  7136 -1.59174536 [rad]
#> 7290:   700028.7    5508826 POINT (700028.7 5508826)  7137  2.36456688 [rad]
#> 7291:   700254.6    5508589 POINT (700230.9 5508609)  7138 -2.67757174 [rad]
#> 7292:   700110.1    5508383 POINT (700110.1 5508383)  7139          NA [rad]
#>                       centroid   group_direction position_group_direction
#>                    <sfc_POINT>           <units>                    <num>
#>    1: POINT (715851.4 5505340) -2.65649015 [rad]                        0
#>    2: POINT (715822.8 5505289)  2.17592086 [rad]                        0
#>    3: POINT (715872.9 5505252) -1.98432277 [rad]                        0
#>    4: POINT (715820.5 5505231)  1.90650150 [rad]                        0
#>    5: POINT (715830.6 5505227) -0.04059949 [rad]                        0
#>   ---                                                                    
#> 7288: POINT (700211.6 5509087) -2.99285448 [rad]                        0
#> 7289: POINT (700165.3 5508825) -1.59174536 [rad]                        0
#> 7290: POINT (700028.7 5508826)  2.36456688 [rad]                        0
#> 7291: POINT (700230.9 5508609) -2.67757174 [rad]                        0
#> 7292: POINT (700110.1 5508383)          NA [rad]                       NA
#>       rank_position_group_direction distance_leader
#>                               <num>           <num>
#>    1:                             1               0
#>    2:                             1               0
#>    3:                             1               0
#>    4:                             1               0
#>    5:                             1               0
#>   ---                                              
#> 7288:                             1               0
#> 7289:                             1               0
#> 7290:                             1               0
#> 7291:                             1               0
#> 7292:                            NA              NA