R - Extract summary table from Survfit with Strata

JimmyR picture JimmyR · Jul 3, 2015 · Viewed 7.7k times · Source

I'm new to R and survival analysis, and I am interested to export into a dataframe the results from survfit where there is strata.

This site has provided an excellent solution but not to one with strata (https://stat.ethz.ch/pipermail/r-help/2014-October/422348.html). How can i append (or stack) each strata with an extra column which contains the strata type. solution in the link offered is not applicable to strata groupings

library(survival)
data(lung)
mod <- with(lung, survfit(Surv(time, status)~ 1))
res <- summary(mod)
str(res)

# Extract the columns you want
cols <- lapply(c(2:6, 8:10) , function(x) res[x])
# Combine the columns into a data frame
tbl <- do.call(data.frame, cols)
str(tbl)

Thank you in advanced, R newbie

Answer

Rorschach picture Rorschach · Jul 3, 2015

It is basically the same as you have there, just an extra column

res <- summary( survfit( Surv(futime, fustat)~rx, data=ovarian))
cols <- lapply(c(2:6, 8:11) , function(x) res[x])
tbl <- do.call(data.frame, cols)
head(tbl)
#   time n.risk n.event n.censor      surv strata   std.err     upper     lower
# 1   59     13       1        0 0.9230769   rx=1 0.0739053 1.0000000 0.7890186
# 2  115     12       1        0 0.8461538   rx=1 0.1000683 1.0000000 0.6710952
# 3  156     11       1        0 0.7692308   rx=1 0.1168545 1.0000000 0.5711496
# 4  268     10       1        0 0.6923077   rx=1 0.1280077 0.9946869 0.4818501
# 5  329      9       1        0 0.6153846   rx=1 0.1349320 0.9457687 0.4004132
# 6  431      8       1        0 0.5384615   rx=1 0.1382642 0.8906828 0.3255265