使用发布包将 Cox 回归结果导出到 excel 或 word
Export Cox regression results to excel or word using publish package
看下面的代码
library(pec)
data(GBSG2,package="pec")
setDT(GBSG2)
GBSG2
library(survival)
library(prodlim)
library(Publish)
cox_lung <- coxph(Surv(time,cens)~tgrade+age+strata(menostat)+tsize+pnodes+progrec+estrec,data=GBSG2)
publish(cox_lung,org=TRUE)
输出:
> publish(cox_GBSG2,org=TRUE)
| Variable | Units | HazardRatio | CI.95 | p-value |
|-----------+-------+-------------+-------------+-----------|
| tgrade | I | Ref | | |
| | II | 1.86 | [1.14;3.03] | 0.01276 |
| | III | 2.24 | [1.32;3.78] | 0.00263 |
| age | | 0.99 | [0.97;1.01] | 0.27167 |
| tsize | | 1.01 | [1.00;1.01] | 0.06029 |
| pnodes | | 1.05 | [1.04;1.07] | < 0.001 |
| progrec | | 1.00 | [1.00;1.00] | < 0.001 |
| estrec | | 1.00 | [1.00;1.00] | 0.70522 |
我想知道是否有一种简单的方法可以将上述输出导出到 CSV、word 或 excel 文件。
这是使用 capture.output
和 gsub
获取 csv 文件的方法。我也在展示中间步骤:
cox_out <- capture.output( publish(cox_lung,org=TRUE) )
# Demonstration of intermediates (don't need to do these
gsub("[|]", ",",cox_out) # Change "|" to commas
[1] ", Variable , Units , HazardRatio , CI.95 , p-value ,"
[2] ",-----------+-------+-------------+-------------+-----------,"
[3] ", tgrade , I , Ref , , ,"
[4] ", , II , 1.86 , [1.14;3.03] , 0.01276 ,"
[5] ", , III , 2.24 , [1.32;3.78] , 0.00263 ,"
[6] ", age , , 0.99 , [0.97;1.01] , 0.27167 ,"
[7] ", tsize , , 1.01 , [1.00;1.01] , 0.06029 ,"
[8] ", pnodes , , 1.05 , [1.04;1.07] , < 0.001 ,"
[9] ", progrec , , 1.00 , [1.00;1.00] , < 0.001 ,"
[10] ", estrec , , 1.00 , [1.00;1.00] , 0.70522 ,"
[11] ""
gsub("[|]", ",",cox_out)[-2] # Remove second line
[1] ", Variable , Units , HazardRatio , CI.95 , p-value ,"
[2] ", tgrade , I , Ref , , ,"
[3] ", , II , 1.86 , [1.14;3.03] , 0.01276 ,"
[4] ", , III , 2.24 , [1.32;3.78] , 0.00263 ,"
[5] ", age , , 0.99 , [0.97;1.01] , 0.27167 ,"
[6] ", tsize , , 1.01 , [1.00;1.01] , 0.06029 ,"
[7] ", pnodes , , 1.05 , [1.04;1.07] , < 0.001 ,"
[8] ", progrec , , 1.00 , [1.00;1.00] , < 0.001 ,"
[9] ", estrec , , 1.00 , [1.00;1.00] , 0.70522 ,"
[10] ""
gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ) # wrap second gsub to take out leading and trailing commas
[1] " Variable , Units , HazardRatio , CI.95 , p-value "
[2] " tgrade , I , Ref , , "
[3] " , II , 1.86 , [1.14;3.03] , 0.01276 "
[4] " , III , 2.24 , [1.32;3.78] , 0.00263 "
[5] " age , , 0.99 , [0.97;1.01] , 0.27167 "
[6] " tsize , , 1.01 , [1.00;1.01] , 0.06029 "
[7] " pnodes , , 1.05 , [1.04;1.07] , < 0.001 "
[8] " progrec , , 1.00 , [1.00;1.00] , < 0.001 "
[9] " estrec , , 1.00 , [1.00;1.00] , 0.70522 "
[10] ""
#
cat( paste( gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ), collapse="\n"),
# need paste( ... , colapse="\n") to append linefeeds at each line.
file="cox_out.csv" )
而不是使用 cat
也许使用 write.table
会更干净(因为它避免了需要“手动”添加换行符):
cox_out <- capture.output( publish(cox_lung,org=TRUE) )
write.table( gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ), file="cox_out.csv" )
看下面的代码
library(pec)
data(GBSG2,package="pec")
setDT(GBSG2)
GBSG2
library(survival)
library(prodlim)
library(Publish)
cox_lung <- coxph(Surv(time,cens)~tgrade+age+strata(menostat)+tsize+pnodes+progrec+estrec,data=GBSG2)
publish(cox_lung,org=TRUE)
输出:
> publish(cox_GBSG2,org=TRUE)
| Variable | Units | HazardRatio | CI.95 | p-value |
|-----------+-------+-------------+-------------+-----------|
| tgrade | I | Ref | | |
| | II | 1.86 | [1.14;3.03] | 0.01276 |
| | III | 2.24 | [1.32;3.78] | 0.00263 |
| age | | 0.99 | [0.97;1.01] | 0.27167 |
| tsize | | 1.01 | [1.00;1.01] | 0.06029 |
| pnodes | | 1.05 | [1.04;1.07] | < 0.001 |
| progrec | | 1.00 | [1.00;1.00] | < 0.001 |
| estrec | | 1.00 | [1.00;1.00] | 0.70522 |
我想知道是否有一种简单的方法可以将上述输出导出到 CSV、word 或 excel 文件。
这是使用 capture.output
和 gsub
获取 csv 文件的方法。我也在展示中间步骤:
cox_out <- capture.output( publish(cox_lung,org=TRUE) )
# Demonstration of intermediates (don't need to do these
gsub("[|]", ",",cox_out) # Change "|" to commas
[1] ", Variable , Units , HazardRatio , CI.95 , p-value ,"
[2] ",-----------+-------+-------------+-------------+-----------,"
[3] ", tgrade , I , Ref , , ,"
[4] ", , II , 1.86 , [1.14;3.03] , 0.01276 ,"
[5] ", , III , 2.24 , [1.32;3.78] , 0.00263 ,"
[6] ", age , , 0.99 , [0.97;1.01] , 0.27167 ,"
[7] ", tsize , , 1.01 , [1.00;1.01] , 0.06029 ,"
[8] ", pnodes , , 1.05 , [1.04;1.07] , < 0.001 ,"
[9] ", progrec , , 1.00 , [1.00;1.00] , < 0.001 ,"
[10] ", estrec , , 1.00 , [1.00;1.00] , 0.70522 ,"
[11] ""
gsub("[|]", ",",cox_out)[-2] # Remove second line
[1] ", Variable , Units , HazardRatio , CI.95 , p-value ,"
[2] ", tgrade , I , Ref , , ,"
[3] ", , II , 1.86 , [1.14;3.03] , 0.01276 ,"
[4] ", , III , 2.24 , [1.32;3.78] , 0.00263 ,"
[5] ", age , , 0.99 , [0.97;1.01] , 0.27167 ,"
[6] ", tsize , , 1.01 , [1.00;1.01] , 0.06029 ,"
[7] ", pnodes , , 1.05 , [1.04;1.07] , < 0.001 ,"
[8] ", progrec , , 1.00 , [1.00;1.00] , < 0.001 ,"
[9] ", estrec , , 1.00 , [1.00;1.00] , 0.70522 ,"
[10] ""
gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ) # wrap second gsub to take out leading and trailing commas
[1] " Variable , Units , HazardRatio , CI.95 , p-value "
[2] " tgrade , I , Ref , , "
[3] " , II , 1.86 , [1.14;3.03] , 0.01276 "
[4] " , III , 2.24 , [1.32;3.78] , 0.00263 "
[5] " age , , 0.99 , [0.97;1.01] , 0.27167 "
[6] " tsize , , 1.01 , [1.00;1.01] , 0.06029 "
[7] " pnodes , , 1.05 , [1.04;1.07] , < 0.001 "
[8] " progrec , , 1.00 , [1.00;1.00] , < 0.001 "
[9] " estrec , , 1.00 , [1.00;1.00] , 0.70522 "
[10] ""
#
cat( paste( gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ), collapse="\n"),
# need paste( ... , colapse="\n") to append linefeeds at each line.
file="cox_out.csv" )
而不是使用 cat
也许使用 write.table
会更干净(因为它避免了需要“手动”添加换行符):
cox_out <- capture.output( publish(cox_lung,org=TRUE) )
write.table( gsub( "^,|,$", "", gsub("[|]", ",",cox_out)[-2] ), file="cox_out.csv" )