[R] Extracting results from a function output

William Dunlap wdunlap at tibco.com
Mon Oct 17 18:29:37 CEST 2011


Often the summary() methods for the modelling functions
contain the kinds of details you are looking for.  Sometimes
there are special extractor functions but often not.
You have to study the help file for summary.<objectClass>
and the vignettes for the package, and perhaps do a little
experimentation to figure it out.

  > library(survival)
  > fit <- survfit(Surv(time, status) ~ x, data=leukemia)
  > fit
  Call: survfit(formula = Surv(time, status) ~ x, data = leukemia)
  
                  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
  > summary(fit)$table[, "median"]
     x=Maintained x=Nonmaintained 
               31              23 
  > summary(fit)$table["x=Maintained", "median"]
  [1] 31


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of R. Michael
> Weylandt
> Sent: Monday, October 17, 2011 8:51 AM
> To: Cem Girit
> Cc: r-help at r-project.org
> Subject: Re: [R] Extracting results from a function output
> 
> Assuming I'm reading your post correctly -- fit is the output object
> and the lines following >fit are the console output, which implicitly
> calls print -- it looks like the median isn't stored in fit but is
> calculated on the fly in the print call. But this doesn't seem
> consistent with your claim that:
> 
> "Print" function does not reveal them either.
> 
> Could you perhaps clarify your statement about print(fit)?
> 
> Cheers from Princeton!
> 
> Michael Weylandt
> 
> On Mon, Oct 17, 2011 at 11:13 AM, Cem Girit <girit at biopticon.com> wrote:
> > Hello,
> >
> > I am having hard time obtaining a value from a function. "fit" is a survival
> > function that produces some results, such as "median", "confidence
> > intervals" etc. But str() function does not list these values. How can I
> > extract these to be able use them? For example, I need "median" value for
> > the group DrugA which is 48. "Print" function does not reveal them either.
> >
> > Thank you.
> >
> >> fit
> > Call: survfit(formula = Surv(tT, dT) ~ gT, conf.type = "log-log")
> >
> > records n.max n.start events median 0.95LCL 0.95UCL
> > gT=DrugA 9 9 9 6 48 32 NA
> > gT=DrugB 9 9 9 3 NA 42 NA
> > gT=DrugC 9 9 9 4 NA 42 NA
> > gT=Vehicle 9 9 9 8 40 37 45
> >
> >> str(fit)
> > List of 14
> > $ n : int [1:4] 9 9 9 9
> > $ time : num [1:28] 32 43 45 46 48 54 55 60 62 42 ...
> > $ n.risk : num [1:28] 9 8 7 6 5 4 3 2 1 9 ...
> > $ n.event : num [1:28] 1 1 1 1 1 0 1 0 0 2 ...
> > $ n.censor : num [1:28] 0 0 0 0 0 1 0 1 1 0 ...
> > $ surv : num [1:28] 0.889 0.778 0.667 0.556 0.444 ...
> > $ type : chr "right"
> > $ strata : Named int [1:4] 9 6 6 7
> > ..- attr(*, "names")= chr [1:4] "gT=DrugA" "gT=DrugB" "gT=DrugC"
> > "gT=Vehicle"
> > $ std.err : num [1:28] 0.118 0.178 0.236 0.298 0.373 ...
> > $ upper : num [1:28] 0.984 0.939 0.878 0.805 0.719 ...
> > $ lower : num [1:28] 0.433 0.365 0.282 0.204 0.136 ...
> > $ conf.type: chr "log-log"
> > $ conf.int : num 0.95
> > $ call : language survfit(formula = Surv(tT, dT) ~ gT, conf.type =
> > "log-log")
> > - attr(*, "class")= chr "survfit"
> >
> >
> >
> > Cem
> >
> >
> >
> > Cem Girit, PhD
> >
> >
> >
> > Biopticon Corporation
> >
> > 182 Nassau Street, Suite 204
> >
> > Princeton, NJ 08542
> >
> > Tel: (609)-853-0231
> >
> > Email:  <mailto:cgirit at biopticon.com> girit at biopticon.com
> >
> >
> >
> > This email message and any attachments are confidential ...{{dropped:14}}
> >
> > ______________________________________________
> > 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.
> >
> 
> ______________________________________________
> 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