[R] Surv object in data.frame

Robert Baer rbaer at atsu.edu
Wed Mar 15 18:59:23 CET 2006

This does work:
coxph(survobj~group, data=df.test[[1]]) # this works like your original

To get insight compare:

Then note the 2nd sentence of the  following from ?coxph

 formula: a formula object, with the response on the left of a '~'
          operator, and the terms on the right.  The response must be a
          survival object as returned by the 'Surv' function.

Robert W. Baer, Ph.D.
Associate Professor
Department of Physiology
A. T. Still University of Health Science
800 W. Jefferson St.
Kirksville, MO 63501-1497 USA

Dear All,

a Surv object I put in a data frame behaves somehow unexpected (see
If I do a Cox regression on the original Surv object it works. If I put it
in a data.frame and do the regression on the data frame it does not work.
Seemingly it has to do with the class attribute, because if I change the
class attribute to let "Surv" appeare first, again it works.
Is this known? Should I have found information on it?
Any comments?


Heinz Tüchler

## example data
starttime <- rep(0,5)
stoptime  <- 1:5
event     <- c(1,0,1,1,1)
group     <- c(1,1,1,2,2)
## Surv object
survobj   <- Surv(starttime, stoptime, event)
## Cox-regression
coxph(survobj~group) # this works
## put Surv object in data.frame
df.test <- data.frame(survobj=I(survobj), group)
## Cox-regression on data.frame
coxph(survobj~group, data=df.test) # this does not work
attr(df.test$survobj, 'class') # survobject has class "AsIs", "Surv"
attr(df.test$survobj, 'class') <- c('Surv', 'AsIs') # put Surv first
attr(df.test$survobj, 'class') # survobject has class "Surv", "AsIs"
coxph(survobj~group, data=df.test) # now it works

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide!

More information about the R-help mailing list