title: Print a data frame with columns aligned (as displayed in R)
body: <p>I have the following data frame in R:</p>
  
  <pre><code>&gt; dframe
                  Mean Median
  Candidates     85.68     60
  NonCands        9.21      4
  Multi          27.48     17
  Mono            4.43      3
  Multi NonCands 22.23     15
  </code></pre>
  
  <p>I want to print it into a file and keep it nicely formatted and aligned just as shown above. I use:</p>
  
  <pre><code>write.table(dframe,file="test",sep="\t", quote=F)
  </code></pre>
  
  <p>which produces the following output:</p>
  
  <pre><code>Mean    Median
  Candidates  85.68   60
  NonCands    9.21    4
  Multi   27.48   17
  Mono    4.43    3
  Multi NonCands  22.23   15
  </code></pre>
  
  <p>Since the data is displayed properly formatted within the R environment I thought it should be trivial to write it to a file with the same format. Apparently I was wrong. I have tried playing with <code>format()</code> and <code>write.matrix</code> from the MASS library but neither produces the desired result. </p>
  
  <p>I have seen some suggestions like <a href="https://stackoverflow.com/a/10588266/1081936">this one</a>, but it seems both too complicated and, more importantly, does not produce the desired result when printing to a file with <code>write.table()</code>.</p>
  
  <p>So, how can I print my data frame to a text file and have it look just as it does within R?</p>
  
  <hr>
  
  <p>UPDATE</p>
  
  <p>Following Justin's suggestion from his comment below, I installed the gdata library and used <code>write.fwf</code>. This is <em>almost</em> what I need:</p>
  
  <pre><code>write.fwf(dframe,file="test",sep="\t", quote=F, rownames=T)
  </code></pre>
  
  <p>produces the following output:</p>
  
  <pre><code>Mean    Median
  Candidates      85.68   60
  NonCands         9.21    4
  Multi           27.48   17
  Mono             4.43    3
  Multi NonCands  22.23   15
  </code></pre>
  
  <p>So, any ideas on how to get "Mean" and "Median" shifted to the right so they align with their respective columns?</p>
  
  <p>Since it may now be relevant, here is how the data.frame was created:</p>
  
  <pre><code>labels&lt;-c("Candidates","NonCands","Multi", "Mono", "Multi NonCands")
  Mean &lt;- c(mean(cands), mean(non),mean(multi),mean(mono),mean(multi_non))
  Median &lt;- c(median(cands), median(non),median(multi),median(mono),median(multi_non))
  names(Mean)&lt;-labels
  dframe&lt;-data.frame(Mean,Median)
  </code></pre>