title: R gtsummary Row with Categorical Variable Totals
body: <p>I have a dataset of approximately 700,000 patients where I have hospital site IDs (factor variable). I would like to create a row where the number of hospitals is visible (this is separate to the number of patients). I have 3 categorical variables as my columns in addition to an overall column.</p>
  <p>At the moment, there is a separate row for each hospital id with the number of patients in each site for each category.</p>
  <p>My code is as follows:</p>
  <pre><code>t1 &lt;- PIR %&gt;% 
    select(siteidn, countryname) %&gt;% 
      tbl_summary(by = countryname ,missing = &quot;no&quot;,
                  label = list(
                   siteidn = &quot;Number of ICUs&quot;),
              statistic = list(
                all_continuous() ~ &quot;{mean} ({sd})&quot;,
                all_categorical() ~ &quot;{n} ({p}%)&quot;)) %&gt;%
    bold_labels() %&gt;% 
    italicize_levels() %&gt;% 
    add_overall()
  
  t2 &lt;- PIR %&gt;% 
    select(siteidn, hospt) %&gt;% 
      tbl_summary(by = hospt ,missing = &quot;no&quot;,
                  label = list(
                   siteidn = &quot;Number of ICUs&quot;),
              statistic = list(
                all_continuous() ~ &quot;{mean} ({sd})&quot;,
                all_categorical() ~ &quot;{n} ({p}%)&quot;)) %&gt;% 
        bold_labels() %&gt;% 
        italicize_levels()
  
  t3 &lt;- PIR %&gt;% 
    select(siteidn, iculevelname) %&gt;% 
      tbl_summary(by = iculevelname ,missing = &quot;no&quot;,
                  label = list(
                   siteidn = &quot;Number of ICUs&quot;),
              statistic = list(
                all_continuous() ~ &quot;{mean} ({sd})&quot;,
                all_categorical() ~ &quot;{n} ({p}%)&quot;)) %&gt;% 
        bold_labels() %&gt;% 
        italicize_levels()
  
  tbl_merge(
    tbls = list(t1, t2, t3),
    tab_spanner = c(&quot;**Country**&quot;, &quot;**Hospital Type**&quot;, &quot;**ICU Level**&quot;))
  </code></pre>
  <p>This produces the following table:</p>
  <p><a href="https://i.stack.imgur.com/k1ztj.png" rel="nofollow noreferrer">Table 1</a></p>
  <p>As can be seen, there is a separate row for each hospital ID. I'd like to have a single row where there are totals of the number of hospitals in each tier (i.e. total number of hospitals in Aus, NZ, Metropolitan, etc).</p>
  <p>My questions are:</p>
  <ol>
  <li>Is there a way to get a total row for a factor variable that is not the patient number?</li>
  <li>Is it possible to have an overall column inserted after merging the tables (so that the overall column does not come under the Country heading)?</li>
  <li>Is there a way to create a row for the number of patients and not have those details in the headings?</li>
  </ol>
  <p>Thanks all for your time.</p>
  <p>Ben</p>
  <p>ADDIT: Here is an image of what I would like the table to look like. I apologise for it's crudeness. I would like to have just one row for the factor variable of total Number of ICUs, rather than have a row of each ICU with the number of patients in it (Red Ink).</p>
  <p>Additionally, is there a way to group the 2 rows under a common heading similar to the factor variables (Green Ink).</p>
  <p>I appreciate that my R skills are rudementary. Thank you all for your patience!</p>
  <p>Ben</p>
  <p><a href="https://i.stack.imgur.com/jgwnx.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jgwnx.jpg" alt="Table_Ideal" /></a></p>