title: most efficient way to export contingency table from R to excel
body: <p>I am creating some contingency tables/crosstabs in R that I want to move to Excel for use in a Word document. I've found discussions on how to write to excel in a few questions- <a href="https://stackoverflow.com/questions/37844276/how-to-export-multivariate-forecast-results-from-r-to-excel">How to export multivariate forecast results from R to excel</a>
  <a href="https://stackoverflow.com/questions/19414605/export-data-from-r-to-excel">Export data from R to excel</a>. But contingency tables' formats (either using base R or packages like descr or gmodels) don't seem to translate well to Excel. </p>
  
  <p>I've come up with my own solution, which produces what I want, but I wonder if there's a more efficient way, e.g. a package I'm overlooking.  </p>
  
  <pre><code>library(xlsx)
  test.data &lt;- as.data.frame(matrix(sample.int(2,size = 10*2, TRUE), nrow = 10, ncol = 2))
  out1 &lt;- table(test.data$V1, test.data$V2)
  out.p &lt;- prop.table(out1,2)
  out.d &lt;- as.data.frame.matrix(out1)
  out.pd &lt;- round(as.data.frame.matrix(out.p),2)
  colnames(out.d) &lt;- c("No", "Yes")
  colnames(out.pd) &lt;- c("No", "Yes")
  out.d1 &lt;- paste(out.d$No, " (",out.pd$No,")",sep="")
  out.d2 &lt;- paste(out.d$Yes," (",out.pd$Yes,")", sep="")
  out2 &lt;- cbind(out.d1,out.d2)
  toadd.r &lt;- c("No","Yes")
  out2 &lt;- cbind(toadd.r,out2)
  sig &lt;- unlist(summary(table(test.data$V1,test.data$V2)))
  sig &lt;- rep(round(sig[6],2),nrow(out2))
  out2 &lt;- cbind(out2, sig)
  colnames(out2) &lt;- c("Outcome","No","Yes","sig")
  write.xlsx(out2, file ="Output.xlsx", sheetName = "Test1")
  </code></pre>