'Convert "survfit()" output to a matrix or dataframe

Example data below.

My basic problem is that running "survfit" by itself gives a nice column with median lifespan for each category, which is the thing I want to extract from my survfit data. Ideally I'd like to export this "survfit" output as a dataframe/table and ultimately save to .csv. But I get errors however I try.

Thanks for help/advice!

Example data:

df<-data.frame(Gtype = as.factor(c("A","A","A","A","A","A","B","B","B","B","B","B","C","C","C","C","C","C")),
Time=as.numeric(c("5","6","7","7","7","7","2","3","3","4","5","7","2","2","2","3","3","4")),
Status=as.numeric(c("1","1","1","1","0","0","1","1","1","1","1","1","1","1","1","1","1","1")))

library(survival)
exsurv<-survfit(Surv(df$Time,df$Status)~strata(df$Gtype))
exsurv

and the "survfit" output I want to get as a dataframe:

> exsurv<-survfit(Surv(df$Time,df$Status)~strata(df$Gtype))
> exsurv
Call: survfit(formula = Surv(df$Time, df$Status) ~ strata(df$Gtype))

                   n events median 0.95LCL 0.95UCL
strata(df$Gtype)=A 6      4    7.0       6      NA
strata(df$Gtype)=B 6      6    3.5       3      NA
strata(df$Gtype)=C 6      6    2.5       2      NA

edit: An earlier version of this question included the print() function superfluously. "print(survfit)" and "survfit()" give the same result.



Solution 1:[1]

Yes broom::tidy function works.

'mymk1' - is the object output of using survfit on my raw survival data set

I tried this and it worked well

results <- broom::tidy(mykm1)

write.csv(results, "./Desktop/Rout/mykm1.csv") 
## the output csv file created in my folder Rout inside my Desktop folder.

The csv file can then be imported easily into any word or spreadsheet.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Gal