group_times {spatsoc} | R Documentation |
Group Times
Description
group_times
groups rows into time groups. The function accepts date
time formatted data and a threshold argument. The threshold argument is used
to specify a time window within which rows are grouped.
Usage
group_times(DT = NULL, datetime = NULL, threshold = NULL)
Arguments
DT |
input data.table |
datetime |
name of date time column(s). either 1 POSIXct or 2 IDate and ITime. e.g.: 'datetime' or c('idate', 'itime') |
threshold |
threshold for grouping times. e.g.: '2 hours', '10 minutes', etc. if not provided, times will be matched exactly. Note that provided threshold must be in the expected format: '## unit' |
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 datetime
argument expects the name of a column in DT
which
is of type POSIXct
or the name of two columns in DT
which are
of type IDate
and ITime
.
threshold
must be provided in units of minutes, hours or days. The
character string should start with an integer followed by a unit, separated
by a space. It is interpreted in terms of 24 hours which poses the following
limitations:
minutes, hours and days cannot be fractional
minutes must divide evenly into 60
minutes must not exceed 60
minutes, hours which are nearer to the next day, are grouped as such
hours must divide evenly into 24
multi-day blocks should divide into the range of days, else the blocks may not be the same length
In addition, the threshold
is considered a fixed window throughout the
time series and the rows are grouped to the nearest interval.
If threshold
is NULL, rows are grouped using the datetime
column directly.
Value
group_times
returns the input DT
appended with a
timegroup
column and additional temporal grouping columns to help
investigate, troubleshoot and interpret the timegroup.
The actual value of timegroup
is arbitrary and represents the
identity of a given timegroup
which 1 or more individuals are
assigned to. If the data was reordered, the group may change, but the
contents of each group would not.
The temporal grouping columns added depend on the threshold
provided:
-
threshold
with unit minutes: "minutes" column added identifying the nearest minute group for each row. -
threshold
with unit hours: "hours" column added identifying the nearest hour group for each row. -
threshold
with unit days: "block" columns added identifying the multiday block for each row.
A message is returned when any of these columns already exist in the input
DT
, because they will be overwritten.
See Also
group_pts
group_lines
group_polys
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')]
group_times(DT, datetime = 'datetime', threshold = '5 minutes')
group_times(DT, datetime = 'datetime', threshold = '2 hours')
group_times(DT, datetime = 'datetime', threshold = '10 days')