【问题标题】:Convert "survfit()" output to a matrix or dataframe将 \"survfit()\" 输出转换为矩阵或数据框
【发布时间】:2022-11-25 19:02:57
【问题描述】:

下面的示例数据。

我的基本问题是单独运行“survfit”会给出一个很好的列,其中包含每个类别的中值寿命,这是我想从我的 survfit 数据中提取的内容。理想情况下,我想将此“survfit”输出导出为数据框/表格,并最终保存为 .csv。但是我尝试时遇到错误。

感谢您的帮助/建议!

示例数据:

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

以及我想作为数据框获得的“survfit”输出:

> 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

编辑: 这个问题的早期版本过多地包含了 print() 函数。 “print(survfit)”和“survfit()”给出相同的结果。

【问题讨论】:

  • 查看对象的结构str(exsurv)。您肯定会找到打印的数据。 (未测试)
  • 您可以使用 broom 包,例如:results &lt;- broom::tidy(exsurv),它将为您提供一个数据框,其中的参数列在列中。
  • 对中位寿命统计数据特别感兴趣,因为这是默认情况下在 print() 函数中按 Gtype 分层计算的,而不是 summary(exsurv)、str(exsurv)、broom::tidy(exsurv)。我刚刚意识到 print() 对此也是多余的。只是运行 survfit(Surv(df$Time,df$Status)~strata(df$Gtype)) 已经给出了与我示例中的 print(exsurv) 相同的结果......

标签: r summary survival


【解决方案1】:

是的 broom::tidy 功能有效。

'mymk1' - 是在我的原始生存数据集上使用 survfit 的对象输出

我试过了,效果很好

results <- broom::tidy(mykm1)

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

然后可以轻松地将 csv 文件导入到任何 word 或电子表格中。

【讨论】:

    【解决方案2】:

    像往常一样,我因为不了解基本的生存功能而使它变得更加复杂。基本摘要 (exsurv) 确实会从 survfit 函数中为您提供中位寿命、平均寿命、置信区间等。

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

    您可以使用下面的代码将摘要(exsurv)数据放入表格中

    survoutput<-summary(exsurv)$table
    

    然后保存为 .csv 作为输出

    write.csv(survoutput, file="exsurvoutput.csv")
    

    【讨论】:

      猜你喜欢
      • 2012-05-20
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      相关资源
      最近更新 更多