gpt4 book ai didi

r - 将 "survfit()"输出转换为矩阵或数据帧

转载 作者:行者123 更新时间:2023-12-03 08:11:04 24 4
gpt4 key购买 nike

下面的示例数据。

我的基本问题是,单独运行“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()”给出相同的结果。

最佳答案

是的,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 文件轻松导入到任何单词或电子表格中。

关于r - 将 "survfit()"输出转换为矩阵或数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70833633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com