widen: Function to transform data from person-time format to person...

Description Usage Arguments Details Value Examples

View source: R/RFunctions_1_0_2.r

Description

Function to transform data from person-time format to person format suitable for lengthen()

Usage

1
2
3
widen(input, id, time, exposure, covariate, history = NULL,
  weight.exposure = NULL, weight.censor = NULL, strata = NULL,
  censor = NULL)

Arguments

input

dataframe in long format e.g., a person-time format

id

unique identifier at the unit (person) level

time

unique index for each observation within each unit

exposure

the exposure of interest at time t

covariate

a vector of covariates at time t

history

variable describing exposure history through time t

weight.exposure

inverse probability weight for exposure, at or through time t

weight.censor

cumulative inverse probability weight for censoring through time t

strata

propensity score strata at time t

censor

censoring indicators at time t

Details

Numeric formats are preserved, factors are coerced into character.

Value

A "wide" dataframe where each row uniquely indexes a single subject’s data, so that columns index measurement of each variable at each time. The indices should be indicated with an underscore suffix followed by the time, e.g. variable_1,variable_2.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Simulate long data set for two subjects
id <- as.numeric(c(1, 1, 1, 2, 2, 2))
time <- as.numeric(c(0, 1, 2, 0, 1, 2))
a <- as.numeric(c(0, 1, 1, 1, 1, 0))
l <- as.numeric(rbinom(6, 1, 0.5))
m <- as.numeric(rbinom(6, 1, 0.5))
n <- as.numeric(rbinom(6, 1, 0.5))

mydata.long <- data.frame(id, time, a, l, m, n)

# Run the widen() function
mydata.wide <- widen(input=mydata.long,
                     id="id", time="time",
                     exposure="a",
                     covariate=c("l","m","n")
                     )

confoundr documentation built on Sept. 20, 2019, 9:03 a.m.