This is a test version of Biostars. For the public version, visit https://www.biostars.org.
R: exporting summary of coxph object

Dear colleges,

I performed Cox regression for proportional hazard using R package "survival". I obtained the results in form of "coxph" object, which seems to be a list. My problem is that I (and it seems like Internet too) do not know how to export it as .txt file for example. Please, give some feedback, I'll appreciate it.

Best regards!

r cox regression data export

a simple summary is:

c <- coxph(Surv(x~y)) 
summary(c)

then if you want to extract p.value and hazard ratio:

pvc <- coef(summary(c))[,5]
hr <- round(coef(summary(c))[,2],3)

Thank you for your answer, however I am familiar with those commands. I do not know how to export it to text or csv file.

1 answer

Just use capture.output. This shows the code and offers only the first 8 lines of 20 lines of text:

fit <- coxph(Surv(time, status) ~ age + sex, lung) txt.out <- capture.output(summary(fit)) txt.out [1] "Call:"
[2] "coxph(formula = Surv(time, status) ~ age + sex, data = lung)"
[3] ""
[4] " n= 228, number of events= 165 "
[5] ""
[6] " coef exp(coef) se(coef) z Pr(>|z|) "
[7] "age 0.017045 1.017191 0.009223 1.848 0.06459 . "
[8] "sex -0.513219 0.598566 0.167458 -3.065 0.00218 **"

-- David

Log in to answer this question.