long2start_stop {simDAG} | R Documentation |
Transform a data.table
in the long-format to a data.table
in the start-stop format
Description
This function transforms a data.table
in the long-format (one row per person per time point) to a data.table
in the start-stop format (one row per person-specific period in which no variables changed).
Usage
long2start_stop(data, id, time, varying, overlap=FALSE,
check_inputs=TRUE)
Arguments
data |
A |
id |
A single character string specifying a unique person identifier included in in |
time |
A single character string specifying a time variable included in in |
varying |
A character vector specifying names of variables included in in |
overlap |
Specifies whether the intervals should overlap or not. If |
check_inputs |
Whether to check if the user input is correct or not. Can be turned off by setting it to |
Details
This function relies on data.table
syntax to make the data transformation as RAM efficient and fast as possible.
Value
Returns a data.table
containing the columns .id
(the unique person identifier), .time
(an integer variable encoding the time) and all other variables included in the input data
in the long format.
Author(s)
Robin Denz
Examples
library(simDAG)
library(data.table)
# generate example data in long format
long <- data.table(.id=rep(seq_len(10), each=5),
.time=rep(seq_len(5), 10),
A=c(rep(FALSE, 43), TRUE, TRUE, rep(FALSE, 3), TRUE,
TRUE),
B=FALSE)
setkey(long, .id, .time)
# transform to start-stop format
long2start_stop(data=long, id=".id", time=".time", varying=c("A", "B"))