[R] How to extract table results from survival summary object
David L Carlson
dcarlson at tamu.edu
Tue Oct 7 17:43:41 CEST 2014
This will create a data.frame containing the results of the summary(mod) object. You can find out what that is using the command ?summary.survfit. You have an error in your example since death is not a variable in lung:
> library(survival)
> data(lung)
> mod <- with(lung, survfit(Surv(time, status)~ 1))
> res <- summary(mod)
> str(res)
List of 14
$ n : int 228
$ time : num [1:139] 5 11 12 13 15 26 30 31 53 54 ...
$ n.risk : num [1:139] 228 227 224 223 221 220 219 218 217 215 ...
$ n.event : num [1:139] 1 3 1 2 1 1 1 1 2 1 ...
$ n.censor : num [1:139] 0 0 0 0 0 0 0 0 0 0 ...
$ surv : num [1:139] 0.996 0.982 0.978 0.969 0.965 ...
$ type : chr "right"
$ std.err : num [1:139] 0.00438 0.00869 0.0097 0.01142 0.01219 ...
$ upper : num [1:139] 1 1 0.997 0.992 0.989 ...
$ lower : num [1:139] 0.987 0.966 0.959 0.947 0.941 ...
$ conf.type: chr "log"
$ conf.int : num 0.95
$ call : language survfit(formula = Surv(time, status) ~ 1)
$ table : Named num [1:7] 228 228 228 165 310 285 363
..- attr(*, "names")= chr [1:7] "records" "n.max" "n.start" "events" ...
- attr(*, "class")= chr "summary.survfit"
> # Extract the columns you want
> cols <- lapply(c(2:6, 8:10) , function(x) res[x])
> # Combine the columns into a data frame
> tbl <- do.call(data.frame, cols)
> str(tbl)
'data.frame': 139 obs. of 8 variables:
$ time : num 5 11 12 13 15 26 30 31 53 54 ...
$ n.risk : num 228 227 224 223 221 220 219 218 217 215 ...
$ n.event : num 1 3 1 2 1 1 1 1 2 1 ...
$ n.censor: num 0 0 0 0 0 0 0 0 0 0 ...
$ surv : num 0.996 0.982 0.978 0.969 0.965 ...
$ std.err : num 0.00438 0.00869 0.0097 0.01142 0.01219 ...
$ upper : num 1 1 0.997 0.992 0.989 ...
$ lower : num 0.987 0.966 0.959 0.947 0.941 ...
Since res is a list containing the columns you want plus other information, we need to extract the needed columns from res and then combine those columns into a data.frame.
-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Juan Andres Hernandez
Sent: Tuesday, October 7, 2014 8:41 AM
To: r-help at r-project.org
Subject: [R] How to extract table results from survival summary object
Hi. I need to extract the "matrix" or "data.frame" results from a survival
object.
library(survival)
data(lung)
mod=with(lung, survfit(Surv(time,death)~ 1))
res=summary(mod)
res show in consola the "matrix" I am looking for, but I can't find the way
to save or assign this table to an object. Anyone knows how to solve it.
Thank's in advance
Juan A. Hernández
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list