简体   繁体   English

在R中按层将具有不等矢量长度的列表转换为数据帧

[英]convert list with unequal vector length to data frame by strata in R

I have the output from a coxph function, which is estimated by strata. 我有一个coxph函数的输出,该函数由层估算。 I would like to transform this output from a list into a data frame. 我想将此输出从列表转换为数据框。 The code I ran for coxph is below: 我为coxph运行的代码如下:

k <- coxph(Surv(cum.goodp, dlq.next) ~ rpc.length + cluster(itemcode) + strata(sector), data = nr.sample)
m <- summary(survfit(k))

There are twenty different strata used to estimate the coxph. 有二十个不同的层次用于估计Coxph。 Here is the structure of the list 这是列表的结构

List of 16
$ n        : int [1:20] 870 843 2278 603 6687 8618 15155 920 2598 654 ...
$ time     : num [1:870] 1 2 3 4 5 6 7 8 9 10 ...
$ n.risk   : num [1:870] 870 592 448 361 320 286 232 214 196 186 ...
$ n.event  : num [1:870] 246 126 77 34 33 25 18 18 8 6 ...
$ n.censor : num [1:870] 32 18 10 7 1 29 0 0 2 0 ...
$ strata   : Factor w/ 20 levels "sector=11","sector=21",..: 1 1 1 1 1 1 1 1 1 1 ...
$ surv     : num [1:870] 0.725 0.571 0.471 0.425 0.379 ...
$ type     : chr "right"
$ cumhaz   : num [1:870] 0.322 0.561 0.754 0.856 0.971 ...
$ std.err  : num [1:870] 0.015 0.017 0.0174 0.0174 0.0173 ...
$ upper    : num [1:870] 0.755 0.605 0.506 0.46 0.414 ...
$ lower    : num [1:870] 0.696 0.538 0.438 0.392 0.347 ...
$ conf.type: chr "log"
$ conf.int : num 0.95
$ call     : language survfit(formula = k)
$ table    : num [1:20, 1:7] 870 843 2278 603 6687 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:20] "sector=11" "sector=21" "sector=22" "sector=23" ...
.. ..$ : chr [1:7] "records" "n.max" "n.start" "events" ...
- attr(*, "class")= chr "summary.survfit"

I have done this before, but without strata. 我以前做过,但没有层次。 When I did not have strata I used the following approach: 当我没有阶层时,我使用以下方法:

col <- lapply(c(1 : 7), function(x) m[x])
tbl <- do.call(data.frame, col)

However, when I try that approach here, I get the familiar error: 但是,当我在这里尝试这种方法时,我得到了熟悉的错误:

cannot coerce class "c("survfit.cox", "survfit")" to a data.frame

All columns have the same name, but they are of different length. 所有列都具有相同的名称,但是长度不同。 If possible, I would like to add a column to the final data frame that contains the particular strata that the results are for. 如果可能的话,我想在最终数据框中添加一列,其中包含结果所针对的特定层次。 Is there a way to do this? 有没有办法做到这一点? It doesn't have to be in base R. Any help would be much appreciated. 它不必位于基数R中。任何帮助将不胜感激。 Thanks so much. 非常感谢。

This problem can be solved via the tidy function in the broom package. 这个问题可以通过扫帚包装中的整齐功能解决。 For the example above, the code is: 对于上面的示例,代码为:

n <- survfit(k)
df <- tidy(n)

The tidy function produces a data frame with a variable "strata". 整齐的函数产生一个带有变量“层”的数据帧。 It does not, however, provide the median and mean, but they can be estimated from the data frame df if one were so inclined. 但是,它不提供中位数和均值,但是如果倾斜得这么大,则可以从数据帧df估计它们。 If the survfit object has multiple strata, the glance(list) cannot provide the median or mean. 如果残差对象具有多个层次,则glance(list)无法提供中位数或均值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM