sc2interval {LightLogR} | R Documentation |
Statechange (sc) Timestamps to Intervals
Description
Takes an input of datetimes
and Statechanges
and creates a column with
Intervals
. If full = TRUE
, it will also create intervals for the day
prior to the first state change and after the last. If output.dataset = FALSE
it will give a named vector, otherwise a tibble
. The state change
info requires a description or name of the state (like "sleep"
or "wake"
,
or "wear"
) that goes into effect at the given Datetime
. Works for grouped
data so that it does not mix up intervals between participants. Missing data
should be explicit if at all possible. Also, the maximum allowed length of an
interval can be set, so that implicit missing timestamps after a set period
of times can be enforced.
Usage
sc2interval(
dataset,
Datetime.colname = Datetime,
Statechange.colname = State,
State.colname = State,
Interval.colname = Interval,
full = TRUE,
starting.state = NA,
output.dataset = TRUE,
Datetime.keep = FALSE,
length.restriction = 60 * 60 * 24
)
Arguments
dataset |
A light logger dataset. Expects a |
Datetime.colname |
column name that contains the datetime. Defaults to
|
Statechange.colname , Interval.colname , State.colname |
Column names that
do contain the name/description of the |
full , starting.state |
These arguments handle the state on the first day
before the first state change and after the last state change on the last
day. If |
output.dataset |
should the output be a |
Datetime.keep |
If |
length.restriction |
If the length between intervals is too great, the
interval state can be set to |
Value
One of
a
data.frame
object identical todataset
but with the interval instead of the datetime. The originalStatechange
column now indicates theState
during theInterval
.a named
vector
with the intervals, where the names are the states
Examples
library(tibble)
library(lubridate)
library(dplyr)
sample <- tibble::tibble(Datetime = c("2023-08-15 6:00:00",
"2023-08-15 23:00:00",
"2023-08-16 6:00:00",
"2023-08-16 22:00:00",
"2023-08-17 6:30:00",
"2023-08-18 1:00:00"),
State = rep(c("wake", "sleep"), 3),
Id = "Participant")
#intervals from sample
sc2interval(sample)
#compare sample (y) and intervals (x)
sc2interval(sample) %>%
mutate(Datetime = int_start(Interval)) %>%
dplyr::left_join(sample, by = c("Id", "State"),
relationship = "many-to-many") %>%
head()