View source: R/long2start_stop.r
long2start_stop | R Documentation |
data.table
in the long-format to a data.table
in the start-stop format
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).
long2start_stop(data, id, time, varying, overlap=FALSE,
check_inputs=TRUE)
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 |
This function relies on data.table
syntax to make the data transformation as RAM efficient and fast as possible.
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.
Robin Denz
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"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.