[R] Surv object in data.frame

Thomas Lumley tlumley at u.washington.edu
Wed Mar 15 18:23:06 CET 2006


On Wed, 15 Mar 2006, Heinz Tuechler wrote:

> 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?

Well, this is the sort of thing that happens when you use kludges like 
AsIs.

The problem is with [.AsIs
    survobj[,1] is supposed to be a vector of times (that's what [.Surv 
returns), but [.AsIs sticks the original class attribute on to it.

> str(survobj[,1])
  num [1:5] 0 0 0 0 0
> str(I(survobj)[,1])
Classes 'AsIs', 'Surv'  num [1:5] 0 0 0 0 0

The solution is not to use I() -- there's no problem with putting survival 
objects in a data frame
> df.right<-data.frame(survobj,group)
> df.right
   survobj group
1  (0,1 ]     1
2  (0,2+]     1
3  (0,3 ]     1
4  (0,4 ]     2
5  (0,5 ]     2


 	-thomas




More information about the R-help mailing list