一种将“ survfit”对象的某些元素提取到数据框中的简洁方法

mat*_*ash 3 r list dataframe

我从survival库中加载数据集,并生成一个survfit对象:

library(survival)
data(lung)
lung$SurvObj <- with(lung, Surv(time, status == 2))
fit <- survfit(SurvObj ~ 1, data = lung, conf.type = "log-log")
Run Code Online (Sandbox Code Playgroud)

该对象是一个列表:

> str(fit)
List of 13
 $ n        : int 228
 $ time     : int [1:186] 5 11 12 13 15 26 30 31 53 54 ...
 $ n.risk   : num [1:186] 228 227 224 223 221 220 219 218 217 215 ...
 $ n.event  : num [1:186] 1 3 1 2 1 1 1 1 2 1 ...
 ...
Run Code Online (Sandbox Code Playgroud)

现在,我指定一些要变成数据帧的成员(长度相同):

members <- c("time", "n.risk", "n.event") 
Run Code Online (Sandbox Code Playgroud)

我在寻找一个简洁的方式,使与三个列表成员列的数据帧,与列命名timen.riskn.event fit$timefit$n.riskfit$n.event

因此,结果数据帧应如下所示:

           time     n.risk       n.event
  [1,]        5        228             1
  [2,]       11        227             3
  [3,]       12        224             1
  ...
Run Code Online (Sandbox Code Playgroud)

李哲源*_*李哲源 5

还行吧

data.frame(unclass(fit)[members])
Run Code Online (Sandbox Code Playgroud)

另一种(更规范的)方式是

with(fit, data.frame(time, n.risk, n.event))
Run Code Online (Sandbox Code Playgroud)