title: R time at risk for each group
body: <p>I have been preparing survival analysis and cox regression in R. However, my line manager is a Stata user and wants the output displayed in a similar way as Stata would display it, e.g.</p>
  
  <pre><code># Stata code
  . strate
  . stsum, by (GROUP)
  </code></pre>
  
  <p><code>stsum</code> will output <strong>a time at risk for each group and an incidence rate</strong>, and I can't figure out how to achieve this with R.</p>
  
  <hr>
  
  <p>The data look roughly like this (I can't get to it as it's in a secure environment):</p>
  
  <pre><code>PERS GROUP INJURY FOLLOWUP
  111  1     0      2190
  222  2     1      45
  333  1     1      560
  444  2     0      1200
  </code></pre>
  
  <p>So far I have been using fairly bog standard code:</p>
  
  <pre><code>library(survival)
  library(coin)
  # survival analysis
  table(data$INJURY, data$GROUP)
  survdiff(Surv(FOLLOWUP, INJURY)~GROUP, data=data)
  surv_test(Surv(FOLLOWUP, INJURY)~factor(GROUP), data=data)
  surv.all &lt;- survfit(Surv(FOLLOWUP, INJURY)~GROUP, data=data)
  print(sur.all, print.rmean=TRUE)
  # cox regression
  cox.all&lt;- coxph(Surv(FOLLOWUP, INJURY)~GROUP, data=data))
  summary(cox.all)
  </code></pre>