11

I've created this model:

model <- survfit(Surv(time,status)~c$sex)
model

and the output is:

Call: survfit(formula = Surv(time, status) ~ c$sex)

             records n.max n.start events median 0.95LCL 0.95UCL
c$sex=female      15    15      15      8    720     517      NA
c$sex=male        28    28      28     23    234     145     712    

So, I want to extract the median for males and the same for females, but have no idea how to do it. Here are my attempts to do it:

>model$median
NULL

>summary(model)$table[, "median"]
c$sex=female c$sex=male 
       720.0            234.5 

I want each one of the values alone ("720" and "234.5"), can somebody help me?

Thanks in advance

JMarcelino
  • 904
  • 4
  • 13
  • 30
  • 1
    Maybe `summary(model)$table[, "median"][1]` for female and `summary(model)$table[, "median"][2]` for male? That said, guessing `summary(model)$table[, "median"]` is a vector. – alexis_laz Oct 23 '13 at 17:09
  • 2
    Hi, welcome to SO. Since you are quite new here, you might want to read the [**about**](http://stackoverflow.com/about) and [**FAQ**](http://stackoverflow.com/faq) sections of the website to help you get the most out of it. If an answer does solve your problem you may want to *consider* upvoting and/or marking it as accepted to show the question has been answered, by ticking the little green check mark next to the suitable answer. You are **not** obliged to do this, but it helps keep the site clean of unanswered questions and rewards those who take the time to solve your problem. – Simon O'Hanlon Oct 23 '13 at 17:15

2 Answers2

15

You've already got it. All you are seeing printed to screen are the names attributes of the length 2 vector.

fit <- survfit(Surv(time, status) ~ x, data = aml)
summary(fit)$table
#                records n.max n.start events median 0.95LCL 0.95UCL
#x=Maintained         11    11      11      7     31      18      NA
#x=Nonmaintained      12    12      12     11     23       8      NA

#  Access first value like any other vector
summary(fit)$table[,'median'][1]
#x=Maintained 
#          31

To print without names use unname()...

unname(summary(fit)$table[,'median'])
# [1] 31 23

But you do not need to unname() them to use them as a numeric value, that is just an aesthetic desire...

sum( summary(fit)$table[,'median'] )
[1] 54

For further proof (!) that it is a vector use str()...

str(summary(fit)$table[,'median'])
# Named num [1:2] 31 23
# - attr(*, "names")= chr [1:2] "x=Maintained" "x=Nonmaintained"
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
5

This also works:

> library(survMisc)
> fit <- survfit(Surv(time, status) ~ x, data = aml)
> median(fit)
                median
x=Maintained        31
x=Nonmaintained     23

And without the names (i.e. remove the structure of a data.frame):

> unname(unlist(median(fit)))
[1] 31 23

It's nice if you also want the confidence interval (default is 'log'):

> median(fit, CI=TRUE)
                median lower upper
x=Maintained        31    13    NA
x=Nonmaintained     23     5    43
dardisco
  • 5,086
  • 2
  • 39
  • 54
  • 1
    It's easier this way, I ask myself why didn't I thought of that. Thank you! – JMarcelino Oct 30 '13 at 10:13
  • 2
    This doesn't work for me. Trying your example throws the error `Error in xi == xj : comparison of these types is not implemented`. Actually, I don't see any `survMisc::median()` function available. Am I doing it wrong or has the library changed ? – Dan Chaltiel Aug 19 '18 at 11:05
  • 1
    @DanChaltiel +1, library has changed. – Thomas Moore May 28 '21 at 05:17