
Create strata from several variables.
constStrata(
data,
strata,
sep = ".",
lex.order = FALSE,
trace = TRUE,
as.numeric = FALSE
)
A factor vector or a numeric vector.
[data.frame] dataset.
[character vector] A vector of the variables capturing the stratification factors.
[character] string to construct the new level labels by joining the constituent ones.
[logical] Should the order of factor concatenation be lexically ordered ?
[logical] Should the execution of the function be traced ?
[logical] Should the strata be converted from factors to numeric?
Brice Ozenne
This function uses the interaction
function from the base package to form the strata.
library(data.table)
library(survival) ## import veteran
# strata with two variables : celltype and karno
veteran$strata1 <- constStrata(veteran,c("celltype","karno"))
table(veteran$strata1)
# strata with three variables : celltype, karno and age dichotomized at 60 years
veteran$age60 <- veteran$age>60
veteran$age60 <- factor(veteran$age60,labels=c("<=60",">60")) # convert to factor with labels
veteran$strata2 <- constStrata(veteran,c("celltype","karno","age60"))
table(veteran$strata2) # factor strata variable
veteran$strata2 <- constStrata(veteran,c("celltype","karno","age60"), as.numeric=TRUE)
table(veteran$strata2) # numeric strata variable
Run the code above in your browser using DataLab