[R] Passing a string variable to Surv

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed May 23 23:47:02 CEST 2001


Vincenzo Forgetta <vforge at po-box.mcgill.ca> writes:

> Hi,
> 
>     I am trying to write a function to automate multiple graph
> generation. My data looks like:
> 
> Table of numeric values with the following headers:
> 
> 
>       timeM1  statusM1 xM1 timeM2  statusM2 xM2 timeM3  statusM3 xM3
> 1
> 2
> 3
> 4
> 5
> 6
> 
> Where M1,M2, M3 hve no similarity except they have a max string length
> of 7. Examples are mcw0045, adl0003, lei0101.
> 
> Now, what I want to do is
> 
> Function(M1, M2, M3, datafile)
> {
>     xnew <- readtable(datafile)
>     name1 <- paste("time",M1",sep"")
>     name2 <- paste(status",M1,sep"")
>     name3 <- paste("x",M1,sep"")
>     fit1 <- survfit(Surv(name1,name2)~name3,data=xnew)
...
> > fit <- survfit(Surv(name1,status)~x,data=xnew)
> Error in Surv(name1, status) : Time variable is not numeric
> 
> I think it is no doing what I want it to do. How to I tell the function
> Surv to pass the value of name2 as a string?

You don't. You convert name1...name3 to *symbols* that can be used
in the model formula and plug them into the correct spot:

name1 <- as.name(paste("time",M1",sep""))
...etc...
fm <- substitute(Surv(name1,name2)~name3)
fit1 <- survfit(fm, data=xnew)

(this only works inside functions, and only in R (not S-PLUS) because
of substitute semantics,

substitute(Surv(name1,name2)~name3, 
	list(name1=name1, name2=name2, name3=name3))

works portably ... I think ....)
-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list