1

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.

Crawdaunt
  • 89
  • 1
  • 12
  • Look at the structure `str(exsurv)` of the object. Surely you will find the data that is printed. (not tested) – markus Jan 24 '22 at 12:20
  • 1
    You could use the `broom` package, for example: `results <- broom::tidy(exsurv)`, that will give you a data frame, with the parameters in columns. – jmcastagnetto Jan 24 '22 at 13:25
  • Was curious specifically in the median lifespan statistic, as this is calculated stratified by Gtype by default in the print() function, but not with summary(exsurv), str(exsurv), broom::tidy(exsurv). I just realized print() is superfluous to this too. Just running survfit(Surv(df$Time,df$Status)~strata(df$Gtype)) already gives the same result as print(exsurv) from my example... – Crawdaunt Jan 24 '22 at 16:17

2 Answers2

3

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.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
1

As usual, I was making it way more complicated by not understanding the basic survival function. The basic summary(exsurv) does give you median lifespan, mean lifespan, confidence intervals, etc... from the survfit function.

exsurv<-survfit(Surv(Time, Status)~ strata(Gtype))

You can put the summary(exsurv) data into a table using the code below

survoutput<-summary(exsurv)$table

And then just save to .csv as an ouput

write.csv(survoutput, file="exsurvoutput.csv")
Crawdaunt
  • 89
  • 1
  • 12