<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/baseline-characteristics-tables-with-qwraps2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.peteredewitt.com/dewittpe.github.io//dewittpe.github.io//2016/summary-table/"> Peter E. DeWitt</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Almost every biomedical research paper requires a “Table 1: baseline patient<br>
characteristics.” Many developers have published tools to help streamline the<br>
construction of such tables. The <code>qwraps2::summary_table</code> function is my<br>
contribution to the toolbox.</p>
<p>I have constructed hundreds of Table 1s while working as a biostatistics<br>
consultant in a biostatistics department. I’ve also constructed many helper<br>
functions to try and streamline the production of such tables. However, every<br>
project, every data set, every lead author, every target journal, etc., will<br>
present slightly different requirements for the contents and formating of the<br>
tables. As such, functions which tried to “do it all”, required<br>
constant modification to provide the needed output from each nuanced project.</p>
<p>The <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=tableone"><code>tableone</code></a> package does a lot<br>
of good things, and is a great tool for quickly building the baseline summary<br>
tables. What my experiences has taught me is that each row group, or even each<br>
row, might require some specific formating. A function that treats all<br>
continuous variables one way and all categorical variables another way, may work<br>
for many cases but not all.</p>
<p>The approach to building the tables I’ve taken now is explicitly define the<br>
summary statistics I want for each variable in the data set, the formatting<br>
for the summary statistics, and in a way that is easy to work with one or more<br>
grouping variables.</p>
<p>The function <code>summary_table</code> within my<br>
<a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=qwraps2"><code>qwraps2</code></a> package is the tool I<br>
and a few colleagues have started to rely on for building baseline patient<br>
characteristic tables. (qwraps2, “quick wraps 2”, is a package of formatting<br>
functions I’ve found useful for formating results and generating some graphics<br>
when authoring .Rmd and .Rnw files.)</p>
<p>Load and attach the qwraps2 namespace. We’ll set the <code>qwraps2_markup</code> option to<br>
<code>markdown</code>. If this option is not set, <code>qwraps2</code> uses<br>
<code>get0ption(qwraps2_markup, &quot;latex&quot;)</code> as the default markup language.</p>
<div>
<pre><code><span>library</span><span>(</span><span>qwraps2</span><span>)</span><span>
</span><span>options</span><span>(</span><span>qwraps2_markup</span><span> </span><span>=</span><span> </span><span>'markdown'</span><span>)</span><span> </span><span># default is latex
</span></code></pre>
</div>
<p></p>
<p>We’ll use the <code>mtcars</code> dataset for our examples. Let’s report several summary<br>
statistics for miles per gallon, number of cylinders, and weight of the<br>
vehicles. The following summary is provided to illustrate the functions and<br>
thus will include some summaries that would not be used in a publication.<br>
The data summary we want will be:</p>
<ul>
<li>Miles Per Gallon
<ul>
<li>min</li>
<li>mean (sd)</li>
<li>median (iqr)</li>
<li>max</li>
</ul>
</li>
<li>Cylinders
<ul>
<li>mean</li>
<li>n (%) of four cylinders engines</li>
<li>n (%) of six cylinders engines</li>
<li>n (%) of eight cylinders engines</li>
</ul>
</li>
<li>Weight
<ul>
<li>range</li>
</ul>
</li>
</ul>
<p>For cylinders we’ll report several things, the mean number of cylinders, and the<br>
count (%) of 4, 6, and 8 cylinders cars. In a publication we would likely not<br>
report such a summary, treating cylinders as both a continuous and categorical<br>
value. However, doing so here helps to illustrate the flexibility of the<br>
<code>summary_table</code> method.</p>
<p>Outlining the wanted summary statistics above as a list-of-lists helps to<br>
explain the construction of the summary object constructed below. The<br>
<code>summary_table</code> method takes two arguments,</p>
<ol>
<li><code>.data</code>, a <code>data.frame</code> or a <code>grouped_df</code> object, and</li>
<li><code>summaries</code> a list-of-lists of right hand sided <code>formula</code>e defining the<br>
summary statistics.</li>
</ol>
<p>The construction of the summary table is achieved via <code>dplyr::summarize_</code>.</p>
<p>The <code>mtcar_summaries</code> object constructed below, defines each needed row of the<br>
summary table via a <code>formula</code>. I’ve included the <code>qwraps2</code> namespace for<br>
clarity.</p>
<div>
<pre><code><span>mtcar_summaries</span><span> </span><span>&lt;-</span><span>
</span><span>list</span><span>(</span><span>&quot;Miles Per Gallon&quot;</span><span> </span><span>=</span><span>
</span><span>list</span><span>(</span><span>&quot;min:&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>min</span><span>(</span><span>mpg</span><span>),</span><span>
</span><span>&quot;mean (sd)&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>qwraps2</span><span>::</span><span>mean_sd</span><span>(</span><span>mpg</span><span>,</span><span> </span><span>denote_sd</span><span> </span><span>=</span><span> </span><span>&quot;paren&quot;</span><span>),</span><span>
</span><span>&quot;median (IQR)&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>qwraps2</span><span>::</span><span>median_iqr</span><span>(</span><span>mpg</span><span>),</span><span>
</span><span>&quot;max:&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>max</span><span>(</span><span>mpg</span><span>)),</span><span>
</span><span>&quot;Cylinders:&quot;</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>&quot;mean&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>mean</span><span>(</span><span>cyl</span><span>),</span><span>
</span><span>&quot;mean (formatted)&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>qwraps2</span><span>::</span><span>frmt</span><span>(</span><span>mean</span><span>(</span><span>cyl</span><span>)),</span><span>
</span><span>&quot;4 cyl, n (%)&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>qwraps2</span><span>::</span><span>n_perc0</span><span>(</span><span>cyl</span><span> </span><span>==</span><span> </span><span>4</span><span>),</span><span>
</span><span>&quot;6 cyl, n (%)&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>qwraps2</span><span>::</span><span>n_perc0</span><span>(</span><span>cyl</span><span> </span><span>==</span><span> </span><span>6</span><span>),</span><span>
</span><span>&quot;8 cyl, n (%)&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>qwraps2</span><span>::</span><span>n_perc0</span><span>(</span><span>cyl</span><span> </span><span>==</span><span> </span><span>8</span><span>)),</span><span>
</span><span>&quot;Weight&quot;</span><span> </span><span>=</span><span>
</span><span>list</span><span>(</span><span>&quot;Range&quot;</span><span> </span><span>=</span><span> </span><span>~</span><span> </span><span>paste</span><span>(</span><span>range</span><span>(</span><span>wt</span><span>),</span><span> </span><span>collapse</span><span> </span><span>=</span><span> </span><span>&quot;, &quot;</span><span>))</span><span>
</span><span>)</span><span>
</span></code></pre>
</div>
<p></p>
<p>The table is constructed and printed with ease:</p>
<div>
<pre><code><span>summary_table</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>mtcar_summaries</span><span>)</span><span>
</span><span>## ## ## | |mtcars (N = 32) |
## |:-----------------------------|:--------------------|
## |**Miles Per Gallon** |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; min: |10.4 |
## |&amp;nbsp;&amp;nbsp; mean (sd) |20.09 (6.03) |
## |&amp;nbsp;&amp;nbsp; median (IQR) |19.20 (15.43, 22.80) |
## |&amp;nbsp;&amp;nbsp; max: |33.9 |
## |**Cylinders:** |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; mean |6.1875 |
## |&amp;nbsp;&amp;nbsp; mean (formatted) |6.19 |
## |&amp;nbsp;&amp;nbsp; 4 cyl, n (%) |11 (34) |
## |&amp;nbsp;&amp;nbsp; 6 cyl, n (%) |7 (22) |
## |&amp;nbsp;&amp;nbsp; 8 cyl, n (%) |14 (44) |
## |**Weight** |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; Range |1.513, 5.424 |
</span></code></pre>
</div>
<p>The markdown output, rendered as html is:</p>
<table>
<thead>
<tr>
<th> </th>
<th>mtcars (N = 32)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Miles Per Gallon</strong></td>
<td>  </td>
</tr>
<tr>
<td>   min:</td>
<td>10.4</td>
</tr>
<tr>
<td>   mean (sd)</td>
<td>20.09 (6.03)</td>
</tr>
<tr>
<td>   median (IQR)</td>
<td>19.20 (15.43, 22.80)</td>
</tr>
<tr>
<td>   max:</td>
<td>33.9</td>
</tr>
<tr>
<td><strong>Cylinders:</strong></td>
<td>  </td>
</tr>
<tr>
<td>   mean</td>
<td>6.1875</td>
</tr>
<tr>
<td>   mean (formatted)</td>
<td>6.19</td>
</tr>
<tr>
<td>   4 cyl, n (%)</td>
<td>11 (34)</td>
</tr>
<tr>
<td>   6 cyl, n (%)</td>
<td>7 (22)</td>
</tr>
<tr>
<td>   8 cyl, n (%)</td>
<td>14 (44)</td>
</tr>
<tr>
<td><strong>Weight</strong></td>
<td>  </td>
</tr>
<tr>
<td>   Range</td>
<td>1.513, 5.424</td>
</tr>
</tbody>
</table>
<p></p>
<p>Extending the table to show the same summary by a grouping variable, we’ll use<br>
<code>am</code> (Transmission: 0 = automatic, 1 = manual), is done as follows:</p>
<div>
<pre><code><span>summary_table</span><span>(</span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>am</span><span>),</span><span> </span><span>mtcar_summaries</span><span>)</span><span>
</span><span>## ## ## | |am: 0 (N = 19) |am: 1 (N = 13) |
## |:-----------------------------|:--------------------|:--------------------|
## |**Miles Per Gallon** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; min: |10.4 |15.0 |
## |&amp;nbsp;&amp;nbsp; mean (sd) |17.15 (3.83) |24.39 (6.17) |
## |&amp;nbsp;&amp;nbsp; median (IQR) |17.30 (14.95, 19.20) |22.80 (21.00, 30.40) |
## |&amp;nbsp;&amp;nbsp; max: |24.4 |33.9 |
## |**Cylinders:** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; mean |6.947368 |5.076923 |
## |&amp;nbsp;&amp;nbsp; mean (formatted) |6.95 |5.08 |
## |&amp;nbsp;&amp;nbsp; 4 cyl, n (%) |3 (16) |8 (62) |
## |&amp;nbsp;&amp;nbsp; 6 cyl, n (%) |4 (21) |3 (23) |
## |&amp;nbsp;&amp;nbsp; 8 cyl, n (%) |12 (63) |2 (15) |
## |**Weight** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; Weight |2.465, 5.424 |1.513, 3.57 |
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th> </th>
<th>am: 0 (N = 19)</th>
<th>am: 1 (N = 13)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Miles Per Gallon</strong></td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   min:</td>
<td>10.4</td>
<td>15.0</td>
</tr>
<tr>
<td>   mean (sd)</td>
<td>17.15 (3.83)</td>
<td>24.39 (6.17)</td>
</tr>
<tr>
<td>   median (IQR)</td>
<td>17.30 (14.95, 19.20)</td>
<td>22.80 (21.00, 30.40)</td>
</tr>
<tr>
<td>   max:</td>
<td>24.4</td>
<td>33.9</td>
</tr>
<tr>
<td><strong>Cylinders:</strong></td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   mean</td>
<td>6.947368</td>
<td>5.076923</td>
</tr>
<tr>
<td>   mean (formatted)</td>
<td>6.95</td>
<td>5.08</td>
</tr>
<tr>
<td>   4 cyl, n (%)</td>
<td>3 (16)</td>
<td>8 (62)</td>
</tr>
<tr>
<td>   6 cyl, n (%)</td>
<td>4 (21)</td>
<td>3 (23)</td>
</tr>
<tr>
<td>   8 cyl, n (%)</td>
<td>12 (63)</td>
<td>2 (15)</td>
</tr>
<tr>
<td><strong>Weight</strong></td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   Weight</td>
<td>2.465, 5.424</td>
<td>1.513, 3.57</td>
</tr>
</tbody>
</table>
<p></p>
<p>And lastly, building one table with a column for the whole data set and columns<br>
for each transmission type is:</p>
<div>
<pre><code><span>cbind</span><span>(</span><span>summary_table</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>mtcar_summaries</span><span>),</span><span>
</span><span>summary_table</span><span>(</span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>am</span><span>),</span><span> </span><span>mtcar_summaries</span><span>))</span><span>
</span><span>## ## ## | |mtcars (N = 32) |am: 0 (N = 19) |am: 1 (N = 13) |
## |:-----------------------------|:--------------------|:--------------------|:--------------------|
## |**Miles Per Gallon** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; min: |10.4 |10.4 |15.0 |
## |&amp;nbsp;&amp;nbsp; mean (sd) |20.09 (6.03) |17.15 (3.83) |24.39 (6.17) |
## |&amp;nbsp;&amp;nbsp; median (IQR) |19.20 (15.43, 22.80) |17.30 (14.95, 19.20) |22.80 (21.00, 30.40) |
## |&amp;nbsp;&amp;nbsp; max: |33.9 |24.4 |33.9 |
## |**Cylinders:** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; mean |6.1875 |6.947368 |5.076923 |
## |&amp;nbsp;&amp;nbsp; mean (formatted) |6.19 |6.95 |5.08 |
## |&amp;nbsp;&amp;nbsp; 4 cyl, n (%) |11 (34) |3 (16) |8 (62) |
## |&amp;nbsp;&amp;nbsp; 6 cyl, n (%) |7 (22) |4 (21) |3 (23) |
## |&amp;nbsp;&amp;nbsp; 8 cyl, n (%) |14 (44) |12 (63) |2 (15) |
## |**Weight** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; Range |1.513, 5.424 |2.465, 5.424 |1.513, 3.57 |
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th> </th>
<th>mtcars (N = 32)</th>
<th>am: 0 (N = 19)</th>
<th>am: 1 (N = 13)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Miles Per Gallon</strong></td>
<td>  </td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   min:</td>
<td>10.4</td>
<td>10.4</td>
<td>15.0</td>
</tr>
<tr>
<td>   mean (sd)</td>
<td>20.09 (6.03)</td>
<td>17.15 (3.83)</td>
<td>24.39 (6.17)</td>
</tr>
<tr>
<td>   median (IQR)</td>
<td>19.20 (15.43, 22.80)</td>
<td>17.30 (14.95, 19.20)</td>
<td>22.80 (21.00, 30.40)</td>
</tr>
<tr>
<td>   max:</td>
<td>33.9</td>
<td>24.4</td>
<td>33.9</td>
</tr>
<tr>
<td><strong>Cylinders:</strong></td>
<td>  </td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   mean</td>
<td>6.1875</td>
<td>6.947368</td>
<td>5.076923</td>
</tr>
<tr>
<td>   mean (formatted)</td>
<td>6.19</td>
<td>6.95</td>
<td>5.08</td>
</tr>
<tr>
<td>   4 cyl, n (%)</td>
<td>11 (34)</td>
<td>3 (16)</td>
<td>8 (62)</td>
</tr>
<tr>
<td>   6 cyl, n (%)</td>
<td>7 (22)</td>
<td>4 (21)</td>
<td>3 (23)</td>
</tr>
<tr>
<td>   8 cyl, n (%)</td>
<td>14 (44)</td>
<td>12 (63)</td>
<td>2 (15)</td>
</tr>
<tr>
<td><strong>Weight</strong></td>
<td>  </td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   Range</td>
<td>1.513, 5.424</td>
<td>2.465, 5.424</td>
<td>1.513, 3.57</td>
</tr>
</tbody>
</table>
<p></p>
<p>Using <code>dplry::group_by</code> will allow you to build the table with more than one<br>
grouping variable. For example:</p>
<div>
<pre><code><span>cbind</span><span>(</span><span>summary_table</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>mtcar_summaries</span><span>),</span><span>
</span><span>summary_table</span><span>(</span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>am</span><span>,</span><span> </span><span>vs</span><span>),</span><span> </span><span>mtcar_summaries</span><span>))</span><span>
</span><span>## ## ## | |mtcars (N = 32) |am: 0 vs: 0 (N = 12) |am: 0 vs: 1 (N = 7) |am: 1 vs: 0 (N = 6) |am: 1 vs: 1 (N = 7) |
## |:-----------------------------|:--------------------|:--------------------|:--------------------|:--------------------|:--------------------|
## |**Miles Per Gallon** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; min: |10.4 |10.4 |17.8 |15.0 |21.4 |
## |&amp;nbsp;&amp;nbsp; mean (sd) |20.09 (6.03) |15.05 (2.77) |20.74 (2.47) |19.75 (4.01) |28.37 (4.76) |
## |&amp;nbsp;&amp;nbsp; median (IQR) |19.20 (15.43, 22.80) |15.20 (14.05, 16.62) |21.40 (18.65, 22.15) |20.35 (16.78, 21.00) |30.40 (25.05, 31.40) |
## |&amp;nbsp;&amp;nbsp; max: |33.9 |19.2 |24.4 |26.0 |33.9 |
## |**Cylinders:** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; mean |6.1875 |8.000000 |5.142857 |6.333333 |4.000000 |
## |&amp;nbsp;&amp;nbsp; mean (formatted) |6.19 |8.00 |5.14 |6.33 |4.00 |
## |&amp;nbsp;&amp;nbsp; 4 cyl, n (%) |11 (34) |0 (0) |3 (43) |1 (17) |7 (100) |
## |&amp;nbsp;&amp;nbsp; 6 cyl, n (%) |7 (22) |0 (0) |4 (57) |3 (50) |0 (0) |
## |&amp;nbsp;&amp;nbsp; 8 cyl, n (%) |14 (44) |12 (100) |0 (0) |2 (33) |0 (0) |
## |**Weight** |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; Range |1.513, 5.424 |3.435, 5.424 |2.465, 3.46 |2.14, 3.57 |1.513, 2.78 |
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th> </th>
<th>mtcars (N = 32)</th>
<th>am: 0 vs: 0 (N = 12)</th>
<th>am: 0 vs: 1 (N = 7)</th>
<th>am: 1 vs: 0 (N = 6)</th>
<th>am: 1 vs: 1 (N = 7)</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Miles Per Gallon</strong></td>
<td>  </td>
<td>  </td>
<td>  </td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   min:</td>
<td>10.4</td>
<td>10.4</td>
<td>17.8</td>
<td>15.0</td>
<td>21.4</td>
</tr>
<tr>
<td>   mean (sd)</td>
<td>20.09 (6.03)</td>
<td>15.05 (2.77)</td>
<td>20.74 (2.47)</td>
<td>19.75 (4.01)</td>
<td>28.37 (4.76)</td>
</tr>
<tr>
<td>   median (IQR)</td>
<td>19.20 (15.43, 22.80)</td>
<td>15.20 (14.05, 16.62)</td>
<td>21.40 (18.65, 22.15)</td>
<td>20.35 (16.78, 21.00)</td>
<td>30.40 (25.05, 31.40)</td>
</tr>
<tr>
<td>   max:</td>
<td>33.9</td>
<td>19.2</td>
<td>24.4</td>
<td>26.0</td>
<td>33.9</td>
</tr>
<tr>
<td><strong>Cylinders:</strong></td>
<td>  </td>
<td>  </td>
<td>  </td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   mean</td>
<td>6.1875</td>
<td>8.000000</td>
<td>5.142857</td>
<td>6.333333</td>
<td>4.000000</td>
</tr>
<tr>
<td>   mean (formatted)</td>
<td>6.19</td>
<td>8.00</td>
<td>5.14</td>
<td>6.33</td>
<td>4.00</td>
</tr>
<tr>
<td>   4 cyl, n (%)</td>
<td>11 (34)</td>
<td>0 (0)</td>
<td>3 (43)</td>
<td>1 (17)</td>
<td>7 (100)</td>
</tr>
<tr>
<td>   6 cyl, n (%)</td>
<td>7 (22)</td>
<td>0 (0)</td>
<td>4 (57)</td>
<td>3 (50)</td>
<td>0 (0)</td>
</tr>
<tr>
<td>   8 cyl, n (%)</td>
<td>14 (44)</td>
<td>12 (100)</td>
<td>0 (0)</td>
<td>2 (33)</td>
<td>0 (0)</td>
</tr>
<tr>
<td><strong>Weight</strong></td>
<td>  </td>
<td>  </td>
<td>  </td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   Range</td>
<td>1.513, 5.424</td>
<td>3.435, 5.424</td>
<td>2.465, 3.46</td>
<td>2.14, 3.57</td>
<td>1.513, 2.78</td>
</tr>
</tbody>
</table>
<p></p>
<p>The formatting of the output is controlled by the<br>
<code>qwraps2:::print.qwraps2_summary_table</code> and <code>qwraps2::qable</code> functions.</p>
<div>
<pre><code><span>args</span><span>(</span><span>qwraps2</span><span>:::</span><span>print.qwraps2_summary_table</span><span>)</span><span>
</span><span>## function (x, rgroup = attr(x, &quot;rgroups&quot;), rnames = rownames(x), ## cnames = colnames(x), ...) ## NULL
</span><span>args</span><span>(</span><span>qwraps2</span><span>::</span><span>qable</span><span>)</span><span>
</span><span>## function (x, rtitle, rgroup, rnames = rownames(x), cnames = colnames(x), ## markup = getOption(&quot;qwraps2_markup&quot;, &quot;latex&quot;), ...) ## NULL
</span></code></pre>
</div>
<p>The <code>print</code> method for <code>qwraps2_summary_table</code> objects calls <code>qable</code> which is a<br>
wrapper around <code>knitr::kable</code>. The row groups,<code> rgroup</code>, row names <code>rnames</code>,<br>
and column names, <code>cnames</code>, are explicitly set in the<br>
<code>print.qwraps2_summary_table</code> method. The <code>...</code> passes additional arguments to<br>
<code>qwraps2::qable</code> which can then continue to pass to <code>knitr::kable</code>.</p>
<p>A quick example of modifying a table:</p>
<div>
<pre><code><span>by_am</span><span> </span><span>&lt;-</span><span> </span><span>summary_table</span><span>(</span><span>dplyr</span><span>::</span><span>group_by</span><span>(</span><span>mtcars</span><span>,</span><span> </span><span>am</span><span>),</span><span> </span><span>mtcar_summaries</span><span>)</span><span>
</span><span>print</span><span>(</span><span>by_am</span><span>,</span><span> </span><span>cnames</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;_Manual_&quot;</span><span>,</span><span> </span><span>&quot;_Automatic_&quot;</span><span>),</span><span>
</span><span>rtitle</span><span> </span><span>=</span><span> </span><span>&quot;Vehicle Characteristics&quot;</span><span>,</span><span> </span><span>align</span><span> </span><span>=</span><span> </span><span>&quot;lcc&quot;</span><span>)</span><span>
</span><span>## ## ## |Vehicle Characteristics | _Manual_ | _Automatic_ |
## |:-----------------------------|:--------------------:|:--------------------:|
## |**Miles Per Gallon** | &amp;nbsp;&amp;nbsp; | &amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; min: | 10.4 | 15.0 |
## |&amp;nbsp;&amp;nbsp; mean (sd) | 17.15 (3.83) | 24.39 (6.17) |
## |&amp;nbsp;&amp;nbsp; median (IQR) | 17.30 (14.95, 19.20) | 22.80 (21.00, 30.40) |
## |&amp;nbsp;&amp;nbsp; max: | 24.4 | 33.9 |
## |**Cylinders:** | &amp;nbsp;&amp;nbsp; | &amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; mean | 6.947368 | 5.076923 |
## |&amp;nbsp;&amp;nbsp; mean (formatted) | 6.95 | 5.08 |
## |&amp;nbsp;&amp;nbsp; 4 cyl, n (%) | 3 (16) | 8 (62) |
## |&amp;nbsp;&amp;nbsp; 6 cyl, n (%) | 4 (21) | 3 (23) |
## |&amp;nbsp;&amp;nbsp; 8 cyl, n (%) | 12 (63) | 2 (15) |
## |**Weight** | &amp;nbsp;&amp;nbsp; | &amp;nbsp;&amp;nbsp; |
## |&amp;nbsp;&amp;nbsp; Weight | 2.465, 5.424 | 1.513, 3.57 |
</span></code></pre>
</div>
<table>
<thead>
<tr>
<th>Vehicle Characteristics</th>
<th><em>Manual</em></th>
<th><em>Automatic</em></th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Miles Per Gallon</strong></td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   min:</td>
<td>10.4</td>
<td>15.0</td>
</tr>
<tr>
<td>   mean (sd)</td>
<td>17.15 (3.83)</td>
<td>24.39 (6.17)</td>
</tr>
<tr>
<td>   median (IQR)</td>
<td>17.30 (14.95, 19.20)</td>
<td>22.80 (21.00, 30.40)</td>
</tr>
<tr>
<td>   max:</td>
<td>24.4</td>
<td>33.9</td>
</tr>
<tr>
<td><strong>Cylinders:</strong></td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   mean</td>
<td>6.947368</td>
<td>5.076923</td>
</tr>
<tr>
<td>   mean (formatted)</td>
<td>6.95</td>
<td>5.08</td>
</tr>
<tr>
<td>   4 cyl, n (%)</td>
<td>3 (16)</td>
<td>8 (62)</td>
</tr>
<tr>
<td>   6 cyl, n (%)</td>
<td>4 (21)</td>
<td>3 (23)</td>
</tr>
<tr>
<td>   8 cyl, n (%)</td>
<td>12 (63)</td>
<td>2 (15)</td>
</tr>
<tr>
<td><strong>Weight</strong></td>
<td>  </td>
<td>  </td>
</tr>
<tr>
<td>   Weight</td>
<td>2.465, 5.424</td>
<td>1.513, 3.57</td>
</tr>
</tbody>
</table>
<p></p>
<p>I hope that some readers will find this approach to building summary tables to<br>
be useful. If you find bugs or have suggestions on how to extend and improve<br>
this tool please create an<br>
<a target="_blank" rel="nofollow" href="https://github.com/dewittpe/qwraps2/issues">issue on github</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.peteredewitt.com/dewittpe.github.io//dewittpe.github.io//2016/summary-table/"> Peter E. DeWitt</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/2IJVQ1Ccik8"></a></div>