Reputation: 307
Let say I have these three vectors:
time <- c(306,455,1010,210,883,1022,310,361,218,166)
status <- c(0,1,0,1,0,0,1,0,1,1)
gender <- c("Male","Male","Female","Male","Male","Male","Female","Female","Female","Female")
and I want to do a Survival Analysis and get the summary.
A <- survfit(Surv(time, status)~gender)
summary(A, censored = TRUE)
The output would be like this:
> summary(A, censored = TRUE)
Call: survfit(formula = Surv(time, status) ~ gender)
gender=Female
time n.risk n.event survival std.err lower 95% CI upper 95% CI
166 5 1 0.8 0.179 0.516 1
218 4 1 0.6 0.219 0.293 1
310 3 1 0.4 0.219 0.137 1
361 2 0 0.4 0.219 0.137 1
1010 1 0 0.4 0.219 0.137 1
gender=Male
time n.risk n.event survival std.err lower 95% CI upper 95% CI
210 5 1 0.800 0.179 0.516 1
306 4 0 0.800 0.179 0.516 1
455 3 1 0.533 0.248 0.214 1
883 2 0 0.533 0.248 0.214 1
1022 1 0 0.533 0.248 0.214 1
My question is, is there any way that I can split the output into Male and Female. For example:
output_Female <- ?????
output_Female
output_Female
time n.risk n.event survival std.err lower 95% CI upper 95% CI
166 5 1 0.8 0.179 0.516 1
218 4 1 0.6 0.219 0.293 1
310 3 1 0.4 0.219 0.137 1
361 2 0 0.4 0.219 0.137 1
1010 1 0 0.4 0.219 0.137 1
output_Male <- ?????
output_Male
output_Male
time n.risk n.event survival std.err lower 95% CI upper 95% CI
166 5 1 0.8 0.179 0.516 1
218 4 1 0.6 0.219 0.293 1
310 3 1 0.4 0.219 0.137 1
361 2 0 0.4 0.219 0.137 1
1010 1 0 0.4 0.219 0.137 1
Upvotes: 1
Views: 233
Reputation: 887158
Here is an option using tidy
library(broom)
library(dplyr)
tidy(A, censored = TRUE) %>%
split(.$strata)
Or with base R
txt <- capture.output(summary(A, censored = TRUE))
ind <- cumsum(grepl("gender=", txt))
lst <- lapply(split(txt[ind >0], ind[ind >0]), function(x)
read.table(text = x[-(1:2)], header = FALSE))
nm1 <- scan(text= gsub("\\s+[0-9]|%\\s+", ".", txt[4]), quiet = TRUE, what = "")
lst <- lapply(lst, setNames, nm1)
Upvotes: 2