group_lines: Groups Lines

Description Usage Arguments Details Value See Also Examples

View source: R/group_lines.R

Description

group_lines groups rows into spatial groups by creating trajectories and grouping based on spatial overlap. The function accepts a data.table with relocation data, individual identifiers and a threshold. The relocation data is transformed into SpatialLines and overlapping SpatialLines are grouped. The threshold argument is used to specify the criteria for distance between lines. Relocation data should be in two columns representing the X and Y coordinates.

Usage

1
2
3
group_lines(DT = NULL, threshold = NULL, projection = NULL,
  id = NULL, coords = NULL, timegroup = NULL, sortBy = NULL,
  splitBy = NULL, spLines = NULL)

Arguments

DT

input data.table

threshold

The width of the buffer around the lines in the units of the projection. Supply 0 to compare intersection without buffering.

projection

PROJ.4 character string

id

Character string of ID column name

coords

Character vector of X coordinate and Y coordinate column names

timegroup

(optional) timegroup field in the DT upon which the grouping will be calculated

sortBy

Character string of date time column(s) to sort rows by. Must be a POSIXct.

splitBy

(optional) character string or vector of grouping column name(s) upon which the grouping will be calculated

spLines

Alternatively to providing a DT, provide a SpatialLines object created with the sp package. If a spLines object is provided, groups cannot be calculated by a timegroup or splitBy.

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.

The id, coords, sortBy (and optional timegroup and splitBy) arguments expect the names of respective columns in DT which correspond to the individual identifier, X and Y coordinates, sorting, timegroup (generated by group_times) and additional grouping columns.

The sortBy is used to order the input data.table when creating SpatialLines. It must a POSIXct to ensure the rows are sorted by date time. The projection expects a PROJ.4 character string (such as those available on spatialreference.org). group_lines expects planar coordinates (not unprojected latitude, longitude).

The threshold must be provided in the units of the coordinates. The threshold can be equal to 0 if strict overlap is required, else it needs to be greater than 0. The coordinates must be planar coordinates (e.g.: UTM). In the case of UTM, a threshold = 50 would indicate a 50m distance threshold.

The timegroup argument is optional, but recommended to pair with group_times. The intended framework is to group rows temporally with group_times then spatially with group_lines (or group_pts, group_polys). With group_lines, pick a relevant group_times threshold such as '1 day' or '7 days' which is informed by your study species and system.

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

Value

group_lines returns the input DT appended with a group column.

This column represents the spatial (and if timegroup was provided - spatiotemporal) group calculated by overlapping lines. As with the other grouping functions, the actual value of 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 group may change, but the contents of each group would not.

A message is returned when a column named group already exists in the input DT, because it will be overwritten.

See Also

build_lines group_times

Other Spatial grouping: group_polys, group_pts

Examples

 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
# Load data.table
library(data.table)

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

# Subset only individuals A, B, and C
DT <- DT[ID %in% c('A', 'B', 'C')]

# Cast the character column to POSIXct
DT[, datetime := as.POSIXct(datetime, tz = 'UTC')]

# Proj4 string for example data
utm <- '+proj=utm +zone=36 +south +ellps=WGS84 +datum=WGS84 +units=m +no_defs'

group_lines(DT, threshold = 50, projection = utm, sortBy = 'datetime',
            id = 'ID', coords = c('X', 'Y'))

## Daily movement tracks
# Temporal grouping
group_times(DT, datetime = 'datetime', threshold = '1 day')

# Subset only first 50 days
DT <- DT[timegroup < 25]

# Spatial grouping
group_lines(DT, threshold = 50, projection = utm,
            id = 'ID', coords = c('X', 'Y'),
            timegroup = 'timegroup', sortBy = 'datetime')

## Daily movement tracks by population
group_lines(DT, threshold = 50, projection = utm,
            id = 'ID', coords = c('X', 'Y'),
            timegroup = 'timegroup', sortBy = 'datetime',
            splitBy = 'population')

ropensci/spatsoc documentation built on Oct. 27, 2018, 12:15 a.m.