title: Using survfit object's formula in survdiff call
body: <p>I'm doing some survival analysis in R, and looking to tidy up/simplify my code.</p>
  
  <p>At the moment I'm doing several steps in my data analysis: </p>
  
  <ol>
  <li>make a Surv object (time variable with indication as to whether each observation was censored); </li>
  <li>fit this Surv object according to a categorical predictor, for plotting/estimation of median survival time processes; and</li>
  <li>calculate a log-rank test to ask whether there is evidence of "significant" differences in survival between the groups.</li>
  </ol>
  
  <p>As an example, here is a mock-up using the lung dataset in the survival package from R. So the following code is similar enough to what I want to do, but much simplified in terms of the predictor set (which is why I want to simplify the code, so I don't make inconsistent calls across models).</p>
  
  <pre><code>library(survival)
  
  # Step 1: Make a survival object with time-to-event and censoring indicator.
  # Following works with defaults as status = 2 = dead in this dataset.
  # Create survival object
  lung.Surv &lt;- with(lung, Surv(time=time, event=status))
  
  # Step 2: Fit survival curves to object based on patient sex, plot this.
  lung.survfit &lt;- survfit(lung.Surv ~ lung$sex)
  print(lung.survfit)
  plot(lung.survfit)
  
  # Step 3: Calculate log-rank test for difference in survival objects
  lung.survdiff &lt;- survdiff(lung.Surv ~ lung$sex)
  print(lung.survdiff)
  </code></pre>
  
  <p>Now this is all fine and dandy, and I can live with this but would like to do better. </p>
  
  <p>So my question is around step 3. What I would like to do here is to be able to use information in the formula from the lung.survfit object to feed into the calculation of the differences in survival curves: i.e. in the call to survdiff. And this is where my domitable [sic] programming skills hit a wall. Below is my current attempt to do this: I'd appreciate any help that you can give! Once I can get this sorted out I should be able to wrap a solution up in a function.</p>
  
  <pre><code>lung.survdiff &lt;- survdiff(parse(text=(lung.survfit$call$formula)))
  ## Which returns following:
  # Error in survdiff(parse(text = (lung.survfit$call$formula))) : 
  # The 'formula' argument is not a formula
  </code></pre>