[R] Surv object in data.frame

Heinz Tuechler tuechler at gmx.at
Wed Mar 15 17:35:04 CET 2006

Dear All,

a Surv object I put in a data frame behaves somehow unexpected (see example).
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

More information about the R-help mailing list