[R] creating Surv object from character vector

David Winsemius dwinsemius at comcast.net
Tue Sep 29 21:30:57 CEST 2009


On Sep 29, 2009, at 3:08 PM, Erik Iverson wrote:

> Hello,
>
> I have
>
> srv <- Surv(sample(1:10), sample(0:1, 10, replace = TRUE))
> srv
>
> [1]  1  10   2+  8   6+  7+  3   5+  4+  9+
> srv.char <- as.character(srv)
> srv.char
>
> [1] " 1 " "10 " " 2+" " 8 " " 6+" " 7+" " 3 " " 5+" " 4+" " 9+"
>
> Is there an inverse to as.character(srv).  That is, I would like
>
> identical(srv, ???(srv.char)) to return TRUE, where ??? is some  
> unknown function.  I don't think it exists, but maybe I'm wrong. I  
> suppose it would be easy enough to roll my own...

Exactly. as.character strips off all the attributes and adds "+"'s to  
the (numeric) times that have status=0 and " "'s (or even "-"'s) to  
others:

 > str(as.character(srv))
  chr [1:10] " 4+" " 2 " " 8 " " 3 " " 6 " " 1 " " 5+" " 7+" "10 " ...


Whereas:
 > str(srv)
  int [1:10, 1:2]  4+  2   8   3   6   1   5+  7+ 10   9+
  - attr(*, "dimnames")=List of 2
   ..$ : NULL
   ..$ : chr [1:2] "time" "status"
  - attr(*, "type")= chr "right"
  - attr(*, "units")= chr "Day"
  - attr(*, "time.label")= chr "sample(1:10)"
  - attr(*, "event.label")= chr "sample(0:1, 10, replace = TRUE)"

 > srv[ , 1]
  [Day]
  [1]  4  2  8  3  6  1  5  7 10  9
 > srv[ , 2]
  [Day]
  [1] 0 1 1 1 1 1 0 0 1 0


 > methods(as.character)
Shows that there is an as.character.Surv function

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list