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.
name of date time column(s). either 1 POSIXct or 2 IDate and ITime. e.g.: 'datetime' or c('idate', 'itime')
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'
DT must be a
data.table. If your data is a
data.frame, you can convert it by reference using
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
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.
threshold is NULL, rows are grouped using the
datetime column directly.
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 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# 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')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.