在purrr coxph结果之后,将子组添加到CSV输出文件-R版本4.1.2(2021-11-01)

发布于 2025-01-28 19:28:36 字数 2269 浏览 4 评论 0原文

下面的代码通过子组分配数据框,并将结果打印到offonentiation之后的CSV文件。我想将子组名称添加为最后一列,但不确定该怎么做。任何帮助将不胜感激。虚构的示例数据代码如下。

 library(survival)
 library(purrr)

 mydata <- read.table(header=T, 
                      text="age    Sex    survival    out_stroke out_cancer 
 out_respiratory id  tstart  tstop region
 51   1   1.419178082 2 1 1 1 0 50 1
 60    2   5   1 2 2 2 0 50 1
 49    1   1.082191781 2 2 2 3 0 50 2
 83    2   0.038356164 1 1 2 4 0 50 2
 68    1   0.77260274  2 1 2 5 0 50 1
 30    2   -0  2 1 2 6 50 0 2 
 44    1   2.336986301 1 2 1 7 0 100 1
 76    2   1.271232877 1 2 2 8 0 100 2")

 mydata$Sex<-ifelse(mydata$Sex==1, "Male", "Female")
 mydata$Sex <- factor(mydata$Sex, levels = c("Female","Male"))
 mydata$Sex = relevel(mydata$Sex, ref = "Female")

 outcomes <- names(mydata[4:6])

 cov <- c("region: ", "age: ")
 cov_name<-c("region + age")


 writeLines(c("OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP"), "HR_new.csv")

 lapply(split(mydata, mydata$Sex), function(y)
   purrr::map(outcomes, function(x) {
     f <- as.formula(paste("Surv(survival, event=", x, ") ~ ",cov_name))
     model <- coxph(f, y)
     model$call$formula <- f
     s <- summary(model)
     cat(paste0(substring(x,5,40),' ',cov, apply(s$coefficients, 1, 
                                                 function(x) {  
                                                   paste0(" ", round(exp(x[1]), 2),
                                                     ' ', round(exp(x[1] - 1.96 * x[3]), 2),
                                                     ' ', round(exp(x[1] + 1.96 * x[3]), 2), 
                                                     " ", round((x[3]), 4)," ",
                                                     " ", summary(model)$nevent)}),
           collapse = '\n'), '\n', sep = '', file = paste0('HR_new.csv'), 
    append = TRUE)
invisible(model)
  })
)

但我想要以下内容,其中显示了最后一列中的子组

“在此处输入图像说明”

The code below splits the dataframe by subgroup and prints the results to a csv file after exponentiation. I would like to add the subgroup name as a final column but am not sure how to do that. Any help would be appreciated. Code for made up sample data is below.

 library(survival)
 library(purrr)

 mydata <- read.table(header=T, 
                      text="age    Sex    survival    out_stroke out_cancer 
 out_respiratory id  tstart  tstop region
 51   1   1.419178082 2 1 1 1 0 50 1
 60    2   5   1 2 2 2 0 50 1
 49    1   1.082191781 2 2 2 3 0 50 2
 83    2   0.038356164 1 1 2 4 0 50 2
 68    1   0.77260274  2 1 2 5 0 50 1
 30    2   -0  2 1 2 6 50 0 2 
 44    1   2.336986301 1 2 1 7 0 100 1
 76    2   1.271232877 1 2 2 8 0 100 2")

 mydata$Sex<-ifelse(mydata$Sex==1, "Male", "Female")
 mydata$Sex <- factor(mydata$Sex, levels = c("Female","Male"))
 mydata$Sex = relevel(mydata$Sex, ref = "Female")

 outcomes <- names(mydata[4:6])

 cov <- c("region: ", "age: ")
 cov_name<-c("region + age")


 writeLines(c("OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP"), "HR_new.csv")

 lapply(split(mydata, mydata$Sex), function(y)
   purrr::map(outcomes, function(x) {
     f <- as.formula(paste("Surv(survival, event=", x, ") ~ ",cov_name))
     model <- coxph(f, y)
     model$call$formula <- f
     s <- summary(model)
     cat(paste0(substring(x,5,40),' ',cov, apply(s$coefficients, 1, 
                                                 function(x) {  
                                                   paste0(" ", round(exp(x[1]), 2),
                                                     ' ', round(exp(x[1] - 1.96 * x[3]), 2),
                                                     ' ', round(exp(x[1] + 1.96 * x[3]), 2), 
                                                     " ", round((x[3]), 4)," ",
                                                     " ", summary(model)$nevent)}),
           collapse = '\n'), '\n', sep = '', file = paste0('HR_new.csv'), 
    append = TRUE)
invisible(model)
  })
)

enter image description here

But I would like the following which shows the subgroup in the last column

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2025-02-04 19:28:36
writeLines(c("OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP"), "HR_new.csv")

lapply(split(mydata, mydata$Sex), function(y)
  purrr::map(outcomes, function(x) {
    f <- as.formula(paste("Surv(survival, event=", x, ") ~ ",cov_name))
    model <- coxph(f, y)
    model$call$formula <- f
    s <- summary(model)
    cat(paste0(substring(x,5,40),' ',cov, apply(s$coefficients, 1, 
                                                function(x) {  
                                                  paste0(" ", round(exp(x[1]), 2),
                                                         ' ', round(exp(x[1] - 1.96 * x[3]), 2),
                                                         ' ', round(exp(x[1] + 1.96 * x[3]), 2), 
                                                         " ", round((x[3]), 4)," ",
                                                         " ", summary(model)$nevent, 
                                                         " ", y$Sex[1])}),
               collapse = '\n'), '\n', sep = '', file = paste0('HR_new.csv'), 
        append = TRUE)
    invisible(model)
  })
)


# OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP
# stroke region:  5094.96 0 Inf 101330.9473  1 Female
# stroke age:  0.62 0 Inf 1331.5643  1 Female
# cancer region:  65019409.08 0 Inf 40192.9701  2 Female
# cancer age:  1.22 0 Inf 2512.0603  2 Female
# respiratory region:  2778652312.63 0 Inf 24930.3879  4 Female
# respiratory age:  0.94 0.82 1.08 0.0704  4 Female
# stroke region:  236112975987.23 0 Inf 30239.6491  3 Male
# stroke age:  16.05 0 Inf 2329.6551  3 Male
# cancer region:  4170531618.22 0 Inf 45664.714  2 Male
# cancer age:  1 0 Inf 11992.1384  2 Male
# respiratory region:  53874452658.47 0 Inf 17348.6865  2 Male
# respiratory age:  10.85 0 Inf 1545.2618  2 Male
writeLines(c("OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP"), "HR_new.csv")

lapply(split(mydata, mydata$Sex), function(y)
  purrr::map(outcomes, function(x) {
    f <- as.formula(paste("Surv(survival, event=", x, ") ~ ",cov_name))
    model <- coxph(f, y)
    model$call$formula <- f
    s <- summary(model)
    cat(paste0(substring(x,5,40),' ',cov, apply(s$coefficients, 1, 
                                                function(x) {  
                                                  paste0(" ", round(exp(x[1]), 2),
                                                         ' ', round(exp(x[1] - 1.96 * x[3]), 2),
                                                         ' ', round(exp(x[1] + 1.96 * x[3]), 2), 
                                                         " ", round((x[3]), 4)," ",
                                                         " ", summary(model)$nevent, 
                                                         " ", y$Sex[1])}),
               collapse = '\n'), '\n', sep = '', file = paste0('HR_new.csv'), 
        append = TRUE)
    invisible(model)
  })
)


# OUTCOME COVARIATE HR LCI UCI SE NEVENT SUBGROUP
# stroke region:  5094.96 0 Inf 101330.9473  1 Female
# stroke age:  0.62 0 Inf 1331.5643  1 Female
# cancer region:  65019409.08 0 Inf 40192.9701  2 Female
# cancer age:  1.22 0 Inf 2512.0603  2 Female
# respiratory region:  2778652312.63 0 Inf 24930.3879  4 Female
# respiratory age:  0.94 0.82 1.08 0.0704  4 Female
# stroke region:  236112975987.23 0 Inf 30239.6491  3 Male
# stroke age:  16.05 0 Inf 2329.6551  3 Male
# cancer region:  4170531618.22 0 Inf 45664.714  2 Male
# cancer age:  1 0 Inf 11992.1384  2 Male
# respiratory region:  53874452658.47 0 Inf 17348.6865  2 Male
# respiratory age:  10.85 0 Inf 1545.2618  2 Male
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文