拆分包含多个因子的输出列表

时间:2018-06-07 02:35:01

标签: r split

假设我有这三个向量:

time <- c(306,455,1010,210,883,1022,310,361,218,166)
status <- c(0,1,0,1,0,0,1,0,1,1)
gender <- c("Male","Male","Female","Male","Male","Male","Female","Female","Female","Female")

我希望进行生存分析并获得摘要。

A <- survfit(Surv(time, status)~gender)
summary(A, censored = TRUE)

输出如下:

> summary(A, censored = TRUE)
  Call: survfit(formula = Surv(time, status) ~ gender)

                gender=Female 
  time n.risk n.event survival std.err lower 95% CI upper 95% CI
  166      5       1      0.8   0.179        0.516            1
  218      4       1      0.6   0.219        0.293            1
  310      3       1      0.4   0.219        0.137            1
  361      2       0      0.4   0.219        0.137            1
  1010     1       0      0.4   0.219        0.137            1

                 gender=Male 
  time n.risk n.event survival std.err lower 95% CI upper 95% CI
  210      5       1    0.800   0.179        0.516            1
  306      4       0    0.800   0.179        0.516            1
  455      3       1    0.533   0.248        0.214            1
  883      2       0    0.533   0.248        0.214            1
  1022     1       0    0.533   0.248        0.214            1

我的问题是,有什么方法可以将输出分成男性和女性。例如:

output_Female <- ?????
output_Female
                    output_Female 
  time n.risk n.event survival std.err lower 95% CI upper 95% CI
  166      5       1      0.8   0.179        0.516            1
  218      4       1      0.6   0.219        0.293            1
  310      3       1      0.4   0.219        0.137            1
  361      2       0      0.4   0.219        0.137            1
  1010     1       0      0.4   0.219        0.137            1

output_Male <- ?????
output_Male
                    output_Male 
  time n.risk n.event survival std.err lower 95% CI upper 95% CI
  166      5       1      0.8   0.179        0.516            1
  218      4       1      0.6   0.219        0.293            1
  310      3       1      0.4   0.219        0.137            1
  361      2       0      0.4   0.219        0.137            1
  1010     1       0      0.4   0.219        0.137            1

1 个答案:

答案 0 :(得分:2)

以下是使用tidy

的选项
library(broom)
library(dplyr)
tidy(A, censored = TRUE) %>% 
   split(.$strata)

base R

txt <- capture.output(summary(A, censored = TRUE))
ind <- cumsum(grepl("gender=", txt))
lst <- lapply(split(txt[ind >0], ind[ind >0]), function(x)
       read.table(text = x[-(1:2)], header = FALSE))

nm1 <- scan(text= gsub("\\s+[0-9]|%\\s+", ".", txt[4]), quiet = TRUE, what = "")
lst <- lapply(lst, setNames, nm1)