lexisFromTo: lexisFromTo

View source: R/lexisFromTo.R

lexisFromToR Documentation

lexisFromTo

Description

lexixFromTo is a specialised version of lexis splitting. In handling drug treatment, employment and similar factors the individual may move in and out of conditions. This function splits according such a situation. The input is first "base data" id/in/out/event (and other variables) that may already have been split by other functions. The other data is a sequence of id/from/to/value/name - that may represent multiple conditions with from/to and with "name" to distinguish.

Usage

lexisFromTo(indat,splitdat,invars,splitvars,default="0",datacheck=TRUE)

Arguments

indat

- base data with id, start, end, event and other data - possibly already split

splitdat

- Data with splitting guide - id/from/to/value/name

invars

- vector of colum names for id/entry/exit/event - in that order, example: c("id","start","end","event")

splitvars

- vector of column names containing dates to split by. example: c("id","start","end","value","name") - must be in that order!

default

- Value given to intervels not given a value by the function.

datacheck

- This function can crash or produce incorrect results if input data have overlapping intervals or negative intervals in any of the two input datasets. Thic is checked and error produced by datacheck. Can be omitted if data are checked otherwise

Details

The input to this function are two data.tables and two lists of the critical variables. The BASE data it the data to be split. This data must have a variable to identify participants, start/end times and a variable to indicate event after last interval. The other table (SPLITTINGUIDE) contains possibly multiple records for each participants with id/from/to/value/name. The program checks that intervals are not negative and that intervals for each "name" and each individual do not overlap. Violation stops the program with error. Overlaps may occur in real situations, but the user needs to make decisions regarding this prior to this function.

It is required that the splittingguide contains at least one record. Missing data for key variables are not allowed and will cause errors.

Value

The function returns a new data table where records have been split according to the splittingguide dataset. Variables unrelated to the splitting are left unchanged.

Author(s)

Christian Torp-Pedersen

Examples

library(data.table)
dat <- data.table(id=c("A","A","B","B","C","D"),
                 start=as.Date(c(0,100,0,100,200,400),origin='1970-01-01'),
                 end=as.Date(c(100,200,100,200,300,500),origin='1970-01-01'),
                 event=c(0,1,0,0,1,1))
split <- data.table (id=c("A","A","A","A","B","B","B","D","D"),
                    start=as.Date(c(0,50,25,150,110,150,400,300,500),origin='1970-01-01'),
                    end= as.Date(c(25,75,150,151,120,250,500,400,500),origin='1970-01-01'),
                    value=c(1,2,3,4,1,2,3,6,7),
                    name=c("d1","d1","d2","d2","d1","d1","d2","d3","d4"))
#Show the dataset:
dat[]
split[]                   
temp <- lexisFromTo(dat # inddato with id/in/out/event
                   ,split # Data with id and dates
                   ,c("id","start","end","event") #names of id/in/out/event - in that order
                  ,c("id","start","end","value","name")) #Nmes var date-vars to split by

tagteam/heaven documentation built on May 28, 2024, 9:22 p.m.