<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Rverse Analytics Blog</title>
<link>https://rverseanalytics.com/blog.html</link>
<atom:link href="https://rverseanalytics.com/blog.xml" rel="self" type="application/rss+xml"/>
<description>Notes on R, statistics and reproducible workflows.</description>
<image>
<url>https://rverseanalytics.com/assets/og-image.png</url>
<title>Rverse Analytics Blog</title>
<link>https://rverseanalytics.com/blog.html</link>
<height>76</height>
<width>144</width>
</image>
<generator>quarto-1.9.38</generator>
<lastBuildDate>Mon, 12 Oct 2026 21:00:00 GMT</lastBuildDate>
<item>
  <title>How to Install R and RStudio (2026): Step-by-Step with Screenshots</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/how-to-install-r-and-rstudio.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li><strong>Two installs, in this order:</strong> R first (the language, from CRAN), then RStudio (the editor, from Posit). Both are free and take about ten minutes together.</li>
<li><strong>You only ever type into RStudio.</strong> R itself has no interface you need to open; RStudio finds it automatically.</li>
<li><strong>Your first session</strong> fits in five commands: arithmetic, a variable, a vector, <code>summary()</code> on a built-in dataset and <code>hist()</code> for a plot.</li>
<li><strong>Packages</strong> are add-ons installed once with <code>install.packages()</code> and loaded in every session with <code>library()</code>. The “there is no package called …” error simply means the install step was skipped.</li>
<li>Done installing? Continue with the free <a href="../courses/r-foundations.html">R Foundations course</a> or the <a href="../learn.html">interactive Learn R lessons</a> that run in your browser.</li>
</ul>
</div>
<p>R is the language. RStudio is the program you write it in. New users often install only one of the two, or install them in the wrong order, and then see a blank window or a “R not found” message. This guide installs both, in the right order, and ends with a short session so you can confirm everything works.</p>
<p>The screenshots come from a fresh session of R 4.6.1 and RStudio 2026.09. Button labels on your machine may differ slightly by version, but the steps have been stable for years.</p>
<section id="step-1-download-r-from-cran" class="level2">
<h2 class="anchored" data-anchor-id="step-1-download-r-from-cran">Step 1: Download R from CRAN</h2>
<p>R is distributed by CRAN, the Comprehensive R Archive Network. Open <a href="https://cran.r-project.org/">cran.r-project.org</a> and pick your operating system from the first box.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/cran-home.png" class="img-fluid figure-img" alt="CRAN home page with the Download and Install R box showing links for Linux, macOS and Windows"></p>
<figcaption>The CRAN home page. The first box lists the three downloads: Linux, macOS and Windows.</figcaption>
</figure>
</div>
<section id="windows" class="level3">
<h3 class="anchored" data-anchor-id="windows">Windows</h3>
<p>Click <strong>Download R for Windows</strong>, then <strong>base</strong>, then the large <strong>Download R-4.x.x for Windows</strong> link at the top of the page. The file is about 90 MB.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/cran-windows.png" class="img-fluid figure-img" alt="CRAN page for R on Windows with the Download R-4.6.1 for Windows link"></p>
<figcaption>The Windows download page. The link at the top is the only one you need.</figcaption>
</figure>
</div>
<p>Run the downloaded <code>.exe</code>. Accept every default in the installer: destination folder, components (64-bit), start menu folder. There is no need to change anything.</p>
</section>
<section id="macos" class="level3">
<h3 class="anchored" data-anchor-id="macos">macOS</h3>
<p>Click <strong>Download R for macOS</strong>. The page offers two installers: one for <strong>Apple silicon</strong> (M1, M2, M3, M4 chips) and one for <strong>older Intel Macs</strong>. If you are not sure which Mac you have, open the Apple menu and choose <em>About This Mac</em>; the <em>Chip</em> line tells you.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/cran-macos.png" class="img-fluid figure-img" alt="CRAN page for R on macOS listing the arm64 package for Apple silicon and the x86_64 package for Intel Macs"></p>
<figcaption>The macOS download page. Pick the Apple silicon or Intel package to match your Mac.</figcaption>
</figure>
</div>
<p>Open the <code>.pkg</code> file and click through the installer. If macOS refuses to open it because it was downloaded from the internet, right-click the file and choose <em>Open</em>.</p>
</section>
<section id="linux" class="level3">
<h3 class="anchored" data-anchor-id="linux">Linux</h3>
<p>On Ubuntu and Debian the quickest route is the system package manager:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> apt update</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sudo</span> apt install r-base r-base-dev</span></code></pre></div></div>
<p>This gives you the R version in your distribution’s repository, which may be a few releases behind CRAN. For the current release, follow the Ubuntu-specific instructions linked from the CRAN Linux page, which add CRAN’s own repository first.</p>
</section>
</section>
<section id="step-2-download-rstudio-from-posit" class="level2">
<h2 class="anchored" data-anchor-id="step-2-download-rstudio-from-posit">Step 2: Download RStudio from Posit</h2>
<p>RStudio is made by Posit and lives at <a href="https://posit.co/downloads/">posit.co/downloads</a>. Click <strong>Download RStudio</strong> and then the installer for your operating system. The page detects your system and offers the right file first.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/posit-downloads.png" class="img-fluid figure-img" alt="Posit RStudio IDE download page with Download RStudio and Download RStudio Server buttons"></p>
<figcaption>The Posit downloads page. You want RStudio Desktop, the free open-source edition.</figcaption>
</figure>
</div>
<ul>
<li><strong>Windows:</strong> run the <code>.exe</code> and accept the defaults.</li>
<li><strong>macOS:</strong> open the <code>.dmg</code> and drag RStudio into <em>Applications</em>.</li>
<li><strong>Linux:</strong> install the <code>.deb</code> or <code>.rpm</code> with your package manager, for example <code>sudo apt install ./rstudio-*.deb</code>.</li>
</ul>
<p>RStudio looks for R in the standard location, so because you installed R first it will find it without any configuration.</p>
</section>
<section id="step-3-open-rstudio-and-check-that-r-responds" class="level2">
<h2 class="anchored" data-anchor-id="step-3-open-rstudio-and-check-that-r-responds">Step 3: Open RStudio and check that R responds</h2>
<p>Start RStudio. You will see three or four panes. The one that matters today is the <strong>Console</strong> on the left, where you type R commands and see their results immediately.</p>
<p>Type the following line into the console and press Enter after it:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">R.version.string</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/rstudio-ide-overview.png" class="img-fluid figure-img" alt="RStudio IDE with the console showing R.version.string returning R version 4.6.1, Sys.Date() and 1 + 1"></p>
<figcaption>RStudio right after launch. The Console on the left shows the R version, today’s date and a first calculation. The Environment pane on the right is still empty.</figcaption>
</figure>
</div>
<p>If you see a version string like <code>"R version 4.6.1 (2026-06-24)"</code>, both installs worked. The other panes:</p>
<ul>
<li><strong>Environment</strong> (top right) lists every object you create. It is empty now and will fill up in a minute.</li>
<li><strong>Files / Plots / Packages / Help</strong> (bottom right) shows your files, your plots, installed packages and documentation.</li>
<li>The <strong>Source</strong> pane (top left, hidden until you open a file) is where you write scripts you want to keep. The console is for trying things out.</li>
</ul>
</section>
<section id="step-4-your-first-r-session" class="level2">
<h2 class="anchored" data-anchor-id="step-4-your-first-r-session">Step 4: Your first R session</h2>
<p>Type each block into the console. The screenshots show what you should see.</p>
<section id="r-as-a-calculator" class="level3">
<h3 class="anchored" data-anchor-id="r-as-a-calculator">R as a calculator</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb3-2"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sqrt</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>)</span>
<span id="cb3-4"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb3-5"><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-as-calculator.png" class="img-fluid figure-img" alt="RStudio console showing basic arithmetic: 2 + 3 gives 5, 10 / 4 gives 2.5, sqrt(16) gives 4, 2^10 gives 1024, 7 %% 3 gives 1"></p>
<figcaption>Arithmetic in the console. <code>%%</code> is the remainder operator: 7 divided by 3 leaves 1.</figcaption>
</figure>
</div>
<p>The <code>[1]</code> in front of each answer is R telling you this is the first element of the result. It matters once results have more than one value.</p>
</section>
<section id="variables" class="level3">
<h3 class="anchored" data-anchor-id="variables">Variables</h3>
<p>The arrow <code>&lt;-</code> stores a value under a name. Type it as a less-than sign followed by a minus sign, or press Alt+Minus in RStudio.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb4-2">y <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span></span>
<span id="cb4-3">x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> y</span>
<span id="cb4-4">name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ada"</span></span>
<span id="cb4-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello,"</span>, name)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-variables.png" class="img-fluid figure-img" alt="RStudio console creating variables x, y and name, with the Environment pane listing them"></p>
<figcaption>Assigning values. Notice that <code>x</code>, <code>y</code> and <code>name</code> now appear in the Environment pane on the right.</figcaption>
</figure>
</div>
<p>Text goes in quotes; numbers do not. <code>paste()</code> glues pieces of text together with a space.</p>
</section>
<section id="vectors" class="level3">
<h3 class="anchored" data-anchor-id="vectors">Vectors</h3>
<p>A vector is several values of the same kind in one object. Most of R works on whole vectors at once, which is why you rarely need loops.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">ages <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">41</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">29</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">52</span>)</span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(ages)</span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(ages)</span>
<span id="cb5-4">ages[ages <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>]</span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(ages, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">decreasing =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-vectors.png" class="img-fluid figure-img" alt="RStudio console with a numeric vector ages, showing length, mean, logical subsetting and sorting"></p>
<figcaption>Vector operations. <code>ages[ages &gt; 30]</code> keeps only the elements where the condition is true.</figcaption>
</figure>
</div>
<p><code>c()</code> stands for <em>combine</em>. The square brackets select elements, and the condition inside them can be anything that gives TRUE or FALSE for each element.</p>
</section>
<section id="a-built-in-data-frame" class="level3">
<h3 class="anchored" data-anchor-id="a-built-in-data-frame">A built-in data frame</h3>
<p>R ships with practice datasets. <code>mtcars</code> holds 32 cars with 11 measurements each. A data frame is R’s table: rows are observations, columns are variables.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(mtcars[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(mtcars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mpg)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(mtcars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cyl)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-data-frame.png" class="img-fluid figure-img" alt="RStudio console showing head of mtcars, summary of mpg and a table of cylinder counts"></p>
<figcaption>Looking at a data frame. <code>head()</code> shows the first rows, <code>summary()</code> gives the five-number summary and mean of one column, <code>table()</code> counts categories.</figcaption>
</figure>
</div>
<p>The dollar sign picks one column: <code>mtcars$mpg</code> is the miles-per-gallon column. <code>table()</code> counts how many cars have 4, 6 or 8 cylinders.</p>
</section>
<section id="your-first-plot" class="level3">
<h3 class="anchored" data-anchor-id="your-first-plot">Your first plot</h3>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hist</span>(mtcars<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>mpg, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"steelblue"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">border =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>,</span>
<span id="cb7-2">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">main =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Fuel economy of 32 cars"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Miles per gallon"</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-first-plot.png" class="img-fluid figure-img" alt="RStudio with a blue histogram of miles per gallon shown in the Plots pane"></p>
<figcaption>A histogram appears in the Plots pane. The <code>+</code> on the second console line means R was waiting for the rest of the command.</figcaption>
</figure>
</div>
<p>Plots go to the <strong>Plots</strong> pane. The <em>Export</em> button there saves the image as PNG or PDF. Note the <code>+</code> prompt on the second line: R saw that the first line ended mid-command and waited for the closing parenthesis. If you ever get stuck at a <code>+</code> prompt, press Escape.</p>
</section>
</section>
<section id="step-5-install-your-first-package" class="level2">
<h2 class="anchored" data-anchor-id="step-5-install-your-first-package">Step 5: Install your first package</h2>
<p>Base R is complete for the basics. Most real work also uses <strong>packages</strong>, add-ons written by the community and distributed through CRAN. Installing one is a single command. Here is what happens if you try to load a package you have not installed yet:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-package-missing.png" class="img-fluid figure-img" alt="RStudio console showing Error in library(ggplot2): there is no package called 'ggplot2'"></p>
<figcaption>The error every beginner meets once: “there is no package called ‘ggplot2’”. It means the package was never installed on this machine, nothing more.</figcaption>
</figure>
</div>
<p>The fix is <code>install.packages()</code>. The example below uses <code>praise</code>, a tiny package that installs in seconds; the command is the same for <code>ggplot2</code>, <code>dplyr</code> or any other CRAN package.</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"praise"</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-install-package.png" class="img-fluid figure-img" alt="RStudio console output of install.packages('praise') ending with DONE (praise)"></p>
<figcaption><code>install.packages()</code> downloads the package from CRAN and installs it. The last lines say <code>DONE (praise)</code>.</figcaption>
</figure>
</div>
<p>Installing is done once per computer. Loading is done once per session with <code>library()</code>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(praise)</span>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">praise</span>()</span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">praise</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"You just installed your first R package. ${EXCLAMATION}!"</span>)</span></code></pre></div></div>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rverseanalytics.com/posts/images/install-r/r-use-package.png" class="img-fluid figure-img" alt="RStudio console showing library(praise) and two calls to praise() returning encouraging sentences"></p>
<figcaption>Loading and using the package. <code>library()</code> makes its functions available for the rest of the session.</figcaption>
</figure>
</div>
<p>Two things trip people up here:</p>
<ul>
<li><strong>Quotes.</strong> <code>install.packages("praise")</code> needs quotes; <code>library(praise)</code> works with or without them.</li>
<li><strong>Windows personal library.</strong> The first <code>install.packages()</code> on Windows may ask whether to create a personal library in your user folder. Answer <em>Yes</em>.</li>
</ul>
</section>
<section id="common-problems" class="level2">
<h2 class="anchored" data-anchor-id="common-problems">Common problems</h2>
<p><strong>RStudio opens but the console says R could not be found.</strong> R was not installed, or RStudio started before the R installer finished. Install R from CRAN, restart RStudio.</p>
<p><strong><code>install.packages()</code> fails with “cannot open URL” or “unable to access index”.</strong> Your network blocks CRAN, usually a corporate proxy. Try again on another network, or ask IT to allow <code>cloud.r-project.org</code>.</p>
<p><strong>On macOS, “package is damaged and can’t be opened”.</strong> Gatekeeper blocked the download. Right-click the installer and choose <em>Open</em>, or allow it under <em>System Settings → Privacy &amp; Security</em>.</p>
<p><strong>Everything is tiny or huge.</strong> In RStudio, <em>Tools → Global Options → Appearance</em> sets the editor font size and theme. The screenshots here use a dark theme and a large font; the default is a light theme.</p>
<p><strong>I closed RStudio and my variables are gone.</strong> That is expected. Anything you want to keep belongs in a script: <em>File → New File → R Script</em>, then run lines with Ctrl+Enter (Cmd+Return on macOS).</p>
</section>
<section id="where-to-go-next" class="level2">
<h2 class="anchored" data-anchor-id="where-to-go-next">Where to go next</h2>
<p>You now have a working R installation and have run arithmetic, variables, vectors, a data frame, a plot and a package. The next step is practising those same ideas on real data:</p>
<ul>
<li><a href="../courses/r-foundations.html">R Foundations</a>: a free 20-lesson video course that starts exactly where this guide ends.</li>
<li><a href="../learn.html">Learn R in your browser</a>: the same material as interactive lessons, no installation needed, handy on a machine where you cannot install software.</li>
<li><a href="../cheatsheets.html">R biostatistics cheat sheets</a>: one-page PDF references for when you forget a function name.</li>
<li><a href="../posts/r-biostatistics-datasets.html">R Biostatistics Datasets</a>: the built-in clinical datasets to practise on once <code>mtcars</code> gets boring.</li>
</ul>
<p>If you are setting up R for a team, a clinical programming group or a SAS-to-R migration rather than for yourself, see <a href="../for-cros.html#regulatory-grade-r">regulatory-grade R</a> for version-pinned environments and validation documentation, and the <a href="../posts/r-package-qualification-riskmetric.html">R package qualification with riskmetric</a> guide for choosing packages.</p>


</section>

 ]]></description>
  <category>r-basics</category>
  <category>getting-started</category>
  <guid>https://rverseanalytics.com/posts/how-to-install-r-and-rstudio.html</guid>
  <pubDate>Mon, 12 Oct 2026 21:00:00 GMT</pubDate>
  <media:content url="https://rverseanalytics.com/posts/images/install-r/rstudio-ide-overview.png" medium="image" type="image/png" height="75" width="144"/>
</item>
<item>
  <title>Building a Traceability Matrix for Statistical Programming (Without Maintaining It by Hand)</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/traceability-matrix-statistical-programming.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>A traceability matrix proves that every planned analysis exists, was produced by an identified program, and was checked. It links <strong>SAP item → specification → program/function → output → QC record</strong>.</li>
<li>Hand-maintained spreadsheets drift. Generate the matrix from metadata that your pipeline already has: a specification table, roxygen tags or structured comments in programs, and the QC log.</li>
<li>A dozen lines of R produce the matrix and, more importantly, the <strong>gaps</strong>: outputs with no QC record, specifications with no program, programs with no specification.</li>
<li>In a migration, the matrix is what turns a pile of comparison logs into an auditable equivalence report.</li>
</ul>
</div>
<p>The traceability matrix is the document a sponsor’s auditor opens first and the one programming teams least enjoy writing. It does not have to be written. If the specification, the code and the QC log each carry a stable identifier, the matrix is a join.</p>
<section id="what-must-be-linked" class="level2">
<h2 class="anchored" data-anchor-id="what-must-be-linked">What must be linked</h2>
<p>For each analysis output (table, listing, figure or analysis dataset):</p>
<ol type="1">
<li><strong>SAP reference.</strong> Section or table number in the statistical analysis plan.</li>
<li><strong>Specification item.</strong> The mock shell or dataset specification with its version.</li>
<li><strong>Program or function.</strong> File path and version-control commit, or package name and version.</li>
<li><strong>Output.</strong> File name, run date, checksum, environment (lockfile hash).</li>
<li><strong>QC record.</strong> Method (dual programming, code review), QC programmer, date, result, link to the comparison log.</li>
</ol>
<p>Add a status column (not started, produced, QC passed, QC failed, superseded) and you have everything an auditor asks for.</p>
</section>
<section id="put-identifiers-where-the-work-happens" class="level2">
<h2 class="anchored" data-anchor-id="put-identifiers-where-the-work-happens">Put identifiers where the work happens</h2>
<p><strong>Specification table.</strong> One row per output, maintained by the statistician, with an <code>output_id</code> such as <code>T14-1-1</code>.</p>
<p><strong>Programs.</strong> A structured header or, for packaged functions, a roxygen tag:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' Demography table 14.1.1</span></span>
<span id="cb1-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#'</span></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @output_id T14-1-1</span></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @sap_ref 9.2</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @spec_version 2.0</span></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#' @export</span></span>
<span id="cb1-7">t_14_1_1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(adsl) { ... }</span></code></pre></div></div>
</div>
<p>For scripts rather than functions, the same fields go in a fixed comment block that a regex can read.</p>
<p><strong>QC log.</strong> Written by the comparison code itself, one row per comparison run, with the <code>output_id</code>, the result and the tolerance rule set used. See <a href="../posts/sas-to-r-dual-programming-tolerance.html">numerical tolerance rules</a>.</p>
</section>
<section id="generating-the-matrix" class="level2">
<h2 class="anchored" data-anchor-id="generating-the-matrix">Generating the matrix</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-2"></span>
<span id="cb2-3">spec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb2-4">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>output_id, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>sap_ref, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>title,                          <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>spec_version,</span>
<span id="cb2-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T14-1-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9.2"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Demographics"</span>,                   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2.0"</span>,</span>
<span id="cb2-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T14-3-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9.4"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Adverse events by SOC and PT"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2.0"</span>,</span>
<span id="cb2-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F14-2-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9.3"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Kaplan-Meier, primary endpoint"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.1"</span>,</span>
<span id="cb2-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"L16-2-7"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"10.1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Listing of deaths"</span>,              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1.0"</span></span>
<span id="cb2-9">)</span>
<span id="cb2-10"></span>
<span id="cb2-11">programs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb2-12">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>output_id, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>program,            <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>commit,</span>
<span id="cb2-13">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T14-1-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R/t_14_1_1.R"</span>,      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1c3e9f"</span>,</span>
<span id="cb2-14">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T14-3-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R/t_14_3_1.R"</span>,      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"a1c3e9f"</span>,</span>
<span id="cb2-15">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F14-2-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R/f_14_2_1.R"</span>,      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"b77d210"</span></span>
<span id="cb2-16">)</span>
<span id="cb2-17"></span>
<span id="cb2-18">outputs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb2-19">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>output_id, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>file,              <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>run_date,    <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>lock_hash,</span>
<span id="cb2-20">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T14-1-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t_14_1_1.rtf"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-10-06"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9f2a…"</span>,</span>
<span id="cb2-21">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T14-3-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t_14_3_1.rtf"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-10-06"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9f2a…"</span>,</span>
<span id="cb2-22">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F14-2-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f_14_2_1.pdf"</span>,     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-10-07"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"9f2a…"</span></span>
<span id="cb2-23">)</span>
<span id="cb2-24"></span>
<span id="cb2-25">qc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb2-26">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>output_id, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>method,            <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>qc_by, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>qc_date,     <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>result,</span>
<span id="cb2-27">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T14-1-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dual programming"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QC2"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-10-07"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pass"</span>,</span>
<span id="cb2-28">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F14-2-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dual programming"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QC2"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-10-07"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fail: 1 deviation open"</span></span>
<span id="cb2-29">)</span>
<span id="cb2-30"></span>
<span id="cb2-31">matrix <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> spec <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(programs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"output_id"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(outputs,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"output_id"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(qc,       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"output_id"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">status =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(</span>
<span id="cb2-36">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(program)              <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"no program"</span>,</span>
<span id="cb2-37">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(file)                 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"not produced"</span>,</span>
<span id="cb2-38">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(result)               <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"awaiting QC"</span>,</span>
<span id="cb2-39">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">startsWith</span>(result, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pass"</span>)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QC passed"</span>,</span>
<span id="cb2-40">    <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>                        <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QC failed"</span></span>
<span id="cb2-41">  ))</span>
<span id="cb2-42"></span>
<span id="cb2-43">matrix <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(output_id, sap_ref, program, file, method, result, status)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 7
  output_id sap_ref program      file         method           result     status
  &lt;chr&gt;     &lt;chr&gt;   &lt;chr&gt;        &lt;chr&gt;        &lt;chr&gt;            &lt;chr&gt;      &lt;chr&gt; 
1 T14-1-1   9.2     R/t_14_1_1.R t_14_1_1.rtf dual programming pass       QC pa…
2 T14-3-1   9.4     R/t_14_3_1.R t_14_3_1.rtf &lt;NA&gt;             &lt;NA&gt;       await…
3 F14-2-1   9.3     R/f_14_2_1.R f_14_2_1.pdf dual programming fail: 1 d… QC fa…
4 L16-2-7   10.1    &lt;NA&gt;         &lt;NA&gt;         &lt;NA&gt;             &lt;NA&gt;       no pr…</code></pre>
</div>
</div>
<p>The join is the matrix. The <code>status</code> column is the project dashboard. And the gaps are visible at once:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">matrix <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(status <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"QC passed"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(output_id, title, status)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 3
  output_id title                          status     
  &lt;chr&gt;     &lt;chr&gt;                          &lt;chr&gt;      
1 T14-3-1   Adverse events by SOC and PT   awaiting QC
2 F14-2-1   Kaplan-Meier, primary endpoint QC failed  
3 L16-2-7   Listing of deaths              no program </code></pre>
</div>
</div>
<p>A listing with no program, a table awaiting QC and a figure with an open deviation. That is the morning stand-up list, generated rather than remembered.</p>
</section>
<section id="catching-the-reverse-gaps" class="level2">
<h2 class="anchored" data-anchor-id="catching-the-reverse-gaps">Catching the reverse gaps</h2>
<p>Traceability runs both ways. Programs with no specification are as much a finding as specifications with no program:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(programs, spec, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"output_id"</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># orphan programs (none here)</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 0 × 3
# ℹ 3 variables: output_id &lt;chr&gt;, program &lt;chr&gt;, commit &lt;chr&gt;</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">anti_join</span>(outputs,  qc,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"output_id"</span>)   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># outputs never QC'd</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 4
  output_id file         run_date   lock_hash
  &lt;chr&gt;     &lt;chr&gt;        &lt;chr&gt;      &lt;chr&gt;    
1 T14-3-1   t_14_3_1.rtf 2026-10-06 9f2a…    </code></pre>
</div>
</div>
<p>Add these two checks to CI and the matrix can never silently drift from the codebase.</p>
</section>
<section id="rendering-it" class="level2">
<h2 class="anchored" data-anchor-id="rendering-it">Rendering it</h2>
<p>The same data frame renders to the sponsor-facing document with <code>gt</code>, <code>flextable</code> or <code>rtables</code>, and to an Excel export for QA with <code>openxlsx</code>. Keep the rendered copies as artefacts of a tagged release, never as the source of truth. When a reviewer asks “which commit produced table 14.3.1 and who checked it”, the answer is one filter away.</p>
</section>
<section id="in-a-sas-to-r-migration" class="level2">
<h2 class="anchored" data-anchor-id="in-a-sas-to-r-migration">In a SAS-to-R migration</h2>
<p>The matrix gets two extra columns during the parallel-run phase: the SAS reference output and the comparison log. The equivalence report is then a summary of the matrix: outputs compared, outputs identical, explained differences, deviations and their resolution. This is the artefact that closes Phase 3 of the <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">five-phase roadmap</a> and feeds the <a href="../posts/r-validation-plan-regulatory-submissions.html">validation summary</a>.</p>
<p>Related: <a href="../posts/r-package-qualification-riskmetric.html">qualifying packages with riskmetric and renv</a>, and our <a href="../for-cros.html">services for CROs and sponsors</a>.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>validation</category>
  <category>r-programming</category>
  <guid>https://rverseanalytics.com/posts/traceability-matrix-statistical-programming.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>From SAS Macros to R Functions: A Worked Conversion</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/sas-macros-to-r-functions.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>A SAS macro is text substitution; an R function is a first-class object. Convert the <strong>intent</strong> (inputs, outputs, edge cases), not the tokens.</li>
<li>Map macro parameters to function arguments with defaults, replace <code>%IF</code> branches with vectorised logic, and return a data frame rather than writing a dataset to a library.</li>
<li>Put converted functions in an <strong>internal package</strong> with roxygen documentation and <code>testthat</code> tests. Each test is a reusable piece of validation evidence.</li>
<li>One shared function replaces N copies of a macro. That is where the migration’s cost savings come from.</li>
</ul>
</div>
<p>Shared macros are the assets in a SAS codebase. They are also where SAS-to-R migrations go wrong when programmers translate <code>%DO</code> loops into <code>for</code> loops and <code>CALL SYMPUT</code> into global variables. Here is a small but realistic conversion done the way we do it in a migration package.</p>
<section id="the-sas-macro" class="level2">
<h2 class="anchored" data-anchor-id="the-sas-macro">The SAS macro</h2>
<p>A typical descriptive-statistics macro: summarise a numeric variable by treatment, returning n, mean (SD), median and range, formatted for a table.</p>
<pre class="sas"><code>%macro desc_stat(indata=, var=, by=TRT01A, dec=1, out=);
  proc means data=&amp;indata noprint nway;
    class &amp;by;
    var &amp;var;
    output out=_s n=n mean=mean std=sd median=med min=min max=max;
  run;
  data &amp;out;
    set _s;
    length stat $40;
    n_c    = put(n, 3.);
    meansd = cats(put(mean, 8.&amp;dec), " (", put(sd, 8.%eval(&amp;dec+1)), ")");
    med_c  = put(med, 8.&amp;dec);
    range  = cats(put(min, 8.&amp;dec), ", ", put(max, 8.&amp;dec));
    keep &amp;by n_c meansd med_c range;
  run;
%mend desc_stat;

%desc_stat(indata=adsl, var=AGE, out=t_age);</code></pre>
<p>Things to notice: the by-variable is a parameter; decimal places drive formats for mean (<code>dec</code>) and SD (<code>dec + 1</code>); the output is a dataset of character columns ready for the table layer; missing values are silently dropped by PROC MEANS.</p>
</section>
<section id="the-r-function" class="level2">
<h2 class="anchored" data-anchor-id="the-r-function">The R function</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-2"></span>
<span id="cb2-3">round_sas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sign</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>digits <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-9</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>digits</span>
<span id="cb2-4"></span>
<span id="cb2-5">fmt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, dec) {</span>
<span id="cb2-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(x), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formatC</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round_sas</span>(x, dec), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> dec))</span>
<span id="cb2-7">}</span>
<span id="cb2-8"></span>
<span id="cb2-9">desc_stat <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRT01A"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dec =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na_rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) {</span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.data.frame</span>(data), var <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(data), by <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(data))</span>
<span id="cb2-11">  data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(by))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-13">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(</span>
<span id="cb2-14">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(.data[[var]])),</span>
<span id="cb2-15">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(.data[[var]],   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> na_rm),</span>
<span id="cb2-16">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(.data[[var]],     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> na_rm),</span>
<span id="cb2-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">med    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(.data[[var]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> na_rm),</span>
<span id="cb2-18">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(.data[[var]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> na_rm)),</span>
<span id="cb2-19">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(.data[[var]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> na_rm)),</span>
<span id="cb2-20">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span></span>
<span id="cb2-21">    ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">transmute</span>(</span>
<span id="cb2-23">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(by)),</span>
<span id="cb2-24">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_c    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formatC</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb2-25">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">meansd =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt</span>(mean, dec), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" ("</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt</span>(sd, dec <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>),</span>
<span id="cb2-26">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">med_c  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt</span>(med, dec),</span>
<span id="cb2-27">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">range  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt</span>(min, dec), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fmt</span>(max, dec))</span>
<span id="cb2-28">    )</span>
<span id="cb2-29">}</span>
<span id="cb2-30"></span>
<span id="cb2-31"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>)</span>
<span id="cb2-32">adsl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb2-33">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">USUBJID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-%03d"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>),</span>
<span id="cb2-34">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRT01A  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Placebo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug 10 mg"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>),</span>
<span id="cb2-35">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AGE     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)))</span>
<span id="cb2-36">)</span>
<span id="cb2-37">adsl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AGE[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span></span>
<span id="cb2-38"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc_stat</span>(adsl, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGE"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 5
  TRT01A     n_c   meansd      med_c range     
  &lt;chr&gt;      &lt;chr&gt; &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt;     
1 Drug 10 mg " 19" 57.6 (5.90) 57.0  47.0, 72.0
2 Placebo    " 19" 55.6 (7.42) 55.0  44.0, 70.0</code></pre>
</div>
</div>
<section id="what-changed-and-why" class="level3">
<h3 class="anchored" data-anchor-id="what-changed-and-why">What changed, and why</h3>
<ul>
<li><strong>Parameters became arguments with defaults.</strong> <code>by = "TRT01A"</code> and <code>dec = 1</code> mirror the macro, and <code>stopifnot()</code> gives an immediate, readable error if a column is missing, where the macro would have produced a cryptic log note.</li>
<li><strong>Formats became a helper.</strong> <code>fmt()</code> centralises rounding and width. We use SAS-style rounding here because the function is for the parallel-run phase; see <a href="../posts/sas-vs-r-rounding-differences.html">rounding differences</a>.</li>
<li><strong>Missing values are explicit.</strong> <code>n</code> counts non-missing, matching PROC MEANS, and the <code>na_rm</code> argument makes the policy visible instead of implicit.</li>
<li><strong>No side effects.</strong> The function returns a data frame. Writing to disk is the caller’s decision, which is what makes the function testable.</li>
</ul>
</section>
</section>
<section id="putting-it-in-a-package" class="level2">
<h2 class="anchored" data-anchor-id="putting-it-in-a-package">Putting it in a package</h2>
<p>In the migration package (<code>usethis::create_package("cromigr")</code>), the function gets roxygen documentation and a test file:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tests/testthat/test-desc_stat.R</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">test_that</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"desc_stat matches SAS reference for AGE"</span>, {</span>
<span id="cb4-3">  ref <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">readRDS</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">test_path</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"fixtures"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"t_age_sas.rds"</span>))   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># exported from SAS</span></span>
<span id="cb4-4">  out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc_stat</span>(adsl_fixture, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGE"</span>)</span>
<span id="cb4-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expect_equal</span>(out, ref)</span>
<span id="cb4-6">})</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">test_that</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"desc_stat handles all-missing groups without error"</span>, {</span>
<span id="cb4-9">  d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRT01A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AGE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_real_</span>))</span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">expect_no_error</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc_stat</span>(d, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AGE"</span>))</span>
<span id="cb4-11">})</span></code></pre></div></div>
</div>
<p>The first test <em>is</em> validation evidence: it encodes the SAS reference output and proves the R function reproduces it. The second protects against the edge case that would have surfaced as a warning in a SAS log that nobody read. <code>devtools::test()</code> runs them on every change; <code>renv</code> pins the package set.</p>
</section>
<section id="patterns-for-other-macro-types" class="level2">
<h2 class="anchored" data-anchor-id="patterns-for-other-macro-types">Patterns for other macro types</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>SAS macro pattern</th>
<th>R equivalent</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>%DO i = 1 %TO &amp;n</code> over variables</td>
<td><code>lapply()</code>/<code>purrr::map()</code> over a character vector of names</td>
</tr>
<tr class="even">
<td><code>%IF &amp;type = CAT %THEN</code> branches</td>
<td>Separate small functions, dispatched by an argument or S3 class</td>
</tr>
<tr class="odd">
<td><code>CALL SYMPUT</code> to pass values around</td>
<td>Return values; never global assignment</td>
</tr>
<tr class="even">
<td><code>PROC SQL</code> joins inside macros</td>
<td><code>dplyr::left_join()</code> with explicit <code>by =</code></td>
</tr>
<tr class="odd">
<td>Macro writing many datasets to WORK</td>
<td>A list of data frames, or one long data frame</td>
</tr>
<tr class="even">
<td><code>%INCLUDE</code> of shared code</td>
<td><code>library(cromigr)</code></td>
</tr>
</tbody>
</table>
<p>The general rule: anything the macro does by rewriting text, the function should do with data structures.</p>
</section>
<section id="what-this-buys-you" class="level2">
<h2 class="anchored" data-anchor-id="what-this-buys-you">What this buys you</h2>
<p>Fifty studies that each called <code>%desc_stat</code> now call one tested function. A specification change is one edit, one test update and one package version. The package’s test log and <code>riskmetric</code> scores feed the <a href="../posts/r-validation-plan-regulatory-submissions.html">validation plan</a> directly. This is Phase 2 of the <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">five-phase roadmap</a>, and it is where the migration either pays for itself or does not.</p>
<p>Related: <a href="../posts/proc-freq-proc-means-in-r.html">PROC FREQ, PROC MEANS and PROC UNIVARIATE in R</a>, and the <a href="../sas-to-r.html">SAS to R migration service</a>.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>sas-to-r</category>
  <category>r-programming</category>
  <guid>https://rverseanalytics.com/posts/sas-macros-to-r-functions.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>SAS to R Migration Roadmap: The Five Phases, What Each Delivers and What It Costs</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/sas-to-r-migration-roadmap-5-phases.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>A migration that works is a <strong>programme of five phases</strong>, each with a deliverable your QA group can file: Assessment → Conversion → Equivalence → Documentation → Adoption.</li>
<li>Phase 1 should be fixed-scope and short (two to six weeks). It produces the inventory, risk classification and macro reuse map that let you price everything else on evidence.</li>
<li>Effort is driven by the <strong>number of distinct macros</strong>, not the number of programs. Fifty studies built on twelve shared macros is a smaller job than ten studies with bespoke code.</li>
<li>Start with a closed study so no live deliverable depends on the pilot, and keep the SAS originals as the reference until the equivalence report is signed.</li>
</ul>
</div>
<p>Most SAS-to-R migrations do not fail on statistics. They stall because the scope was never bounded, because every programmer converted code their own way, or because “validation” was left for the end. The roadmap below is the structure we use to avoid all three. It maps directly onto what sponsors and CRO procurement teams ask for.</p>
<section id="phase-1-assessment" class="level2">
<h2 class="anchored" data-anchor-id="phase-1-assessment">Phase 1: Assessment</h2>
<p><strong>Goal:</strong> know exactly what you have before deciding what to move.</p>
<p>Deliverables:</p>
<ul>
<li><strong>Program inventory.</strong> Every SAS program, macro and format catalogue, with study, purpose, author, last run and line count. A script over the SAS logs and <code>%INCLUDE</code> tree does most of this.</li>
<li><strong>Complexity and risk classification.</strong> Programs scored on size, macro depth, external dependencies (e.g.&nbsp;PROC SQL against databases, custom formats) and output criticality. Usually three bands: straightforward, moderate, complex.</li>
<li><strong>Macro reuse map.</strong> Which macros are shared across studies and how often they are called. This is the single most important number in the project.</li>
<li><strong>Specification and shell review.</strong> Are there written specifications and mock shells for the outputs? Where they are missing, the SAS code <em>is</em> the spec, which raises the risk band.</li>
<li><strong>Migration candidate list.</strong> What should move first (shared, well-specified, high reuse), what should move later, and what should be retired or left in SAS because it is run once a year.</li>
</ul>
<p>Typical effort: two to six weeks for a mid-sized CRO portfolio. This phase is fixed-price in our proposals because its scope is bounded by the inventory itself.</p>
</section>
<section id="phase-2-conversion" class="level2">
<h2 class="anchored" data-anchor-id="phase-2-conversion">Phase 2: Conversion</h2>
<p><strong>Goal:</strong> a maintainable R codebase, not a line-by-line transliteration.</p>
<p>Deliverables:</p>
<ul>
<li><strong>R programming standards.</strong> Short, enforced, with examples. Project layout, naming, how specifications are referenced, lint rules, and when code must be packaged.</li>
<li><strong>Package and environment strategy.</strong> Approved package list (pharmaverse first: admiral for ADaM, rtables/tern or gtsummary for tables, xportr for transport files), the renv lockfile policy, and the internal package repository if one is needed.</li>
<li><strong>Reusable functions and packages.</strong> Each shared macro becomes a documented, unit-tested function in an internal package. See <a href="../posts/sas-macros-to-r-functions.html">turning SAS macros into R functions</a>.</li>
<li><strong>Converted programs</strong> that call those functions to produce the datasets and TLFs for the pilot study.</li>
<li><strong>Formatting layer.</strong> RTF/DOCX/PDF output matching the sponsor’s shells, including titles, footnotes, pagination and page-by-group behaviour.</li>
</ul>
<p>The effort driver is the macro map from Phase 1. Converting a macro once and reusing it is the entire economic argument for the project. Resist the temptation to convert programs study by study without this layer; it produces fifty copies of the same bug.</p>
</section>
<section id="phase-3-equivalence-and-validation" class="level2">
<h2 class="anchored" data-anchor-id="phase-3-equivalence-and-validation">Phase 3: Equivalence and validation</h2>
<p><strong>Goal:</strong> documented proof that R reproduces the SAS reference.</p>
<p>Deliverables:</p>
<ul>
<li><strong>Dual-programming protocol</strong> naming what is compared and how, with the <a href="../posts/sas-to-r-dual-programming-tolerance.html">numerical tolerance rules</a> decided before the first comparison.</li>
<li><strong>Dataset-level comparison</strong> of every analysis dataset (structure, keys, values).</li>
<li><strong>TLF-level comparison</strong> of every displayed cell after a common rounding rule.</li>
<li><strong>Independent QC</strong> by a programmer who did not write the R code.</li>
<li><strong>Traceability</strong> from SAP to specification to program to output to QC record.</li>
<li><strong>Exception and deviation log</strong> with root causes and resolutions.</li>
<li><strong>Validation summary</strong> with counts: compared, identical, explained, deviations resolved.</li>
</ul>
<p>Most differences that appear here are conventions: <a href="../posts/sas-vs-r-rounding-differences.html">rounding</a>, <a href="../posts/missing-values-sas-vs-r.html">missing value handling</a>, sums-of-squares types, tie handling. Each becomes a standard once documented.</p>
</section>
<section id="phase-4-documentation" class="level2">
<h2 class="anchored" data-anchor-id="phase-4-documentation">Phase 4: Documentation</h2>
<p><strong>Goal:</strong> a document set your quality system and a sponsor’s auditor can accept.</p>
<p>Deliverables: the <a href="../posts/r-validation-plan-regulatory-submissions.html">R validation plan</a>, environment and package qualification records (<a href="../posts/r-package-qualification-riskmetric.html">riskmetric</a>), SOPs for programming, QC and change control, the risk assessment, the <a href="../posts/traceability-matrix-statistical-programming.html">traceability matrix</a>, programmer and QC documentation templates, and a regulatory justification memo summarising why the R process meets expectations. Much of this is written in parallel with Phase 3, because Phase 3 generates the evidence it references.</p>
</section>
<section id="phase-5-sponsor-and-regulatory-adoption" class="level2">
<h2 class="anchored" data-anchor-id="phase-5-sponsor-and-regulatory-adoption">Phase 5: Sponsor and regulatory adoption</h2>
<p><strong>Goal:</strong> the people who have to approve the change say yes.</p>
<p>Deliverables:</p>
<ul>
<li><strong>Sponsor presentation</strong> of the approach, the evidence and the controls, pitched to QA and biostatistics heads rather than programmers.</li>
<li><strong>R adoption strategy</strong> for the organisation: which studies move when, training plan, how SAS and R coexist during transition.</li>
<li><strong>Regulatory positioning.</strong> A written response to “why R?” grounded in the FDA Statistical Software Clarifying Statement, the R Consortium pilot submissions, and your own validation evidence. Read <a href="../posts/fda-r-submissions-2026.html">Is R accepted by the FDA?</a>.</li>
<li><strong>Response strategy</strong> for health-authority or sponsor questions about R versus SAS, including who answers and with what evidence.</li>
<li><strong>Submission package strategy.</strong> How R code, lockfiles and logs are delivered in an eCTD structure, following the pilot submissions’ precedent.</li>
</ul>
</section>
<section id="sequencing-and-timeline" class="level2">
<h2 class="anchored" data-anchor-id="sequencing-and-timeline">Sequencing and timeline</h2>
<p>A realistic first cycle for one pilot study: Phase 1 in weeks 1-4, Phases 2-3 in weeks 5-16, Phase 4 largely overlapping weeks 10-18, Phase 5 at weeks 18-20. Later studies reuse the package layer and documentation, so each subsequent study is a fraction of the first.</p>
</section>
<section id="mistakes-that-stall-migrations" class="level2">
<h2 class="anchored" data-anchor-id="mistakes-that-stall-migrations">Mistakes that stall migrations</h2>
<ol type="1">
<li><strong>Converting programs instead of macros.</strong> The codebase grows and nothing gets easier.</li>
<li><strong>Deciding tolerances after seeing differences.</strong> The equivalence report loses credibility.</li>
<li><strong>Leaving validation to the end.</strong> The evidence is scattered and the plan is written backwards.</li>
<li><strong>A pilot on a live deliverable.</strong> Deadline pressure wins; the migration is blamed.</li>
<li><strong>No owner for package approval.</strong> Programmers install what they need and the lockfile becomes a liability.</li>
</ol>
</section>
<section id="how-we-run-it" class="level2">
<h2 class="anchored" data-anchor-id="how-we-run-it">How we run it</h2>
<p>We offer Phase 1 as a fixed-scope, fixed-price assessment, then price Phases 2-5 from its findings. The equivalence report and the templates are yours whichever way you continue. Details and a scoping call are on the <a href="../sas-to-r.html">SAS to R migration page</a> and the <a href="../for-cros.html">services for CROs and pharma</a>.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>sas-to-r</category>
  <guid>https://rverseanalytics.com/posts/sas-to-r-migration-roadmap-5-phases.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Missing Values in SAS vs R: The Differences That Change Your Numbers</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/missing-values-sas-vs-r.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>In SAS, numeric missing is <strong>less than every number</strong>: <code>if AGE &lt; 65</code> keeps missing ages. In R, <code>AGE &lt; 65</code> is <code>NA</code> for missing values and <code>subset()</code>/<code>filter()</code> drop them. This single rule explains most record-count differences in a parallel run.</li>
<li>SAS has special missings (<code>.A</code> to <code>.Z</code>, <code>._</code>) used for “not done”, “not applicable” and the like. R has one <code>NA</code> per type; model the reason in a second variable.</li>
<li>SAS character missing is a blank string that sorts first; R distinguishes <code>""</code> from <code>NA_character_</code>.</li>
<li>R functions default to <strong>propagating</strong> NA (<code>mean()</code> returns NA) where SAS procedures <strong>drop</strong> it. Always state <code>na.rm</code> explicitly in migration code.</li>
</ul>
</div>
<p>Every SAS programmer knows that a missing value is “the smallest number”. Every R programmer knows that <code>NA</code> means “unknown” and that comparisons with it are unknown too. Both are internally consistent, and they produce different record counts the moment a filter touches a variable with missing values. In a migration this appears on day one, so it is worth being precise about it.</p>
<section id="comparisons-and-filters" class="level2">
<h2 class="anchored" data-anchor-id="comparisons-and-filters">Comparisons and filters</h2>
<pre class="sas"><code>data young;
  set adsl;
  if AGE &lt; 65;   /* missing AGE is kept: . &lt; 65 is TRUE in SAS */
run;</code></pre>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">adsl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">USUBJID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-%03d"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AGE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">54</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">71</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">63</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">68</span>))</span>
<span id="cb2-2">adsl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AGE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  TRUE    NA FALSE  TRUE    NA FALSE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(adsl, AGE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># R drops the NA rows</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  USUBJID AGE
1  01-001  54
4  01-004  63</code></pre>
</div>
</div>
<p>To reproduce the SAS behaviour explicitly:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset</span>(adsl, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(AGE) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> AGE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  USUBJID AGE
1  01-001  54
2  01-002  NA
4  01-004  63
5  01-005  NA</code></pre>
</div>
</div>
<p>The SAS-safe idiom is <code>if AGE &lt; 65 and not missing(AGE)</code>, and good SAS code has it. If the legacy code does not, the R translation will have fewer records and the equivalence review has to decide which behaviour the specification intended. In our experience the specification nearly always intends the R behaviour, and the SAS program had a latent defect.</p>
</section>
<section id="sorting" class="level2">
<h2 class="anchored" data-anchor-id="sorting">Sorting</h2>
<p>SAS sorts numeric missing first (ascending) and character blanks first. R’s <code>order()</code> and <code>dplyr::arrange()</code> put <code>NA</code> <strong>last</strong> by default.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(x)                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NA dropped entirely</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.last =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NA last</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  1  2  3 NA</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sort</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.last =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NA first, like SAS</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] NA  1  2  3</code></pre>
</div>
</div>
<p>For <code>first.</code>/<code>last.</code> logic or “take the earliest visit” derivations, this changes which record is picked. Match SAS during parallel runs with <code>na.last = FALSE</code> or <code>arrange(desc(is.na(x)), x)</code>, and write the intended rule into the specification.</p>
</section>
<section id="counts-and-denominators" class="level2">
<h2 class="anchored" data-anchor-id="counts-and-denominators">Counts and denominators</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1">sex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M"</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(sex)                      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># NA excluded, like PROC FREQ default</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>sex
F M 
3 1 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">useNA =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ifany"</span>)     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># like PROC FREQ / MISSING</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>sex
   F    M &lt;NA&gt; 
   3    1    2 </code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(sex)                     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># total records</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 6</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(sex))                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PROC MEANS N</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4</code></pre>
</div>
</div>
<p><code>dplyr::n()</code> counts rows including NA; PROC MEANS <code>N</code> counts non-missing. Use <code>sum(!is.na(x))</code> wherever a SAS <code>N</code> is reproduced. See <a href="../posts/proc-freq-proc-means-in-r.html">PROC FREQ and PROC MEANS in R</a> for the full set of translations.</p>
</section>
<section id="aggregation-functions" class="level2">
<h2 class="anchored" data-anchor-id="aggregation-functions">Aggregation functions</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb22" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1">v <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">4.0</span>)</span>
<span id="cb22-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(v)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] NA</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.25</code></pre>
</div>
</div>
<p>SAS <code>MEAN()</code> across variables in a DATA step and PROC MEANS both skip missing silently. R propagates. The migration standard should require <code>na.rm = TRUE</code> (or the opposite) to be written at every call site, so the choice is visible to the QC reviewer.</p>
</section>
<section id="special-missing-values" class="level2">
<h2 class="anchored" data-anchor-id="special-missing-values">Special missing values</h2>
<p>SAS numeric missing can be <code>.</code>, <code>._</code> or <code>.A</code> through <code>.Z</code>, and they sort in that order, all below every number. Clinical datasets use them for “not done”, “not evaluable”, “below limit of quantification” and so on. R has no equivalent. The clean translation is one value variable plus one reason variable:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1">lb <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">USUBJID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"02"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"03"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"04"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AVAL =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5.2</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">7.9</span>),</span>
<span id="cb26-2">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AVALC =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5.2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;LLOQ"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ND"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"7.9"</span>))</span>
<span id="cb26-3">lb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>missing_reason <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(lb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AVAL), lb<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AVALC, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>)</span>
<span id="cb26-4">lb</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  USUBJID AVAL AVALC missing_reason
1      01  5.2   5.2           &lt;NA&gt;
2      02   NA &lt;LLOQ          &lt;LLOQ
3      03   NA    ND             ND
4      04  7.9   7.9           &lt;NA&gt;</code></pre>
</div>
</div>
<p>In SDTM/ADaM the analogous information lives in <code>--STAT</code>, <code>--REASND</code> and <code>AVALC</code>, so this is also the standards-compliant form. <code>haven::read_sas()</code> and <code>haven::read_xpt()</code> preserve SAS special missings as tagged NA, which <code>haven::is_tagged_na()</code> and <code>haven::na_tag()</code> can read back if you need the original codes during parallel runs.</p>
</section>
<section id="character-missing" class="level2">
<h2 class="anchored" data-anchor-id="character-missing">Character missing</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1">s <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb28-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(s)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE FALSE  TRUE</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1">s <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">""</span></span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE  TRUE    NA</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb32" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nchar</span>(s)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  1  0 NA</code></pre>
</div>
</div>
<p>SAS has only the blank. R distinguishes empty string from NA, and <code>haven</code> imports SAS blanks as <code>""</code>, not <code>NA</code>. Decide once: our standard converts imported <code>""</code> to <code>NA_character_</code> at the boundary (<code>dplyr::na_if(x, "")</code>), so downstream code has one notion of missing.</p>
</section>
<section id="arithmetic-and-derivations" class="level2">
<h2 class="anchored" data-anchor-id="arithmetic-and-derivations">Arithmetic and derivations</h2>
<p>SAS: any arithmetic with a missing operand is missing, and the log notes “Missing values were generated”. R: the same, silently. The difference is the <code>SUM()</code> function versus the <code>+</code> operator in SAS: <code>SUM(a, b)</code> ignores missing, <code>a + b</code> propagates. In R <code>sum(a, b, na.rm = TRUE)</code> is the first and <code>a + b</code> the second. A macro that used <code>SUM()</code> for a total score must be translated with <code>rowSums(..., na.rm = TRUE)</code> or the scores will differ for every subject with one missing item. Check the scoring rule in the specification: often a minimum number of non-missing items is required before a total is computed, and that rule belongs in the function.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb34" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1">items <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">q3 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))</span>
<span id="cb34-2">items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total_sas_sum  <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(items[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># SUM(q1,q2,q3)</span></span>
<span id="cb34-3">items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total_plus     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>q3               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># q1+q2+q3</span></span>
<span id="cb34-4">items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_items        <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(items[, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>]))</span>
<span id="cb34-5">items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total_rule     <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ifelse</span>(items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_items <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>total_sas_sum <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> items<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>n_items, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># prorated, ≥2 items</span></span>
<span id="cb34-6">items</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  q1 q2 q3 total_sas_sum total_plus n_items total_rule
1  3  2  5            10         10       3         10
2  4 NA  4             8         NA       2         12
3 NA NA  3             3         NA       1         NA</code></pre>
</div>
</div>
</section>
<section id="a-checklist-for-migration-code-review" class="level2">
<h2 class="anchored" data-anchor-id="a-checklist-for-migration-code-review">A checklist for migration code review</h2>
<ol type="1">
<li>Every comparison on a variable that can be missing states what happens to NA.</li>
<li>Every <code>sort</code>/<code>arrange</code> on such a variable states <code>na.last</code>.</li>
<li>Every aggregation states <code>na.rm</code>.</li>
<li><code>""</code> is converted to <code>NA</code> at import, once.</li>
<li>Special missings are carried as a reason variable, not lost.</li>
<li>Record counts are logged before and after each filter and compared to the SAS log.</li>
</ol>
<p>These six lines prevent the majority of “R gives a different N” tickets. The rest are covered by the <a href="../posts/sas-to-r-dual-programming-tolerance.html">numerical tolerance rules</a> and the <a href="../posts/sas-vs-r-rounding-differences.html">rounding post</a>.</p>


</section>

 ]]></description>
  <category>sas-to-r</category>
  <category>r-programming</category>
  <category>clinical</category>
  <guid>https://rverseanalytics.com/posts/missing-values-sas-vs-r.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Qualifying R Packages for Clinical Use with riskmetric and renv</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/r-package-qualification-riskmetric.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>Package qualification answers one question per package: <strong>is the evidence of quality sufficient for this intended use?</strong> Write the criteria first, then score.</li>
<li><code>riskmetric</code> computes standardised metrics (documentation, tests, maintenance, adoption) and a composite score; the score informs the decision, it does not make it.</li>
<li>Operational qualification means <strong>running the package’s own tests in your environment</strong> and filing the log, not reading the CRAN check page.</li>
<li><code>renv</code> pins the approved set. A change to the lockfile is a change-controlled event that triggers re-qualification of what changed.</li>
</ul>
</div>
<p>The R ecosystem has twenty thousand packages and no central certification. That is exactly why a sponsor’s QA group asks how you choose and control them. The answer is a short procedure and a folder of evidence. Here is the procedure we deliver as part of an <a href="../posts/r-validation-plan-regulatory-submissions.html">R validation plan</a>.</p>
<section id="step-1-acceptance-criteria" class="level2">
<h2 class="anchored" data-anchor-id="step-1-acceptance-criteria">Step 1: acceptance criteria</h2>
<p>Write them before looking at any package. A workable set:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Criterion</th>
<th>Threshold</th>
<th>Evidence</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Source</td>
<td>CRAN, Bioconductor or approved internal repository</td>
<td>Repository record</td>
</tr>
<tr class="even">
<td>Documentation</td>
<td>All exported functions documented; vignette or website</td>
<td><code>riskmetric</code> doc metrics</td>
</tr>
<tr class="odd">
<td>Testing</td>
<td>Test suite present; coverage reported or test count substantial</td>
<td><code>riskmetric</code> test metrics; OQ log</td>
</tr>
<tr class="even">
<td>Maintenance</td>
<td>Release or commit within 24 months; bug tracker active</td>
<td><code>riskmetric</code> maintenance metrics</td>
</tr>
<tr class="odd">
<td>Adoption</td>
<td>Meaningful download counts or multi-sponsor use</td>
<td><code>riskmetric</code> community metrics</td>
</tr>
<tr class="even">
<td>Licence</td>
<td>Compatible with intended use</td>
<td>DESCRIPTION</td>
</tr>
<tr class="odd">
<td>Reverse dependency risk</td>
<td>Dependencies themselves approved or assessed</td>
<td>Dependency tree</td>
</tr>
</tbody>
</table>
<p>Tiers help: <strong>core</strong> packages used in every analysis (base, stats, survival, admiral, rtables, gtsummary) get the full assessment and OQ; <strong>supporting</strong> packages (string handling, file IO) get the criteria check; <strong>convenience</strong> packages used only interactively are not qualified and not allowed in production code.</p>
</section>
<section id="step-2-score-with-riskmetric" class="level2">
<h2 class="anchored" data-anchor-id="step-2-score-with-riskmetric">Step 2: score with riskmetric</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(riskmetric)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-3"></span>
<span id="cb1-4">pkgs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admiral"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rtables"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tern"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gtsummary"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"survival"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"renv"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xportr"</span>)</span>
<span id="cb1-5"></span>
<span id="cb1-6">assessment <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pkg_ref</span>(pkgs) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pkg_assess</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pkg_score</span>()</span>
<span id="cb1-9"></span>
<span id="cb1-10">assessment <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(package, version, pkg_score, has_vignettes, has_website,</span>
<span id="cb1-12">         covr_coverage, downloads_1yr, news_current, bugs_status) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(pkg_score))</span></code></pre></div></div>
</div>
<p><code>riskmetric</code> reports metrics such as whether a vignette and website exist, test coverage when available, downloads in the last year, whether the NEWS file is current, and the proportion of recent bug reports closed, then combines them into <code>pkg_score</code>. The composite is a convenience. The decision record should cite the individual metrics against the criteria in Step 1, because that is what an auditor can check.</p>
<p>The R Validation Hub, a cross-industry group under the R Consortium, maintains riskmetric and publishes a white paper describing this risk-based approach. Citing it in your SOP is appropriate and expected.</p>
</section>
<section id="step-3-operational-qualification" class="level2">
<h2 class="anchored" data-anchor-id="step-3-operational-qualification">Step 3: operational qualification</h2>
<p>Install the approved versions, then run each core package’s own tests in your environment:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">testInstalledPackage</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"survival"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">types =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tests"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">outDir =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"oq/survival"</span>)</span>
<span id="cb2-2">res   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0L means all tests passed; logs land in oq/survival/</span></span></code></pre></div></div>
</div>
<p>Note that <code>tools::testInstalledPackage()</code> requires the package to have been installed with its tests (<code>install.packages(..., INSTALL_opts = "--install-tests")</code>); set that in the qualification build. For packages that use <code>testthat</code>, <code>testthat::test_package()</code> works after the same install option. Base R’s own suite runs with <code>tools::testInstalledBasic("both")</code>.</p>
<p>File the logs with the R version, platform and the renv lockfile hash. This is the IQ/OQ record. It takes an afternoon the first time and is scripted thereafter.</p>
</section>
<section id="step-4-pin-with-renv" class="level2">
<h2 class="anchored" data-anchor-id="step-4-pin-with-renv">Step 4: pin with renv</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">renv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bare =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(pkgs)             <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># approved versions from the approved repository</span></span>
<span id="cb3-3">renv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">snapshot</span>()                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># writes renv.lock</span></span></code></pre></div></div>
</div>
<p>The lockfile is the definition of the qualified environment. Three rules make it work in a regulated setting:</p>
<ol type="1">
<li><strong>Repositories are fixed.</strong> Point <code>renv</code> at a dated snapshot (Posit Package Manager or an internal mirror) so <code>renv::restore()</code> is reproducible years later.</li>
<li><strong>Changes are controlled.</strong> Updating a package means a change request, a re-run of Steps 2-3 for the package and its changed dependencies, a new lockfile and a new environment version number.</li>
<li><strong>Production runs record the hash.</strong> <code>tools::md5sum("renv.lock")</code> and <code>sessionInfo()</code> go into every output log.</li>
</ol>
</section>
<section id="step-5-the-decision-record" class="level2">
<h2 class="anchored" data-anchor-id="step-5-the-decision-record">Step 5: the decision record</h2>
<p>One row per package per version:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
<col style="width: 11%">
</colgroup>
<thead>
<tr class="header">
<th>Package</th>
<th>Version</th>
<th>Tier</th>
<th>Criteria met</th>
<th>Score</th>
<th>OQ result</th>
<th>Decision</th>
<th>Approver</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>admiral</td>
<td>1.x</td>
<td>core</td>
<td>all</td>
<td>0.xx</td>
<td>pass</td>
<td>approved</td>
<td>QA lead</td>
<td>2026-10-08</td>
</tr>
</tbody>
</table>
<p>Where a criterion is not met and the package is still needed, the decision says so and names the compensating control: “coverage not reported; all outputs depending on this package are dual-programmed.” That is a legitimate decision. Silence is not.</p>
</section>
<section id="what-about-packages-you-wrote" class="level2">
<h2 class="anchored" data-anchor-id="what-about-packages-you-wrote">What about packages you wrote?</h2>
<p>Internal migration packages (see <a href="../posts/sas-macros-to-r-functions.html">from SAS macros to R functions</a>) go through the same table. They will score low on adoption and high on tests if you built them properly. The compensating control is the dual-programming evidence they were built from. Keep them under version control, give them a NEWS file, and run <code>R CMD check</code> in CI so the evidence is generated, not assembled.</p>
</section>
<section id="how-often-to-re-qualify" class="level2">
<h2 class="anchored" data-anchor-id="how-often-to-re-qualify">How often to re-qualify</h2>
<ul>
<li>When the lockfile changes (that package and its changed dependencies).</li>
<li>When R is upgraded (full OQ).</li>
<li>Annually as a review of the decision records, even without changes, because maintenance status changes.</li>
</ul>
<p>The procedure above fits on two pages of an SOP and produces a folder a reviewer can open. Together with the <a href="../posts/r-validation-plan-regulatory-submissions.html">validation plan</a> and the <a href="../posts/traceability-matrix-statistical-programming.html">traceability matrix</a>, it is the documentation layer of the <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">SAS to R roadmap</a>.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>validation</category>
  <category>r-programming</category>
  <guid>https://rverseanalytics.com/posts/r-package-qualification-riskmetric.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Dual Programming SAS vs R: Numerical Tolerance Rules That Survive QC</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/sas-to-r-dual-programming-tolerance.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>Compare at three levels: <strong>dataset</strong> (structure, keys, values), <strong>TLF</strong> (every displayed cell) and <strong>log</strong> (warnings, record counts). Each level has its own tolerance.</li>
<li>Use <strong>absolute tolerance</strong> for bounded quantities (percentages, p-values, correlations) and <strong>relative tolerance</strong> for unbounded estimates (means, hazard ratios, confidence limits). Typical values: 1e-8 for values stored in full precision, half a unit in the last displayed digit for displayed values.</li>
<li>Every difference gets one of three labels: <strong>explained</strong> (convention, e.g.&nbsp;rounding), <strong>accepted</strong> (within tolerance) or <strong>deviation</strong> (investigate). The counts of each go into the validation summary.</li>
<li><code>all.equal()</code> and <code>waldo::compare()</code> cover most dataset comparisons; a small wrapper gives you a PROC COMPARE-style report.</li>
</ul>
</div>
<p>Dual programming is the backbone of a SAS-to-R migration: one programmer produces the output in R, the SAS original stands as the reference, and the two are compared programmatically. The comparison is only useful if the rules for “same” are written down before anyone looks at the numbers. This post is the set of rules we start from.</p>
<section id="three-comparison-levels" class="level2">
<h2 class="anchored" data-anchor-id="three-comparison-levels">Three comparison levels</h2>
<p><strong>1. Dataset level.</strong> Analysis datasets (ADaM or sponsor-specific) must match in variable names, types, labels where required, sort keys, record counts and values. Values are compared variable by variable with a tolerance appropriate to the type.</p>
<p><strong>2. TLF level.</strong> Tables are compared cell by cell on the <em>displayed</em> string after both sides apply the same rounding rule. Figures are compared on the data behind them (the plotted coordinates), not pixel by pixel.</p>
<p><strong>3. Log level.</strong> Record counts at each derivation step, number of subjects per treatment arm, and the absence of unexpected warnings. This catches silent filtering differences that value comparison alone can miss.</p>
</section>
<section id="choosing-tolerances" class="level2">
<h2 class="anchored" data-anchor-id="choosing-tolerances">Choosing tolerances</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Quantity</th>
<th>Tolerance type</th>
<th>Typical value</th>
<th>Why</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Counts, N, n</td>
<td>exact</td>
<td>0</td>
<td>Integers must match</td>
</tr>
<tr class="even">
<td>Percentages (stored)</td>
<td>absolute</td>
<td>1e-8</td>
<td>Bounded in [0, 100], full precision</td>
</tr>
<tr class="odd">
<td>Percentages (displayed)</td>
<td>displayed digit</td>
<td>0 after same rounding rule</td>
<td>See <a href="../posts/sas-vs-r-rounding-differences.html">rounding differences</a></td>
</tr>
<tr class="even">
<td>p-values</td>
<td>absolute</td>
<td>1e-6 stored; 0 displayed at 3-4 decimals</td>
<td>Algorithms differ (e.g.&nbsp;exact vs asymptotic)</td>
</tr>
<tr class="odd">
<td>Means, SDs, medians</td>
<td>relative</td>
<td>1e-8</td>
<td>Scale-free</td>
</tr>
<tr class="even">
<td>Hazard ratios, odds ratios, CIs</td>
<td>relative</td>
<td>1e-6</td>
<td>Iterative fitting; convergence criteria differ</td>
</tr>
<tr class="odd">
<td>Survival estimates, KM quantiles</td>
<td>absolute</td>
<td>1e-6; exact for event times</td>
<td>Step functions; ties handled identically</td>
</tr>
<tr class="even">
<td>Model coefficients (mixed models)</td>
<td>relative</td>
<td>1e-4</td>
<td>Optimisers differ; document algorithm</td>
</tr>
</tbody>
</table>
<p>The weaker tolerances for iterative models are not a weakness of R. SAS PROC MIXED and R <code>nlme</code>/<code>lme4</code> use different optimisers and stopping rules; agreement to four significant digits is what the mathematics gives you unless you tighten both sides.</p>
</section>
<section id="automating-the-dataset-comparison" class="level2">
<h2 class="anchored" data-anchor-id="automating-the-dataset-comparison">Automating the dataset comparison</h2>
<p><code>all.equal()</code> already handles numeric tolerance. A thin wrapper turns it into a report you can file:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">compare_ds <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(base, compare, keys, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tol =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-8</span>) {</span>
<span id="cb1-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(keys <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(base)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all</span>(keys <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(compare)))</span>
<span id="cb1-3">  base    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> base[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(order, base[keys]), , drop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>]</span>
<span id="cb1-4">  compare <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> compare[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(order, compare[keys]), , drop <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>]</span>
<span id="cb1-5">  vars <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">intersect</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(base), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(compare))</span>
<span id="cb1-6">  out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">lapply</span>(vars, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(v) {</span>
<span id="cb1-7">    b <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> base[[v]]; c <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> compare[[v]]</span>
<span id="cb1-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.numeric</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.numeric</span>(c)) {</span>
<span id="cb1-9">      diff <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(b <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> c); diff[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(c)] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb1-10">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"numeric"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_diff =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(diff <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> tol, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-11">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_abs_diff =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressWarnings</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(diff, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)))</span>
<span id="cb1-12">    } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb1-13">      same <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(c)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(c))</span>
<span id="cb1-14">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">variable =</span> v, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"character"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_diff =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>same, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_abs_diff =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb1-15">    }</span>
<span id="cb1-16">  })</span>
<span id="cb1-17">  res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">do.call</span>(rbind, out)</span>
<span id="cb1-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(res, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"only_in_base"</span>)    <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(base), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(compare))</span>
<span id="cb1-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(res, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"only_in_compare"</span>) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setdiff</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(compare), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(base))</span>
<span id="cb1-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(res, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_records"</span>)       <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(base), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compare =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(compare))</span>
<span id="cb1-21">  res</span>
<span id="cb1-22">}</span>
<span id="cb1-23"></span>
<span id="cb1-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Simulated SAS reference vs R output for an ADSL-like dataset</span></span>
<span id="cb1-25"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb1-26">sas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">USUBJID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-%03d"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRT01A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Placebo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug"</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb1-27">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AGE =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">54</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">61</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">47</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">66</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">49</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">63</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BMIBL =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span>
<span id="cb1-28">r   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sas</span>
<span id="cb1-29">r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>BMIBL[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>BMIBL[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>] <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-10</span>        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># floating-point noise: accepted</span></span>
<span id="cb1-30">r<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AGE[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span>                          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a real deviation</span></span>
<span id="cb1-31">res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">compare_ds</span>(sas, r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">keys =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USUBJID"</span>)</span>
<span id="cb1-32">res</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  variable      type n_diff max_abs_diff
1  USUBJID character      0           NA
2   TRT01A character      0           NA
3      AGE   numeric      1 1.000000e+00
4    BMIBL   numeric      0 9.999823e-11</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">attr</span>(res, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_records"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   base compare 
      8       8 </code></pre>
</div>
</div>
<p>For interactive work, <code>waldo::compare(sas, r, tolerance = 1e-8)</code> gives a coloured diff of exactly the same information, and <code>diffdf::diffdf()</code> produces a PROC COMPARE-like listing if you prefer a dedicated package.</p>
</section>
<section id="comparing-tlfs" class="level2">
<h2 class="anchored" data-anchor-id="comparing-tlfs">Comparing TLFs</h2>
<p>Treat each table as a long data frame: one row per (table, row label, column label, cell string). Produce that structure from both the SAS RTF/listing and the R output (we parse the SAS RTF or, better, ask for the SAS summary dataset behind the table). Then:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">tlf_sas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age, mean (SD)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Female, n (%)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BMI, mean (SD)"</span>),</span>
<span id="cb5-2">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cell =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"61.3 (8.2)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 (37.5)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"27.4 (2.9)"</span>))</span>
<span id="cb5-3">tlf_r   <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row =</span> tlf_sas<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>row, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug"</span>,</span>
<span id="cb5-4">                      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cell =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"61.3 (8.2)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"3 (37.5)"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"27.4 (3.0)"</span>))</span>
<span id="cb5-5">m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">merge</span>(tlf_sas, tlf_r, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">suffixes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_sas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"_r"</span>))</span>
<span id="cb5-6">m<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>match <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> m<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cell_sas <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> m<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>cell_r</span>
<span id="cb5-7">m</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>             row  col   cell_sas     cell_r match
1 Age, mean (SD) Drug 61.3 (8.2) 61.3 (8.2)  TRUE
2 BMI, mean (SD) Drug 27.4 (2.9) 27.4 (3.0) FALSE
3  Female, n (%) Drug   3 (37.5)   3 (37.5)  TRUE</code></pre>
</div>
</div>
<p>A mismatch in a displayed SD at the last digit is the classic case for the three-way classification below.</p>
</section>
<section id="classifying-differences" class="level2">
<h2 class="anchored" data-anchor-id="classifying-differences">Classifying differences</h2>
<p>Every non-match receives one label and one sentence:</p>
<ul>
<li><strong>Explained</strong>: a documented convention difference. Example: “SD displayed 2.9 vs 3.0; SAS rounds half away from zero, R rounds half to even; stored values agree to 1e-10.” These are closed by reference to the tolerance rules.</li>
<li><strong>Accepted</strong>: within the stated tolerance but not identical. Example: hazard ratio 0.7312 vs 0.7311 (relative difference 1.4e-4 under a 1e-4 rule for mixed models would <em>not</em> be accepted; under a Cox model rule of 1e-6 it is a deviation). The example shows why rules must be tied to the method.</li>
<li><strong>Deviation</strong>: outside tolerance or not explained. Each deviation has an owner, a root cause and a resolution (fix R, fix SAS reference, or amend the specification). Deviations are never closed by widening the tolerance after the fact.</li>
</ul>
<p>The validation summary reports the count of comparisons, the count in each class, and a listing of deviations with their resolutions. A table package with, say, 4,800 cells compared, 4,788 identical, 11 explained and 1 resolved deviation is a strong, honest result.</p>
</section>
<section id="common-sources-of-deviations-we-see" class="level2">
<h2 class="anchored" data-anchor-id="common-sources-of-deviations-we-see">Common sources of deviations we see</h2>
<ol type="1">
<li><strong>Population flags</strong> derived differently (e.g.&nbsp;ITT vs safety) because a SAS macro applied a filter that was not in the specification.</li>
<li><strong>Tie handling</strong> in Wilcoxon and log-rank tests; <code>survival::survdiff()</code> and <code>coin::wilcox_test()</code> can be configured to match SAS defaults.</li>
<li><strong>Type III vs Type I sums of squares</strong> in ANOVA. R’s <code>anova()</code> on <code>lm()</code> is sequential (Type I); use <code>car::Anova(type = 3)</code> with appropriate contrasts.</li>
<li><strong>Missing-value sorting and comparisons</strong>, where SAS treats missing as the smallest value. See <a href="../posts/missing-values-sas-vs-r.html">missing values in SAS vs R</a>.</li>
<li><strong>Date arithmetic</strong>, especially partial dates imputed by a macro.</li>
</ol>
<p>Each of these becomes a line in the equivalence report the first time it appears and a rule in the standards the second time.</p>
<p>Related: <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">SAS to R migration roadmap</a>, <a href="../posts/r-validation-plan-regulatory-submissions.html">how to write an R validation plan</a>, and our <a href="../sas-to-r.html">SAS to R service</a>.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>sas-to-r</category>
  <category>validation</category>
  <guid>https://rverseanalytics.com/posts/sas-to-r-dual-programming-tolerance.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Why SAS and R Round Differently (and How to Make Them Agree)</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/sas-vs-r-rounding-differences.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>SAS <code>ROUND(x, 0.1)</code> rounds half <strong>away from zero</strong>. R’s <code>round(x, 1)</code> follows IEC 60559 and rounds half <strong>to even</strong>, so <code>round(2.5)</code> is <code>2</code> and <code>round(0.125, 2)</code> depends on the binary representation.</li>
<li>In a typical TLF package the mismatch shows up in a handful of cells per table: percentages that end in exactly 5 at the next digit, means of integer data, and derived values such as BMI.</li>
<li>A six-line <code>round_sas()</code> helper reproduces SAS behaviour in R. Put it in your migration package, test it, and reference it in the numerical tolerance rules.</li>
<li>The right policy is usually “match SAS during parallel runs, then adopt one convention site-wide”, not “argue each cell”.</li>
</ul>
</div>
<p>If you have ever compared an R table to its SAS original and found a 12.5 % that became 12.4 %, you have met the single most common source of SAS-to-R “discrepancies”. It is not a bug on either side. The two systems implement different, equally defensible rounding rules, and they sit on top of binary floating-point arithmetic that makes some decimals impossible to represent exactly.</p>
<section id="the-two-rules" class="level2">
<h2 class="anchored" data-anchor-id="the-two-rules">The two rules</h2>
<p><strong>SAS</strong> <code>ROUND()</code> rounds half away from zero: 2.5 becomes 3, −2.5 becomes −3, 0.125 to two decimals becomes 0.13. Internally SAS also applies a “fuzz” so that values that are <em>almost</em> a half, due to floating-point noise, are treated as a half.</p>
<p><strong>R</strong> <code>round()</code> documents that it follows the IEC 60559 standard: “round half to even”. 2.5 becomes 2, 3.5 becomes 4. And because the function operates on the binary double, <code>round(0.125, 2)</code> is not guaranteed to be 0.12 or 0.13 by the rule alone; it depends on whether 0.125 is represented exactly (it is) and how the algorithm handles the scaled value.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.125</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.135</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.675</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">12.45</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-3">  x,</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r_round   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r_round2  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sprintf_1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%.1f"</span>, x)</span>
<span id="cb1-7">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       x r_round r_round2 sprintf_1
1  0.500     0.5     0.50       0.5
2  1.500     1.5     1.50       1.5
3  2.500     2.5     2.50       2.5
4 -2.500    -2.5    -2.50      -2.5
5  0.125     0.1     0.12       0.1
6  0.135     0.1     0.14       0.1
7  2.675     2.7     2.67       2.7
8 12.450    12.4    12.45      12.4</code></pre>
</div>
</div>
<p>Notice that <code>sprintf()</code> does not rescue you: it uses the C library’s formatting, which also rounds the binary value, and 2.675 prints as 2.67 because the stored double is slightly below 2.675.</p>
</section>
<section id="reproducing-sas-rounding-in-r" class="level2">
<h2 class="anchored" data-anchor-id="reproducing-sas-rounding-in-r">Reproducing SAS rounding in R</h2>
<p>The standard approach is to add a tiny tolerance and use <code>floor()</code> on the shifted absolute value, then restore the sign:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">round_sas <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">digits =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fuzz =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1e-9</span>) {</span>
<span id="cb3-2">  m <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">^</span>digits</span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sign</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">floor</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">abs</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> m <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> fuzz) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> m</span>
<span id="cb3-4">}</span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-7">  x,</span>
<span id="cb3-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r_round   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb3-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sas_round =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round_sas</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb3-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r_round2  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb3-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sas_round2 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round_sas</span>(x, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb3-12">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>       x r_round sas_round r_round2 sas_round2
1  0.500     0.5       0.5     0.50       0.50
2  1.500     1.5       1.5     1.50       1.50
3  2.500     2.5       2.5     2.50       2.50
4 -2.500    -2.5      -2.5    -2.50      -2.50
5  0.125     0.1       0.1     0.12       0.13
6  0.135     0.1       0.1     0.14       0.14
7  2.675     2.7       2.7     2.67       2.68
8 12.450    12.4      12.5    12.45      12.45</code></pre>
</div>
</div>
<p>The <code>fuzz</code> argument mirrors SAS’s behaviour of treating values within a tiny distance of a half as a half. Keep it small (1e-9 is conventional) and document it. Note that <code>janitor::round_half_up()</code> implements the same idea if you prefer a maintained dependency.</p>
</section>
<section id="where-it-bites-in-real-tables" class="level2">
<h2 class="anchored" data-anchor-id="where-it-bites-in-real-tables">Where it bites in real tables</h2>
<ul>
<li><strong>Percentages in frequency tables.</strong> <code>n / N * 100</code> with N = 8, 16, 40 or 80 produces exact halves at the first decimal. With N = 40, every odd count gives a <code>.5</code>.</li>
<li><strong>Means of integer scores.</strong> The mean of an even number of integers is often an exact half.</li>
<li><strong>Derived quantities</strong> such as BMI or change from baseline, where a two-stage rounding (derive, round, then summarise and round again) multiplies the opportunities for disagreement.</li>
<li><strong>Displayed vs stored values.</strong> SAS <code>PUT()</code> with a format rounds the displayed value; the dataset keeps full precision. If your R pipeline rounds <em>before</em> summarising, you will see differences that are not about rounding rules at all.</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-2">n <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">11</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">17</span>)</span>
<span id="cb5-3">N <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span></span>
<span id="cb5-4">pct <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb5-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(n, pct, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(pct, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sas =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round_sas</span>(pct, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">differ =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(pct, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round_sas</span>(pct, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>))</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   n  pct    r  sas differ
1  3  7.5  7.5  7.5  FALSE
2  5 12.5 12.5 12.5  FALSE
3  7 17.5 17.5 17.5  FALSE
4  9 22.5 22.5 22.5  FALSE
5 11 27.5 27.5 27.5  FALSE
6 13 32.5 32.5 32.5  FALSE
7 15 37.5 37.5 37.5  FALSE
8 17 42.5 42.5 42.5  FALSE</code></pre>
</div>
</div>
<p>Half of the odd counts disagree. In a 20-table package that is dozens of cells.</p>
</section>
<section id="what-to-write-in-the-tolerance-document" class="level2">
<h2 class="anchored" data-anchor-id="what-to-write-in-the-tolerance-document">What to write in the tolerance document</h2>
<p>Our migration tolerance rules usually contain three statements on rounding:</p>
<ol type="1">
<li><strong>Rule used.</strong> “Displayed values are rounded half away from zero using <code>round_sas()</code> (package <code>xyzmigr</code> v1.2.0), matching SAS <code>ROUND()</code>.” Or, after adoption, “rounded half to even using base R <code>round()</code>”; the point is that one rule is named.</li>
<li><strong>Order of operations.</strong> Rounding happens once, at the display layer. Intermediate derivations keep full precision. Two-stage rounding is a documented exception.</li>
<li><strong>Acceptance criterion.</strong> Numeric comparison of displayed values after applying the same rule on both sides must show zero differences; differences in the final displayed digit that are traceable to representation (e.g.&nbsp;2.675) are logged as <em>explained</em>, not as <em>deviations</em>.</li>
</ol>
<p>With those three lines, the reviewer sees a decision, not a surprise.</p>
</section>
<section id="should-you-keep-sas-rounding-forever" class="level2">
<h2 class="anchored" data-anchor-id="should-you-keep-sas-rounding-forever">Should you keep SAS rounding forever?</h2>
<p>During the parallel-run phase, yes: matching SAS is what makes the equivalence report clean. After adoption, many teams switch to R’s default and note the change in the SOP, because half-to-even is the IEEE standard and statistically unbiased. Either is acceptable; mixing both inside one submission is not.</p>
<p>Related reading: <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">SAS to R migration roadmap</a>, <a href="../posts/sas-to-r-dual-programming-tolerance.html">numerical tolerance rules for dual programming</a>, and the <a href="../sas-to-r.html">SAS to R service page</a>.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>sas-to-r</category>
  <category>validation</category>
  <guid>https://rverseanalytics.com/posts/sas-vs-r-rounding-differences.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>PROC FREQ, PROC MEANS and PROC UNIVARIATE in R: Side-by-Side Translations</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/proc-freq-proc-means-in-r.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li><code>PROC FREQ</code> → <code>table()</code>/<code>dplyr::count()</code>; watch the denominator (<code>MISSING</code> option) and the percent rounding.</li>
<li><code>PROC MEANS</code> → <code>dplyr::summarise()</code> with <code>across()</code>; <code>n</code> must count non-missing values and <code>NWAY</code> corresponds to grouping by all class variables.</li>
<li><code>PROC UNIVARIATE</code> percentiles default to SAS definition 5; R’s <code>quantile()</code> defaults to type 7. Use <code>type = 2</code> to match SAS PCTLDEF=5.</li>
<li>Write each translation once as a function, test it against a SAS export, and reuse it. See <a href="../posts/sas-macros-to-r-functions.html">from SAS macros to R functions</a>.</li>
</ul>
</div>
<p>Three procedures produce the bulk of a clinical TLF package: PROC FREQ for counts and percentages, PROC MEANS for descriptive statistics, and PROC UNIVARIATE for percentiles and distribution checks. Their R translations are short, but each has one or two options whose defaults differ and will show up in a parallel run. Here they are, side by side.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-3">adsl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">USUBJID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-%03d"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>),</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRT01A  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Placebo"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SEX     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M"</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">RACE    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"WHITE"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BLACK OR AFRICAN AMERICAN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ASIAN"</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">prob =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">05</span>)),</span>
<span id="cb1-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AGE     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">59</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)),</span>
<span id="cb1-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BMIBL   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">27</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-10">)</span></code></pre></div></div>
</div>
<section id="proc-freq" class="level2">
<h2 class="anchored" data-anchor-id="proc-freq">PROC FREQ</h2>
<pre class="sas"><code>proc freq data=adsl;
  tables TRT01A*RACE / nocol nopct;
run;</code></pre>
<p>The default excludes missing values from the table <em>and</em> from the percentage denominator. Adding <code>/ MISSING</code> includes them in both.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Default PROC FREQ: missings excluded from counts and denominator</span></span>
<span id="cb3-2">freq_tab <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(data, by, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_missing =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) {</span>
<span id="cb3-3">  d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (include_missing) data <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> data[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(data[[var]]), ]</span>
<span id="cb3-4">  d <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(.data[[by]], .data[[var]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(.data[[by]]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">N =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(n), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pct =</span> n <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> N <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb3-9">}</span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">freq_tab</span>(adsl, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRT01A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RACE"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 5
  TRT01A  RACE                          n     N   pct
  &lt;chr&gt;   &lt;chr&gt;                     &lt;int&gt; &lt;int&gt; &lt;dbl&gt;
1 Drug    ASIAN                         4    30  13.3
2 Drug    BLACK OR AFRICAN AMERICAN     7    30  23.3
3 Drug    WHITE                        19    30  63.3
4 Placebo ASIAN                         5    28  17.9
5 Placebo BLACK OR AFRICAN AMERICAN     9    28  32.1
6 Placebo WHITE                        14    28  50  </code></pre>
</div>
</div>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PROC FREQ ... / MISSING</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">freq_tab</span>(adsl, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRT01A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"RACE"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">include_missing =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 5
  TRT01A  RACE                          n     N   pct
  &lt;chr&gt;   &lt;chr&gt;                     &lt;int&gt; &lt;int&gt; &lt;dbl&gt;
1 Drug    ASIAN                         4    30 13.3 
2 Drug    BLACK OR AFRICAN AMERICAN     7    30 23.3 
3 Drug    WHITE                        19    30 63.3 
4 Placebo ASIAN                         5    30 16.7 
5 Placebo BLACK OR AFRICAN AMERICAN     9    30 30   
6 Placebo WHITE                        14    30 46.7 
7 Placebo &lt;NA&gt;                          2    30  6.67</code></pre>
</div>
</div>
<p>In a demography table the usual clinical convention is a third variant: the denominator is the number of subjects in the arm (from ADSL), missing is shown as its own row, and the percentage for missing is often suppressed. Encode that convention in the function, not in each program.</p>
<p><strong>Mismatch sources:</strong> the denominator choice above; row-percent vs column-percent (<code>PROC FREQ</code> prints both unless suppressed); and the display rounding of percentages, covered in <a href="../posts/sas-vs-r-rounding-differences.html">rounding differences between SAS and R</a>.</p>
</section>
<section id="proc-means" class="level2">
<h2 class="anchored" data-anchor-id="proc-means">PROC MEANS</h2>
<pre class="sas"><code>proc means data=adsl n mean std median min max maxdec=1 nway;
  class TRT01A;
  var AGE BMIBL;
run;</code></pre>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">adsl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(TRT01A) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(AGE, BMIBL), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb8-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n      =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(.x)),</span>
<span id="cb8-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean   =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb8-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd     =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb8-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">median =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">median</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb8-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">min    =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">min</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb8-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max    =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(.x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb8-10">  ), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.names =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{.col}_{.fn}"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"drop"</span>)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 13
  TRT01A  AGE_n AGE_mean AGE_sd AGE_median AGE_min AGE_max BMIBL_n BMIBL_mean
  &lt;chr&gt;   &lt;int&gt;    &lt;dbl&gt;  &lt;dbl&gt;      &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;   &lt;int&gt;      &lt;dbl&gt;
1 Drug       30     58.9   7.60       58        40      75      30       28.3
2 Placebo    30     61.8   7.35       62.5      46      76      30       26.0
# ℹ 4 more variables: BMIBL_sd &lt;dbl&gt;, BMIBL_median &lt;dbl&gt;, BMIBL_min &lt;dbl&gt;,
#   BMIBL_max &lt;dbl&gt;</code></pre>
</div>
</div>
<p><strong>Mismatch sources:</strong></p>
<ul>
<li><code>n</code> in PROC MEANS is the count of non-missing values; <code>dplyr::n()</code> counts rows. Use <code>sum(!is.na(x))</code>.</li>
<li><code>NWAY</code> means “only the highest-level combination of class variables”. Grouping by all class variables in R gives exactly that. Without <code>NWAY</code>, SAS also outputs the marginal totals (<code>_TYPE_</code> &lt; max), which in R you add with a second summarise over fewer groups or <code>add_overall()</code>-style helpers.</li>
<li><code>MAXDEC=</code> is display only. Do not round before summarising.</li>
<li>SAS <code>STD</code> and R <code>sd()</code> both use the n−1 denominator; <code>VARDEF=</code> changes that in SAS and must be matched if used.</li>
</ul>
</section>
<section id="proc-univariate-and-percentiles" class="level2">
<h2 class="anchored" data-anchor-id="proc-univariate-and-percentiles">PROC UNIVARIATE and percentiles</h2>
<pre class="sas"><code>proc univariate data=adsl;
  class TRT01A;
  var AGE;
  output out=pct pctlpts=25 50 75 pctlpre=P;
run;</code></pre>
<p>SAS’s default percentile definition is <code>PCTLDEF=5</code> (empirical distribution function with averaging). R’s <code>quantile()</code> default is type 7 (linear interpolation), and the two differ for small groups.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> adsl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>AGE[adsl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>TRT01A <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug"</span>]</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbind</span>(</span>
<span id="cb11-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">r_default_type7 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>),</span>
<span id="cb11-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sas_pctldef5    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(x, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb11-5">)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                25% 50%   75%
r_default_type7  54  58 64.75
sas_pctldef5     54  58 65.00</code></pre>
</div>
</div>
<p>Hyndman and Fan’s type 2 is the SAS default (PCTLDEF=5); type 6 corresponds to PCTLDEF=4 and type 3 to PCTLDEF=2. Put the chosen type in the standards document and in the function signature:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1">pctl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">probs =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(.<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sas_def =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>) {</span>
<span id="cb13-2">  type <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">1</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">3</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">4</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">5</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(sas_def)]</span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</span>(x, probs, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb13-4">}</span>
<span id="cb13-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pctl</span>(x)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 54 58 65</code></pre>
</div>
</div>
<p>Medians agree between type 2 and type 7 for odd <em>n</em> and usually for even <em>n</em>; quartiles are where differences appear. The median of a Q1-Q3 display in a demography table is therefore safe, but the quartiles themselves need the matching type.</p>
<p><strong>Other PROC UNIVARIATE outputs:</strong> skewness and kurtosis use the sample-adjusted formulas in SAS (<code>VARDEF=DF</code>); <code>e1071::skewness(type = 2)</code> and <code>kurtosis(type = 2)</code> match them. Normality tests: Shapiro-Wilk is <code>shapiro.test()</code>; SAS reports it only for n ≤ 2000, R up to 5000.</p>
</section>
<section id="a-translation-table-to-keep" class="level2">
<h2 class="anchored" data-anchor-id="a-translation-table-to-keep">A translation table to keep</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>SAS</th>
<th>R</th>
<th>Watch for</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>PROC FREQ</code></td>
<td><code>count()</code> + <code>mutate(pct = n/sum(n)*100)</code></td>
<td>Denominator and MISSING option</td>
</tr>
<tr class="even">
<td><code>PROC FREQ ... / CHISQ</code></td>
<td><code>chisq.test(correct = FALSE)</code></td>
<td>SAS does not apply Yates’ correction by default</td>
</tr>
<tr class="odd">
<td><code>PROC FREQ ... / FISHER</code></td>
<td><code>fisher.test()</code></td>
<td>Two-sided p agrees; one-sided definitions differ</td>
</tr>
<tr class="even">
<td><code>PROC MEANS</code></td>
<td><code>summarise(across(...))</code></td>
<td><code>n</code> = non-missing; NWAY vs marginals</td>
</tr>
<tr class="odd">
<td><code>PROC UNIVARIATE</code> percentiles</td>
<td><code>quantile(type = 2)</code></td>
<td>PCTLDEF default 5</td>
</tr>
<tr class="even">
<td><code>PROC UNIVARIATE ... NORMAL</code></td>
<td><code>shapiro.test()</code></td>
<td>Sample-size limits</td>
</tr>
<tr class="odd">
<td><code>PROC SORT NODUPKEY</code></td>
<td><code>distinct(..., .keep_all = TRUE)</code></td>
<td>Keeps first occurrence in both</td>
</tr>
<tr class="even">
<td><code>PROC TRANSPOSE</code></td>
<td><code>tidyr::pivot_wider()</code>/<code>pivot_longer()</code></td>
<td>Variable naming of transposed columns</td>
</tr>
</tbody>
</table>
<p>Each row of this table is one function in a migration package and one test against a SAS export. That is the whole method: translate once, test once, reuse everywhere. For the full programme see the <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">five-phase migration roadmap</a> and the <a href="../sas-to-r.html">PROC-to-R equivalence map</a>.</p>


</section>

 ]]></description>
  <category>sas-to-r</category>
  <category>r-programming</category>
  <category>clinical</category>
  <guid>https://rverseanalytics.com/posts/proc-freq-proc-means-in-r.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>R vs SAS for Clinical Trials in 2026: An Honest Comparison for Decision-Makers</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/r-vs-sas-clinical-trials-2026.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li><strong>Regulatory acceptance is no longer the differentiator.</strong> The FDA requires no specific software and has received fully R-based pilot submissions; the EMA position is equivalent. Both languages are acceptable with a controlled process.</li>
<li><strong>The validation burden is similar in kind and different in ownership.</strong> SAS users rely partly on the vendor’s quality system; R users document their own environment, package and QC controls. The R evidence tends to be more transparent once set up.</li>
<li><strong>The ecosystem gap has closed for ADaM and TLFs</strong> (admiral, rtables, tern, gtsummary, xportr). It has not fully closed for some legacy sponsor macro libraries, which is where migration effort goes.</li>
<li>The realistic choice for most CROs is <strong>coexistence with a managed migration</strong>, not a switch-over date.</li>
</ul>
</div>
<p>Programmers argue about syntax. Decision-makers need to know what changes for the business, the quality system and the sponsor relationship. This comparison tries to be fair to both tools as they stand in 2026.</p>
<section id="regulatory-acceptance" class="level2">
<h2 class="anchored" data-anchor-id="regulatory-acceptance">Regulatory acceptance</h2>
<p><strong>SAS:</strong> decades of precedent; reviewers are familiar with its outputs and transport files.</p>
<p><strong>R:</strong> the FDA’s Statistical Software Clarifying Statement has long said no specific software is required. The R Consortium’s R Submissions Working Group has completed multiple pilot submissions, including fully R-based ADaM and TLF packages and a Shiny application run inside the agency’s environment. Reviewers now see R code in real submissions. Details in <a href="../posts/fda-r-submissions-2026.html">Is R accepted by the FDA?</a>.</p>
<p><strong>Verdict:</strong> both acceptable. Acceptance depends on process evidence, not the language.</p>
</section>
<section id="validation" class="level2">
<h2 class="anchored" data-anchor-id="validation">Validation</h2>
<p><strong>SAS:</strong> the vendor provides installation qualification tooling and a quality statement; sponsors still own user-level validation of macros and programs.</p>
<p><strong>R:</strong> the organisation owns environment qualification (renv, test-suite runs), package qualification (riskmetric, decision records) and process controls. See <a href="../posts/r-validation-plan-regulatory-submissions.html">how to write an R validation plan</a> and <a href="../posts/r-package-qualification-riskmetric.html">qualifying R packages</a>.</p>
<p><strong>Verdict:</strong> more set-up work in R, after which the evidence is explicit and automated. Teams that treated SAS as “validated by default” sometimes discover their macro libraries were never qualified to the standard they now apply to R. That is a finding about the past, not about R.</p>
</section>
<section id="ecosystem-for-clinical-work" class="level2">
<h2 class="anchored" data-anchor-id="ecosystem-for-clinical-work">Ecosystem for clinical work</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th>Need</th>
<th>SAS</th>
<th>R (2026)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>SDTM</td>
<td>Established sponsor/CRO macros</td>
<td>sdtm.oak and sponsor code; less mature than ADaM tooling</td>
</tr>
<tr class="even">
<td>ADaM</td>
<td>PROC/macro libraries</td>
<td>admiral family, metacore/metatools for metadata-driven derivations</td>
</tr>
<tr class="odd">
<td>TLFs</td>
<td>PROC REPORT, ODS, sponsor macros</td>
<td>rtables + tern (Roche-origin), gtsummary, Tplyr; RTF/DOCX output</td>
</tr>
<tr class="even">
<td>Transport files</td>
<td>PROC CPORT/XPORT</td>
<td>xportr with validation against specs</td>
</tr>
<tr class="odd">
<td>Define.xml</td>
<td>Vendor tools</td>
<td>Generation from metadata in R, often paired with vendor tools</td>
</tr>
<tr class="even">
<td>Survival, mixed models</td>
<td>PROC LIFETEST/PHREG/MIXED</td>
<td>survival, nlme/lme4, mmrm (designed for regulatory use)</td>
</tr>
<tr class="odd">
<td>Bayesian and adaptive designs</td>
<td>Limited</td>
<td>Strong (brms, rstan, gsDesign, rpact)</td>
</tr>
<tr class="even">
<td>Interactive review</td>
<td>Limited</td>
<td>Shiny, including the FDA pilot precedent</td>
</tr>
</tbody>
</table>
<p><strong>Verdict:</strong> parity for the core submission deliverables; R ahead for methods and interactivity; SAS ahead wherever a sponsor’s institutional macro library is the de facto standard.</p>
</section>
<section id="equivalence-of-results" class="level2">
<h2 class="anchored" data-anchor-id="equivalence-of-results">Equivalence of results</h2>
<p>The two systems agree to numerical precision for almost everything when the same method is specified. Where they differ, the cause is a default rather than an error: <a href="../posts/sas-vs-r-rounding-differences.html">rounding conventions</a>, <a href="../posts/missing-values-sas-vs-r.html">missing-value semantics</a>, percentile definitions, sums-of-squares types, tie handling. Every one of these is resolvable and documentable; the <a href="../posts/sas-to-r-dual-programming-tolerance.html">tolerance rules</a> exist for that purpose.</p>
<p><strong>Verdict:</strong> no statistical reason to prefer either; a process reason to document defaults.</p>
</section>
<section id="talent-and-training" class="level2">
<h2 class="anchored" data-anchor-id="talent-and-training">Talent and training</h2>
<p><strong>SAS:</strong> a shrinking but experienced pool, concentrated in pharma and CROs; new graduates rarely learn it first.</p>
<p><strong>R:</strong> the default in statistics departments and increasingly in biostatistics programmes; the pharmaverse community trains clinical-specific skills. Experienced SAS programmers typically become productive in R within months when trained on their own studies rather than on toy examples.</p>
<p><strong>Verdict:</strong> the long-run talent trend favours R; the short-run cost is retraining a team that knows your macros.</p>
</section>
<section id="cost" class="level2">
<h2 class="anchored" data-anchor-id="cost">Cost</h2>
<p><strong>SAS:</strong> licensing is a significant recurring line, plus infrastructure.</p>
<p><strong>R:</strong> no licence; costs move to qualification effort, internal package maintenance, and optionally commercial support or platforms (Posit, cloud environments). Migration itself is a one-off project cost whose size depends on the macro library; see the <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">five-phase roadmap</a>.</p>
<p><strong>Verdict:</strong> R is cheaper at steady state for most organisations; the transition has a real price that should be estimated from an inventory, not assumed.</p>
</section>
<section id="risks-on-each-side" class="level2">
<h2 class="anchored" data-anchor-id="risks-on-each-side">Risks on each side</h2>
<p><strong>Risk of migrating:</strong> disruption to live deliverables if the pilot is not isolated; inconsistent conversions if standards are not set first; a validation gap if documentation is left to the end. Each is mitigated by sequencing, which is what the roadmap is for.</p>
<p><strong>Risk of not migrating:</strong> rising licence and talent costs; increasing sponsor requests for R deliverables or R-literate review; dependence on a macro library whose authors have left. These risks grow quietly.</p>
</section>
<section id="what-we-recommend-to-most-cros" class="level2">
<h2 class="anchored" data-anchor-id="what-we-recommend-to-most-cros">What we recommend to most CROs</h2>
<ol type="1">
<li>Run a <strong>Phase 1 assessment</strong>: inventory, risk classification, macro reuse map. Two to six weeks, fixed scope. The findings replace speculation about cost.</li>
<li>Pilot on a <strong>closed study</strong>, parallel-run against SAS, with tolerance rules decided up front.</li>
<li>Build the <strong>package layer</strong> and the validation documentation once, then move studies in order of reuse.</li>
<li>Keep SAS where it is cheap to keep (rarely run legacy programs) and retire it where it is expensive (shared macros under active maintenance).</li>
<li>Prepare the <strong>sponsor and regulatory narrative</strong> early, grounded in the FDA statement, the pilot submissions and your own evidence.</li>
</ol>
<p>Coexistence is normal. A managed migration is a programme, not a leap. If you would like a second opinion on an existing plan or help with Phase 1, the <a href="../sas-to-r.html">SAS to R migration page</a> explains how we work and how to reach us.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>sas-to-r</category>
  <guid>https://rverseanalytics.com/posts/r-vs-sas-clinical-trials-2026.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Write an R Validation Plan for Regulatory Submissions</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/r-validation-plan-regulatory-submissions.html</link>
  <description><![CDATA[ 




<div class="takeaways">
<ul>
<li>Regulators do not validate software; <strong>you validate your use of it</strong>. An R validation plan documents the intended use, the risks, the controls and the evidence.</li>
<li>The plan has eight parts: scope, roles, risk assessment, environment qualification (IQ/OQ), package qualification, process controls (dual programming, code review), traceability, and deviation management ending in a validation summary report.</li>
<li>Pin the environment with <strong>renv</strong>, qualify it by running package test suites, assess packages with <strong>riskmetric</strong> against written acceptance criteria, and record <code>sessionInfo()</code> in every production run.</li>
<li>Keep the plan short enough to be read and the evidence large enough to be audited. Most plans are 15-25 pages; the evidence pack is whatever your pipeline produces automatically.</li>
</ul>
</div>
<p>“Is R validated?” is the first question a sponsor’s QA group asks and the one most often answered badly. The honest answer is that no statistical software is validated in the abstract. The FDA’s position, repeated in its Statistical Software Clarifying Statement and demonstrated by the R Consortium submission pilots, is that sponsors must be able to show their analyses are reproducible, documented and controlled. A validation plan is where you show it. This is the structure we use.</p>
<section id="scope-and-intended-use" class="level2">
<h2 class="anchored" data-anchor-id="scope-and-intended-use">1. Scope and intended use</h2>
<p>State what R will be used for and what it will not. Typical scope: ADaM dataset derivation, TLF production and statistical analysis for named studies or for a programme. Out of scope might be data management or SDTM mapping if those remain elsewhere. Naming the intended use makes the risk assessment concrete.</p>
</section>
<section id="roles-and-responsibilities" class="level2">
<h2 class="anchored" data-anchor-id="roles-and-responsibilities">2. Roles and responsibilities</h2>
<p>Who owns the environment, who approves packages, who performs independent QC, who signs the summary. In a migration this section also names the SAS reference programmer for the parallel-run phase.</p>
</section>
<section id="risk-assessment" class="level2">
<h2 class="anchored" data-anchor-id="risk-assessment">3. Risk assessment</h2>
<p>A short table, usually one page: for each use (dataset derivation, descriptive tables, inferential analysis, figures), the impact of an error (patient safety, regulatory decision, operational), the likelihood given controls, and the resulting control level. High-impact uses get full dual programming; low-impact ones may get code review plus spot checks. Writing this down is what allows you to <em>not</em> dual-program a listing and still be compliant.</p>
</section>
<section id="environment-qualification" class="level2">
<h2 class="anchored" data-anchor-id="environment-qualification">4. Environment qualification</h2>
<p><strong>Installation qualification (IQ).</strong> Record R version, operating system, compiler, BLAS/LAPACK, and the exact package set. <code>renv</code> produces the lockfile; store it with the study.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1">renv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">init</span>()                   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># once per project</span></span>
<span id="cb1-2">renv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">snapshot</span>()               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># after approving the package set</span></span>
<span id="cb1-3">renv<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">restore</span>()                <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># on any new machine: identical environment</span></span></code></pre></div></div>
</div>
<p><strong>Operational qualification (OQ).</strong> Prove the installation behaves as the package authors intend by running their test suites in your environment and filing the results:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">tools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">testInstalledPackage</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"survival"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">types =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tests"</span>)</span>
<span id="cb2-2">tools<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">testInstalledPackage</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admiral"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">types =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"tests"</span>)</span></code></pre></div></div>
</div>
<p>Base R itself ships regression tests (<code>make check-all</code> at build time, or <code>tools::testInstalledBasic("both")</code>). Attach the logs to the IQ/OQ record. Re-qualify whenever the lockfile changes.</p>
<p><strong>Session evidence.</strong> Every production run writes <code>sessionInfo()</code> (or <code>sessioninfo::session_info()</code>) and the lockfile hash to the log. This is what lets an auditor tie an output to an environment three years later.</p>
</section>
<section id="package-qualification" class="level2">
<h2 class="anchored" data-anchor-id="package-qualification">5. Package qualification</h2>
<p>Packages are the part reviewers worry about. The plan defines acceptance criteria and a procedure:</p>
<ul>
<li><strong>Criteria</strong> might be: on CRAN or Bioconductor or an approved internal repository; documented exports; a test suite with meaningful coverage; active maintenance (release or commit within 24 months); a licence compatible with use.</li>
<li><strong>Assessment</strong> uses <code>riskmetric</code> to compute standard metrics (documentation, tests, maintenance, community usage) and store the scores:</li>
</ul>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(riskmetric)</span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pkg_ref</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"admiral"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rtables"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gtsummary"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"survival"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pkg_assess</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pkg_score</span>()</span></code></pre></div></div>
</div>
<ul>
<li><strong>Decision record.</strong> For each package: scores, the criteria met, the approver and the date. Low-scoring packages can still be approved with additional controls (for example, dual programming of every output that depends on them), but the decision is written.</li>
</ul>
<p>The pharmaverse packages (admiral, rtables, tern, xportr, metacore) publish extensive tests and are used by multiple sponsors, which is exactly the evidence the criteria ask for.</p>
</section>
<section id="process-controls" class="level2">
<h2 class="anchored" data-anchor-id="process-controls">6. Process controls</h2>
<p><strong>Programming standards.</strong> A short document: file layout, naming, how specifications are referenced in code, how derivations are commented, when functions must be packaged. See <a href="../posts/sas-macros-to-r-functions.html">turning SAS macros into R functions</a>.</p>
<p><strong>Independent QC.</strong> Dual programming for everything the risk assessment marks as high impact, with the <a href="../posts/sas-to-r-dual-programming-tolerance.html">numerical tolerance rules</a> referenced here. Code review for everything else.</p>
<p><strong>Version control.</strong> Git, with the production branch protected and each delivery tagged. The tag, the lockfile hash and the output checksums go in the delivery note.</p>
</section>
<section id="traceability" class="level2">
<h2 class="anchored" data-anchor-id="traceability">7. Traceability</h2>
<p>A traceability matrix links every requirement to its implementation and evidence: SAP section → specification item → program/function → output → QC record. In practice this is a table you can generate from metadata rather than maintain by hand. Read <a href="../posts/traceability-matrix-statistical-programming.html">building a traceability matrix for statistical programming</a>.</p>
</section>
<section id="deviations-and-the-summary-report" class="level2">
<h2 class="anchored" data-anchor-id="deviations-and-the-summary-report">8. Deviations and the summary report</h2>
<p>Define what counts as a deviation (a failed comparison outside tolerance, a package used outside approval, a run on an unqualified environment), how it is logged, who resolves it, and how resolution is verified. The <strong>validation summary report</strong> closes the plan: what was planned, what was executed, counts of comparisons and deviations, open items, and a signed statement that the system is fit for its intended use.</p>
</section>
<section id="how-long-does-this-take" class="level2">
<h2 class="anchored" data-anchor-id="how-long-does-this-take">How long does this take?</h2>
<p>For a team migrating a first study, the plan itself is one to two weeks of writing with QA review. Environment and package qualification is largely automated once set up and takes days. The ongoing cost is the dual programming you would be doing anyway. The output is a document set your QA group can file and a sponsor can audit. If you are starting from scratch, our <a href="../sas-to-r.html">SAS to R migration service</a> delivers the plan, the templates and the first evidence pack as Phase 4 of the <a href="../posts/sas-to-r-migration-roadmap-5-phases.html">five-phase roadmap</a>.</p>
<p>Background reading: <a href="../posts/fda-r-submissions-2026.html">Is R accepted by the FDA?</a> and <a href="../posts/r-package-qualification-riskmetric.html">qualifying R packages with riskmetric</a>.</p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>validation</category>
  <category>sas-to-r</category>
  <guid>https://rverseanalytics.com/posts/r-validation-plan-regulatory-submissions.html</guid>
  <pubDate>Wed, 07 Oct 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>ADaM Derivations in R with admiral: a Mini ADSL, End to End</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/adam-derivation-admiral-r.html</link>
  <description><![CDATA[ 




<p>If your statistical programming lives in the CDISC world, the question is no longer <em>can</em> R do ADaM — the <a href="https://pharmaverse.org">pharmaverse</a> exists precisely for this, and its flagship package <strong>admiral</strong> is developed openly by Roche, GSK and friends. The real question is what a derivation actually looks like. Here is a miniature but honest ADSL flow: SDTM-shaped inputs in, analysis-ready dataset out, every variable traceable.</p>
<section id="sdtm-shaped-inputs" class="level2">
<h2 class="anchored" data-anchor-id="sdtm-shaped-inputs">SDTM-shaped inputs</h2>
<p>In production these come from your SDTM datasets (DM, EX, DS). For a self-contained example we synthesise tiny versions with the same structure:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(admiral)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(lubridate)</span>
<span id="cb1-4"></span>
<span id="cb1-5">dm <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb1-6">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>STUDYID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>USUBJID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>AGE, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>SEX, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>ARM,       <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>COUNTRY,</span>
<span id="cb1-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-101"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">64</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug A"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TUR"</span>,</span>
<span id="cb1-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-102"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">71</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug A"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TUR"</span>,</span>
<span id="cb1-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-103"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">58</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Placebo"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DEU"</span>,</span>
<span id="cb1-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-104"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"M"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Placebo"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DEU"</span>,</span>
<span id="cb1-11">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-105"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">47</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug A"</span>,   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GBR"</span></span>
<span id="cb1-12">)</span>
<span id="cb1-13"></span>
<span id="cb1-14">ex <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb1-15">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>STUDYID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>USUBJID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>EXSTDTC,     <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>EXENDTC,</span>
<span id="cb1-16">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-101"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-01-10"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-06-30"</span>,</span>
<span id="cb1-17">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-102"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-01-14"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-03-02"</span>,</span>
<span id="cb1-18">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-103"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-01-11"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-06-28"</span>,</span>
<span id="cb1-19">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"XYZ-1"</span>,  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-104"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-01-20"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2026-05-15"</span></span>
<span id="cb1-20">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 01-105 randomised but never dosed - watch what happens to her flags</span></span>
<span id="cb1-21">)</span></code></pre></div></div>
</div>
</section>
<section id="derive-treatment-dates-and-flags-with-admiral" class="level2">
<h2 class="anchored" data-anchor-id="derive-treatment-dates-and-flags-with-admiral">Derive treatment dates and flags with admiral</h2>
<p><code>derive_vars_merged()</code> is admiral’s workhorse: merge a summarised source onto the target with explicit semantics, no silent many-to-many surprises.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">adsl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> dm <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">derive_vars_merged</span>(</span>
<span id="cb2-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dataset_add =</span> ex,</span>
<span id="cb2-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by_vars     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exprs</span>(STUDYID, USUBJID),</span>
<span id="cb2-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exprs</span>(EXSTDTC),</span>
<span id="cb2-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mode        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"first"</span>,</span>
<span id="cb2-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_vars    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exprs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRTSDTC =</span> EXSTDTC)</span>
<span id="cb2-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">derive_vars_merged</span>(</span>
<span id="cb2-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dataset_add =</span> ex,</span>
<span id="cb2-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by_vars     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exprs</span>(STUDYID, USUBJID),</span>
<span id="cb2-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">order       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exprs</span>(EXENDTC),</span>
<span id="cb2-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mode        =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"last"</span>,</span>
<span id="cb2-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_vars    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">exprs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRTEDTC =</span> EXENDTC)</span>
<span id="cb2-15">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">derive_vars_dt</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_vars_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRTS"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dtc =</span> TRTSDTC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">derive_vars_dt</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_vars_prefix =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TRTE"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dtc =</span> TRTEDTC) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-18">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb2-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">SAFFL  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(TRTSDT), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"N"</span>),   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># safety population: treated</span></span>
<span id="cb2-20">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ITTFL  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>,                                  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ITT: all randomised</span></span>
<span id="cb2-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRT01P =</span> ARM,</span>
<span id="cb2-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRT01A =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(SAFFL <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Y"</span>, ARM, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>)</span>
<span id="cb2-23">  )</span>
<span id="cb2-24"></span>
<span id="cb2-25">adsl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(USUBJID, TRT01P, TRTSDT, TRTEDT, SAFFL, ITTFL)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 6
  USUBJID TRT01P  TRTSDT     TRTEDT     SAFFL ITTFL
  &lt;chr&gt;   &lt;chr&gt;   &lt;date&gt;     &lt;date&gt;     &lt;chr&gt; &lt;chr&gt;
1 01-101  Drug A  2026-01-10 2026-06-30 Y     Y    
2 01-102  Drug A  2026-01-14 2026-03-02 Y     Y    
3 01-103  Placebo 2026-01-11 2026-06-28 Y     Y    
4 01-104  Placebo 2026-01-20 2026-05-15 Y     Y    
5 01-105  Drug A  NA         NA         N     Y    </code></pre>
</div>
</div>
<p>Subject 01-105 shows why flags matter: randomised, so <code>ITTFL = "Y"</code> — but never dosed, so <code>SAFFL = "N"</code> and she has no actual treatment. An intention-to-treat table includes her; a safety table must not. The estimand language of ICH E9(R1) starts exactly here.</p>
</section>
<section id="age-groups-and-treatment-duration" class="level2">
<h2 class="anchored" data-anchor-id="age-groups-and-treatment-duration">Age groups and treatment duration</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">adsl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> adsl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb4-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AGEGR1 =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(AGE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">65</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&lt;65"</span>, AGE <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">75</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"65-&lt;75"</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"&gt;=75"</span>),</span>
<span id="cb4-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">TRTDURD =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.integer</span>(TRTEDT <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> TRTSDT) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-5">  )</span>
<span id="cb4-6"></span>
<span id="cb4-7">adsl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(USUBJID, AGE, AGEGR1, TRTDURD)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 4
  USUBJID   AGE AGEGR1 TRTDURD
  &lt;chr&gt;   &lt;dbl&gt; &lt;chr&gt;    &lt;dbl&gt;
1 01-101     64 &lt;65        172
2 01-102     71 65-&lt;75      48
3 01-103     58 &lt;65        169
4 01-104     80 &gt;=75       116
5 01-105     47 &lt;65         NA</code></pre>
</div>
</div>
</section>
<section id="why-this-beats-a-hand-rolled-script" class="level2">
<h2 class="anchored" data-anchor-id="why-this-beats-a-hand-rolled-script">Why this beats a hand-rolled script</h2>
<ul>
<li><strong>Traceability</strong> — each admiral call names its source, keys and selection rule; the derivation <em>is</em> the documentation your define.xml points to.</li>
<li><strong>Convention over improvisation</strong> — variable names (<code>TRT01P</code>, <code>SAFFL</code>, <code>AGEGR1</code>) follow ADaM, so downstream <a href="../posts/tlf-tables-r-gtsummary-rtables.html">TLF code</a> and reviewers both know what they are looking at.</li>
<li><strong>Testable</strong> — derivations are functions, so they can carry unit tests; your QC programmer reruns them against the spec instead of eyeballing a DATA step.</li>
</ul>
<p>In production you would add DTHFL/DTHDT from DS, last-known-alive dates, and time-to-event variables via <code>derive_vars_duration()</code> — the grammar stays identical.</p>
<hr>
<p><em>Need ADaM-style datasets or their QC done in R? That’s part of our <a href="../for-cros.html">CRO services</a> — or see the <a href="../sas-to-r.html">SAS to R migration guide</a> if the whole pipeline is moving.</em></p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>cdisc</category>
  <category>biostatistics</category>
  <guid>https://rverseanalytics.com/posts/adam-derivation-admiral-r.html</guid>
  <pubDate>Fri, 07 Aug 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Is R Accepted by the FDA? The State of R-Based Submissions</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/fda-r-submissions-2026.html</link>
  <description><![CDATA[ 




<p>“Regulators only accept SAS” was already folklore a decade ago — the FDA’s own Statistical Software Clarifying Statement says the agency does not require any specific software — but folklore with a licence renewal attached tends to live long. What finally changed the conversation was the <strong>R Consortium R Submissions Working Group</strong>: a series of pilot submissions, prepared openly and reviewed by the FDA, demonstrating the full path in practice.</p>
<section id="what-the-pilots-showed" class="level2">
<h2 class="anchored" data-anchor-id="what-the-pilots-showed">What the pilots showed</h2>
<p>Across the pilots, sponsor-style teams assembled and the FDA received and reviewed submission packages in which:</p>
<ul>
<li>analysis datasets and <strong>TLFs were produced entirely in R</strong>, with the R code included in the package;</li>
<li>a <strong>Shiny application</strong> was submitted and successfully executed in the agency’s own environment;</li>
<li>container- and webassembly-based delivery of interactive components was exercised in later pilots.</li>
</ul>
<p>The point was never that R produces prettier p-values. It was operational: the eCTD structure, the code delivery format, the reviewer’s ability to re-run the analysis — all of it works with R. The “is it accepted?” question is settled; what remains is engineering discipline.</p>
</section>
<section id="but-is-r-validated" class="level2">
<h2 class="anchored" data-anchor-id="but-is-r-validated">“But is R validated?”</h2>
<p>Validation is a property of <strong>your process</strong>, not of a software licence. SAS installations are validated by their owners’ quality systems — the same logic applies to R:</p>
<ul>
<li><strong>Pin the environment.</strong> {renv} locks package versions per project; the lockfile ships with the deliverable, and <code>sessionInfo()</code> is captured in every run log.</li>
<li><strong>Assess package risk.</strong> {riskmetric} scores packages on documentation, test coverage, maintenance and community adoption — the evidence base for your package acceptance SOP. Core packages (base R, survival, and the pharmaverse tools) publish extensive test suites.</li>
<li><strong>Qualify the installation.</strong> Run the package test suites in your environment and file the results — the R equivalent of an installation/operational qualification.</li>
<li><strong>Double-program the outputs.</strong> Independent reproduction from the spec remains the QC gold standard regardless of language — we describe the workflow on our <a href="../for-cros.html">CRO services page</a>.</li>
</ul>
<p>A version-pinned, test-covered, double-programmed R pipeline produces <em>stronger</em> validation evidence than an unversioned macro library that has “always been there”.</p>
</section>
<section id="the-ecosystem-that-makes-it-practical" class="level2">
<h2 class="anchored" data-anchor-id="the-ecosystem-that-makes-it-practical">The ecosystem that makes it practical</h2>
<p>The pharmaverse — an industry collaboration including Roche, GSK, J&amp;J, Novartis and others — now covers the submission pipeline end to end: <strong>admiral</strong> for <a href="../posts/adam-derivation-admiral-r.html">ADaM derivations</a>, <strong>rtables/tern</strong> for <a href="../posts/tlf-tables-r-gtsummary-rtables.html">CSR-grade TLFs</a>, <strong>xportr</strong> for transport files with define.xml-ready metadata, and {mmrm} for the standard longitudinal model. These are not hobby packages; they are the internal tooling of the industry, published.</p>
</section>
<section id="what-this-means-if-you-run-a-cro" class="level2">
<h2 class="anchored" data-anchor-id="what-this-means-if-you-run-a-cro">What this means if you run a CRO</h2>
<p>The cost case for <a href="../sas-to-r.html">SAS-to-R migration</a> was always obvious; the pilots removed the regulatory excuse for postponing it. The teams that migrate carefully — pilot study, parallel run, documented equivalence — get the licence savings <em>and</em> a more auditable pipeline. The teams that wait are now competing against them on price.</p>
<hr>
<p><em>Weighing this up for your own shop? We run fixed-scope pilot migrations and R-based TLF production — <a href="../contact.html">talk to us</a>, or start with the <a href="../sas-to-r.html">migration guide</a>.</em></p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>cdisc</category>
  <guid>https://rverseanalytics.com/posts/fda-r-submissions-2026.html</guid>
  <pubDate>Fri, 07 Aug 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Clinical TLFs in R: gtsummary and rtables Side by Side</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/tlf-tables-r-gtsummary-rtables.html</link>
  <description><![CDATA[ 




<p>Tables, listings and figures are where a clinical study report is won or lost — and where statistical programming hours actually go. R now has two mature roads to a shell-faithful table: <strong>gtsummary</strong>, the fastest route to a beautiful Table 1, and <strong>rtables</strong>, the Roche-built pharmaverse engine designed around CSR layouts (nested row splits, exact column structures, RTF delivery). A working biostatistician should hold both.</p>
<section id="road-1-demography-table-1-with-gtsummary" class="level2">
<h2 class="anchored" data-anchor-id="road-1-demography-table-1-with-gtsummary">Road 1 — Demography Table 1 with gtsummary</h2>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gtsummary)</span>
<span id="cb1-2"></span>
<span id="cb1-3">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, marker, stage, grade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>(</span>
<span id="cb1-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> trt,</span>
<span id="cb1-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">statistic =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_continuous</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{mean} ({sd})"</span>),</span>
<span id="cb1-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">missing_text =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Missing"</span></span>
<span id="cb1-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_overall</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_p</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">modify_header</span>(label <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Characteristic**"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bold_labels</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="landctdzaz" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#landctdzaz table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#landctdzaz thead, #landctdzaz tbody, #landctdzaz tfoot, #landctdzaz tr, #landctdzaz td, #landctdzaz th {
  border-style: none;
}

#landctdzaz p {
  margin: 0;
  padding: 0;
}

#landctdzaz .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#landctdzaz .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#landctdzaz .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#landctdzaz .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#landctdzaz .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#landctdzaz .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#landctdzaz .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#landctdzaz .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#landctdzaz .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#landctdzaz .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#landctdzaz .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#landctdzaz .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#landctdzaz .gt_spanner_row {
  border-bottom-style: hidden;
}

#landctdzaz .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#landctdzaz .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#landctdzaz .gt_from_md > :first-child {
  margin-top: 0;
}

#landctdzaz .gt_from_md > :last-child {
  margin-bottom: 0;
}

#landctdzaz .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#landctdzaz .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#landctdzaz .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#landctdzaz .gt_row_group_first td {
  border-top-width: 2px;
}

#landctdzaz .gt_row_group_first th {
  border-top-width: 2px;
}

#landctdzaz .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#landctdzaz .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#landctdzaz .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#landctdzaz .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#landctdzaz .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#landctdzaz .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#landctdzaz .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#landctdzaz .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#landctdzaz .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#landctdzaz .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#landctdzaz .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#landctdzaz .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#landctdzaz .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#landctdzaz .gt_left {
  text-align: left;
}

#landctdzaz .gt_center {
  text-align: center;
}

#landctdzaz .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#landctdzaz .gt_font_normal {
  font-weight: normal;
}

#landctdzaz .gt_font_bold {
  font-weight: bold;
}

#landctdzaz .gt_font_italic {
  font-style: italic;
}

#landctdzaz .gt_super {
  font-size: 65%;
}

#landctdzaz .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#landctdzaz .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#landctdzaz .gt_indent_1 {
  text-indent: 5px;
}

#landctdzaz .gt_indent_2 {
  text-indent: 10px;
}

#landctdzaz .gt_indent_3 {
  text-indent: 15px;
}

#landctdzaz .gt_indent_4 {
  text-indent: 20px;
}

#landctdzaz .gt_indent_5 {
  text-indent: 25px;
}

#landctdzaz .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#landctdzaz div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Overall</strong><br>
N = 200<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug A</strong><br>
N = 98<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug B</strong><br>
N = 102<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="p.value" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>p-value</strong><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Age</td>
<td class="gt_row gt_center" headers="stat_0">47 (14)</td>
<td class="gt_row gt_center" headers="stat_1">47 (15)</td>
<td class="gt_row gt_center" headers="stat_2">47 (14)</td>
<td class="gt_row gt_center" headers="p.value">0.7</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Missing</td>
<td class="gt_row gt_center" headers="stat_0">11</td>
<td class="gt_row gt_center" headers="stat_1">7</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Marker Level (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_0">0.92 (0.86)</td>
<td class="gt_row gt_center" headers="stat_1">1.02 (0.89)</td>
<td class="gt_row gt_center" headers="stat_2">0.82 (0.83)</td>
<td class="gt_row gt_center" headers="p.value">0.085</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Missing</td>
<td class="gt_row gt_center" headers="stat_0">10</td>
<td class="gt_row gt_center" headers="stat_1">6</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">T Stage</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T1</td>
<td class="gt_row gt_center" headers="stat_0">53 (27%)</td>
<td class="gt_row gt_center" headers="stat_1">28 (29%)</td>
<td class="gt_row gt_center" headers="stat_2">25 (25%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T2</td>
<td class="gt_row gt_center" headers="stat_0">54 (27%)</td>
<td class="gt_row gt_center" headers="stat_1">25 (26%)</td>
<td class="gt_row gt_center" headers="stat_2">29 (28%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T3</td>
<td class="gt_row gt_center" headers="stat_0">43 (22%)</td>
<td class="gt_row gt_center" headers="stat_1">22 (22%)</td>
<td class="gt_row gt_center" headers="stat_2">21 (21%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T4</td>
<td class="gt_row gt_center" headers="stat_0">50 (25%)</td>
<td class="gt_row gt_center" headers="stat_1">23 (23%)</td>
<td class="gt_row gt_center" headers="stat_2">27 (26%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Grade</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">35 (36%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">32 (33%)</td>
<td class="gt_row gt_center" headers="stat_2">36 (35%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_0">64 (32%)</td>
<td class="gt_row gt_center" headers="stat_1">31 (32%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Mean (SD); n (%)</td>
</tr>
<tr class="gt_footnotes even">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span> Wilcoxon rank sum test; Pearson’s Chi-squared test</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>Five functions, publication-ready, and <code>as_flex_table()</code> exports it straight into the Word shell your sponsor expects. This is the road for baseline tables, regression summaries and most academic deliverables — we walk through it in the <a href="../posts/gtsummary-tutorial-table1.html">Table 1 video lesson</a>.</p>
</section>
<section id="road-2-ae-summary-with-rtables" class="level2">
<h2 class="anchored" data-anchor-id="road-2-ae-summary-with-rtables">Road 2 — AE summary with rtables</h2>
<p>Adverse-event tables are structurally different: subjects counted once per system organ class and preferred term, “n (%)” against the safety population, nested rows. This is rtables territory. A tiny synthetic ADAE:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rtables)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tern)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># clinical analyze functions on top of rtables</span></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb2-4"></span>
<span id="cb2-5">adsl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb2-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">USUBJID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sprintf</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01-%03d"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>),</span>
<span id="cb2-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ARM     =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Drug A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Placebo"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb2-8">)</span>
<span id="cb2-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">42</span>)</span>
<span id="cb2-10">adae <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tibble</span>(</span>
<span id="cb2-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">USUBJID =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(adsl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>USUBJID, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb2-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AESOC   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gastrointestinal disorders"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nervous system disorders"</span>,</span>
<span id="cb2-13">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Infections and infestations"</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb2-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">AEDECOD =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nausea"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Headache"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Dizziness"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Nasopharyngitis"</span>,</span>
<span id="cb2-15">                     <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diarrhoea"</span>), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb2-16">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(adsl, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USUBJID"</span>)</span>
<span id="cb2-18"></span>
<span id="cb2-19">lyt <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">basic_table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show_colcounts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split_cols_by</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ARM"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">split_rows_by</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AESOC"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">split_fun =</span> drop_split_levels) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize_num_patients</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">var =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"USUBJID"</span>,</span>
<span id="cb2-23">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.stats =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>,</span>
<span id="cb2-24">                         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.labels =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unique =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Patients with &gt;=1 event"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count_occurrences</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">vars =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AEDECOD"</span>)</span>
<span id="cb2-26"></span>
<span id="cb2-27"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">build_table</span>(lyt, adae, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alt_counts_df =</span> adsl)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>                           Placebo       Drug A  
                            (N=30)       (N=30)  
—————————————————————————————————————————————————
Patients with &gt;=1 event   10 (33.3%)   15 (50.0%)
  Diarrhoea                1 (3.3%)    3 (10.0%) 
  Dizziness                2 (6.7%)     2 (6.7%) 
  Headache                3 (10.0%)     1 (3.3%) 
  Nasopharyngitis         5 (16.7%)    5 (16.7%) 
  Nausea                   2 (6.7%)    6 (20.0%) 
Patients with &gt;=1 event   19 (63.3%)   10 (33.3%)
  Diarrhoea               4 (13.3%)    4 (13.3%) 
  Dizziness               5 (16.7%)    3 (10.0%) 
  Headache                9 (30.0%)        0     
  Nasopharyngitis          2 (6.7%)    3 (10.0%) 
  Nausea                  6 (20.0%)     2 (6.7%) 
Patients with &gt;=1 event   5 (16.7%)    11 (36.7%)
  Diarrhoea                1 (3.3%)    4 (13.3%) 
  Dizziness                1 (3.3%)     1 (3.3%) 
  Headache                 2 (6.7%)     1 (3.3%) 
  Nasopharyngitis             0         2 (6.7%) 
  Nausea                   1 (3.3%)    4 (13.3%) </code></pre>
</div>
</div>
<p>Read the layout like a sentence: split columns by arm, split rows by SOC, count unique patients, then count occurrences by preferred term. The layout object (<code>lyt</code>) is <em>separate from the data</em> — which means the same shell definition is reusable across studies, and your QC programmer can review the layout as a specification.</p>
</section>
<section id="which-road-when" class="level2">
<h2 class="anchored" data-anchor-id="which-road-when">Which road when?</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="header">
<th>Situation</th>
<th>Reach for</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Baseline characteristics, regression tables, academic papers</td>
<td>gtsummary</td>
</tr>
<tr class="even">
<td>AE/exposure/lab tables against a CSR shell, RTF batch delivery</td>
<td>rtables</td>
</tr>
<tr class="odd">
<td>Denominators from a different dataset than the counts</td>
<td>rtables (<code>alt_counts_df</code>)</td>
</tr>
<tr class="even">
<td>One-off table needed in ten minutes</td>
<td>gtsummary</td>
</tr>
</tbody>
</table>
<p>The honest answer for a CRO deliverable is usually <em>both in the same pipeline</em> — gtsummary where it is faster, rtables where the shell demands structure. The inputs come from ADaM datasets (<a href="../posts/adam-derivation-admiral-r.html">derived with admiral</a>), and both roads end in the same place: a table that matches the mock shell to the cell.</p>
<hr>
<p><em>Full shell-to-output pairs live in our <a href="../tlf-gallery.html">TLF gallery</a>. Need a TLF package produced or double-programmed? <a href="../for-cros.html">That’s what we do →</a></em></p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>cdisc</category>
  <category>biostatistics</category>
  <guid>https://rverseanalytics.com/posts/tlf-tables-r-gtsummary-rtables.html</guid>
  <pubDate>Fri, 07 Aug 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Biostatistics datasets built into R (that you can explore in the browser)</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/r-biostatistics-datasets.html</link>
  <description><![CDATA[ 




<p>One of R’s quiet superpowers for learning and teaching biostatistics is the wealth of real clinical datasets that come built in — no downloads, no cleaning, no data-sharing paperwork. They’re perfect for practising a method, building a reproducible example, or teaching a class. Here’s a tour of the most useful ones, grouped by where they live. Want to poke at them right now? Every dataset below is loadable in our <a href="../learn/playground.html">browser R playground</a> — no install.</p>
<section id="the-datasets-package-always-available" class="level2">
<h2 class="anchored" data-anchor-id="the-datasets-package-always-available">The <code>datasets</code> package (always available)</h2>
<p>R ships with a <code>datasets</code> package that’s loaded by default, so these need no <code>library()</code> call. Two are genuine epidemiology classics:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># infert: a matched case-control study of infertility after abortion</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str</span>(infert)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>'data.frame':   248 obs. of  8 variables:
 $ education     : Factor w/ 3 levels "0-5yrs","6-11yrs",..: 1 1 1 1 2 2 2 2 2 2 ...
 $ age           : num  26 42 39 34 35 36 23 32 21 28 ...
 $ parity        : num  6 1 6 4 3 4 1 2 1 2 ...
 $ induced       : num  1 1 2 2 1 2 0 0 0 0 ...
 $ case          : num  1 1 1 1 1 1 1 1 1 1 ...
 $ spontaneous   : num  2 0 0 0 1 1 0 0 1 0 ...
 $ stratum       : int  1 2 3 4 5 6 7 8 9 10 ...
 $ pooled.stratum: num  3 1 4 2 32 36 6 22 5 19 ...</code></pre>
</div>
</div>
<p><code>infert</code> is a <strong>matched case-control</strong> study — ideal for teaching conditional logistic regression and matched designs. Its companion is <code>esoph</code>, a case-control study of oesophageal cancer with age, alcohol and tobacco exposure:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(esoph)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  agegp     alcgp    tobgp ncases ncontrols
1 25-34 0-39g/day 0-9g/day      0        40
2 25-34 0-39g/day    10-19      0        10
3 25-34 0-39g/day    20-29      0         6
4 25-34 0-39g/day      30+      0         5
5 25-34     40-79 0-9g/day      0        27
6 25-34     40-79    10-19      0         7</code></pre>
</div>
</div>
<p>Two more worth knowing: <code>ToothGrowth</code> (a dose–response experiment, good for two-way ANOVA) and <code>sleep</code> (the paired dataset from Student’s original t-test paper).</p>
</section>
<section id="the-survival-package-time-to-event-gold-standards" class="level2">
<h2 class="anchored" data-anchor-id="the-survival-package-time-to-event-gold-standards">The <code>survival</code> package (time-to-event gold standards)</h2>
<p>If you’re learning survival analysis, these are the datasets every textbook uses. Load the package and they’re available directly:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(survival)</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pbc: the Mayo Clinic primary biliary cirrhosis trial</span></span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dim</span>(pbc)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 418  20</code></pre>
</div>
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(pbc[, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"time"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"status"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"trt"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"age"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sex"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stage"</span>)])</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>  time status trt      age sex stage
1  400      2   1 58.76523   f     4
2 4500      0   1 56.44627   f     3
3 1012      2   1 70.07255   m     4
4 1925      2   1 54.74059   f     4
5 1504      1   2 38.10541   f     3
6 2503      2   2 66.25873   f     3</code></pre>
</div>
</div>
<p><code>pbc</code> (primary biliary cirrhosis, 418 patients) and <code>lung</code> (NCCTG advanced lung cancer, 228 patients) are the two you’ll meet most often. Here’s a Kaplan–Meier plot straight from <code>pbc</code>, comparing survival by sex — the kind of figure these datasets are made for:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">fit <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">survfit</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Surv</span>(time, status <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> sex, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> pbc)</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">plot</span>(fit, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2f6fed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#17a2b8"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb9-4">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xlab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Days"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ylab =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Survival probability"</span>,</span>
<span id="cb9-5">     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mark.time =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb9-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">legend</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"topright"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Male"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Female"</span>),</span>
<span id="cb9-7">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2f6fed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#17a2b8"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lwd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-km" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-km-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rverseanalytics.com/posts/r-biostatistics-datasets_files/figure-html/fig-km-1.png" class="img-fluid figure-img" width="672">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-km-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Kaplan–Meier survival curves by sex, from the built-in survival::pbc dataset.
</figcaption>
</figure>
</div>
</div>
</div>
<p>Other survival datasets worth exploring: <code>veteran</code> (a lung-cancer trial), <code>colon</code> (adjuvant chemotherapy, with recurrence and death), and <code>ovarian</code>. See our <a href="../posts/kaplan-meier-ggplot2.html">Kaplan–Meier with ggplot2</a> and <a href="../posts/cox-proportional-hazards-r.html">Cox proportional hazards</a> posts for what to do next.</p>
</section>
<section id="the-mass-package-a-logistic-regression-classic" class="level2">
<h2 class="anchored" data-anchor-id="the-mass-package-a-logistic-regression-classic">The <code>MASS</code> package (a logistic-regression classic)</h2>
<p><code>MASS::birthwt</code> is the canonical teaching dataset for logistic regression: predicting <strong>low birth weight</strong> from maternal factors like smoking, age and hypertension.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(MASS)</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Low birth weight (low = 1) by maternal smoking (smoke = 1)</span></span>
<span id="cb10-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">table</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">low =</span> birthwt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>low, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">smoke =</span> birthwt<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>smoke)</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code>   smoke
low  0  1
  0 86 44
  1 29 30</code></pre>
</div>
</div>
<p>189 births, a binary outcome, and a handful of interpretable predictors — it’s hard to beat for a first logistic model.</p>
</section>
<section id="gtsummarytrial-for-summary-and-regression-tables" class="level2">
<h2 class="anchored" data-anchor-id="gtsummarytrial-for-summary-and-regression-tables"><code>gtsummary::trial</code> (for summary and regression tables)</h2>
<p>The <code>trial</code> dataset (a simulated 200-patient oncology trial) ships with <strong>gtsummary</strong> and is purpose-built for practising publication-ready tables. We cover it in depth in the <a href="../posts/gtsummary-tutorial-table1.html">step-by-step Table 1 tutorial</a> — and you can build tables from it live in the <a href="../gtsummary-live-demo.html">gtsummary demo</a>.</p>
</section>
<section id="medicaldata-a-modern-teaching-collection" class="level2">
<h2 class="anchored" data-anchor-id="medicaldata-a-modern-teaching-collection"><code>medicaldata</code> (a modern teaching collection)</h2>
<p>For something more contemporary, the <a href="https://higgi13425.github.io/medicaldata/">medicaldata</a> package (by Peter Higgins) is a curated collection of real medical datasets chosen specifically for teaching. Highlights:</p>
<ul>
<li><strong><code>strep_tb</code></strong> — the 1948 streptomycin trial for tuberculosis, widely considered the <strong>first modern randomised controlled trial</strong>.</li>
<li><strong><code>indo_rct</code></strong> — a randomised trial of indomethacin to prevent post-ERCP pancreatitis.</li>
<li><strong><code>licorice_gargle</code></strong> — a surgical RCT (does gargling licorice reduce post-op sore throat?).</li>
<li><strong><code>covid_testing</code></strong> — a large, messy, real-world dataset ideal for practising data cleaning.</li>
</ul>
<p><code>medicaldata</code> isn’t part of base R, so on your own machine you’d install it once with <code>install.packages("medicaldata")</code>. The easiest way to try it without installing anything is in the browser — see below.</p>
</section>
<section id="try-them-all-live-no-install" class="level2">
<h2 class="anchored" data-anchor-id="try-them-all-live-no-install">Try them all live, no install</h2>
<p>You can load and explore every one of these — including <code>medicaldata</code> — right now in our <a href="../learn/playground.html"><strong>R playground</strong></a>. It runs real R in your browser via WebAssembly; the datasets section has ready-to-run cells for <code>pbc</code>, <code>birthwt</code>, <code>strep_tb</code> and more. Not sure which test to run on them? Our <a href="../which-test.html">which test should I use?</a> guide points the way.</p>
<hr>
<p><em>Practising is free; production analysis on your own clinical data is where things get real. Our <a href="../clinical-r.html">Clinical R toolkit</a> collects the recipes, and <a href="../contact.html">we’re here</a> when you need the full analysis.</em></p>
<p><em>Part of our <a href="../biostatistics-in-r.html">guide to biostatistics in R</a>.</em></p>


</section>

 ]]></description>
  <category>r-tutorial</category>
  <category>biostatistics</category>
  <category>datasets</category>
  <category>teaching</category>
  <guid>https://rverseanalytics.com/posts/r-biostatistics-datasets.html</guid>
  <pubDate>Thu, 09 Jul 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>gtsummary for beginners: build a Table 1 from clinical data, step by step</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/gtsummary-tutorial-table1.html</link>
  <description><![CDATA[ 




<p>If you’ve ever built a “Table 1” — the baseline-characteristics table at the start of every clinical paper — by hand, you know it’s slow and easy to get wrong. The <a href="https://www.danieldsjoberg.com/gtsummary/">gtsummary</a> package builds it correctly from your data with a few readable lines. This tutorial goes slowly, one step at a time, so you can see exactly what each piece does. Prefer to experiment as you read? Skip to the live demo and run gtsummary in your browser — no install.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Already comfortable with the basics and just want the recipe? Our <a href="../posts/table1-baseline-characteristics-gtsummary.html">one-line Table 1 reference</a> is the quick version. This post is the slow, first-time walkthrough.</p>
</div>
</div>
<section id="the-data" class="level2">
<h2 class="anchored" data-anchor-id="the-data">The data</h2>
<p>We’ll use <code>trial</code>, a small clinical-trial dataset that ships with gtsummary — 200 patients, with treatment, age, tumour marker, stage and grade. Because it comes with the package, you can follow along with no downloads.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gtsummary)</span>
<span id="cb1-2"></span>
<span id="cb1-3">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, marker, stage, grade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span></code></pre></div></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 5
  trt      age marker stage grade
  &lt;chr&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;fct&gt; &lt;fct&gt;
1 Drug A    23  0.16  T1    II   
2 Drug B     9  1.11  T2    I    
3 Drug A    31  0.277 T1    II   
4 Drug A    NA  2.07  T3    III  
5 Drug A    51  2.77  T4    III  
6 Drug B    39  0.613 T4    I    </code></pre>
</div>
</div>
<p>Each column already carries a human-readable <strong>label</strong> (for example <code>age</code> is labelled “Age”) — gtsummary uses these automatically, which is part of why its tables look finished.</p>
</section>
<section id="step-1-the-simplest-possible-summary" class="level2">
<h2 class="anchored" data-anchor-id="step-1-the-simplest-possible-summary">Step 1 — the simplest possible summary</h2>
<p>Pass the data to <code>tbl_summary()</code>. That’s it:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(age, marker, stage, grade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb3-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="gasrrpjanr" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#gasrrpjanr table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#gasrrpjanr thead, #gasrrpjanr tbody, #gasrrpjanr tfoot, #gasrrpjanr tr, #gasrrpjanr td, #gasrrpjanr th {
  border-style: none;
}

#gasrrpjanr p {
  margin: 0;
  padding: 0;
}

#gasrrpjanr .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#gasrrpjanr .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#gasrrpjanr .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#gasrrpjanr .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#gasrrpjanr .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#gasrrpjanr .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#gasrrpjanr .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#gasrrpjanr .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#gasrrpjanr .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#gasrrpjanr .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#gasrrpjanr .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#gasrrpjanr .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#gasrrpjanr .gt_spanner_row {
  border-bottom-style: hidden;
}

#gasrrpjanr .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#gasrrpjanr .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#gasrrpjanr .gt_from_md > :first-child {
  margin-top: 0;
}

#gasrrpjanr .gt_from_md > :last-child {
  margin-bottom: 0;
}

#gasrrpjanr .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#gasrrpjanr .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#gasrrpjanr .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#gasrrpjanr .gt_row_group_first td {
  border-top-width: 2px;
}

#gasrrpjanr .gt_row_group_first th {
  border-top-width: 2px;
}

#gasrrpjanr .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#gasrrpjanr .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#gasrrpjanr .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#gasrrpjanr .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#gasrrpjanr .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#gasrrpjanr .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#gasrrpjanr .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#gasrrpjanr .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#gasrrpjanr .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#gasrrpjanr .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#gasrrpjanr .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#gasrrpjanr .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#gasrrpjanr .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#gasrrpjanr .gt_left {
  text-align: left;
}

#gasrrpjanr .gt_center {
  text-align: center;
}

#gasrrpjanr .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#gasrrpjanr .gt_font_normal {
  font-weight: normal;
}

#gasrrpjanr .gt_font_bold {
  font-weight: bold;
}

#gasrrpjanr .gt_font_italic {
  font-style: italic;
}

#gasrrpjanr .gt_super {
  font-size: 65%;
}

#gasrrpjanr .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#gasrrpjanr .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#gasrrpjanr .gt_indent_1 {
  text-indent: 5px;
}

#gasrrpjanr .gt_indent_2 {
  text-indent: 10px;
}

#gasrrpjanr .gt_indent_3 {
  text-indent: 15px;
}

#gasrrpjanr .gt_indent_4 {
  text-indent: 20px;
}

#gasrrpjanr .gt_indent_5 {
  text-indent: 25px;
}

#gasrrpjanr .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#gasrrpjanr div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 50%">
<col style="width: 50%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>N = 200</strong><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">Age</td>
<td class="gt_row gt_center" headers="stat_0">47 (38, 57)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_0">11</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">Marker Level (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_0">0.64 (0.22, 1.41)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_0">10</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">T Stage</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T1</td>
<td class="gt_row gt_center" headers="stat_0">53 (27%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T2</td>
<td class="gt_row gt_center" headers="stat_0">54 (27%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T3</td>
<td class="gt_row gt_center" headers="stat_0">43 (22%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T4</td>
<td class="gt_row gt_center" headers="stat_0">50 (25%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">Grade</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_0">64 (32%)</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="2" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Median (Q1, Q3); n (%)</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>Notice what happened without any instructions from us: continuous variables (age, marker) are summarised as <strong>median (IQR)</strong>, categorical ones (stage, grade) as <strong>n (%)</strong>, and missing values are counted. gtsummary chose a sensible default for every variable type.</p>
</section>
<section id="step-2-split-by-treatment-group" class="level2">
<h2 class="anchored" data-anchor-id="step-2-split-by-treatment-group">Step 2 — split by treatment group</h2>
<p>A Table 1 almost always compares groups. Add <code>by = trt</code> to get one column per treatment arm:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, marker, stage, grade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> trt)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="kxlfjwsdkv" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#kxlfjwsdkv table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#kxlfjwsdkv thead, #kxlfjwsdkv tbody, #kxlfjwsdkv tfoot, #kxlfjwsdkv tr, #kxlfjwsdkv td, #kxlfjwsdkv th {
  border-style: none;
}

#kxlfjwsdkv p {
  margin: 0;
  padding: 0;
}

#kxlfjwsdkv .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#kxlfjwsdkv .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#kxlfjwsdkv .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#kxlfjwsdkv .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#kxlfjwsdkv .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kxlfjwsdkv .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kxlfjwsdkv .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kxlfjwsdkv .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#kxlfjwsdkv .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#kxlfjwsdkv .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#kxlfjwsdkv .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#kxlfjwsdkv .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#kxlfjwsdkv .gt_spanner_row {
  border-bottom-style: hidden;
}

#kxlfjwsdkv .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#kxlfjwsdkv .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#kxlfjwsdkv .gt_from_md > :first-child {
  margin-top: 0;
}

#kxlfjwsdkv .gt_from_md > :last-child {
  margin-bottom: 0;
}

#kxlfjwsdkv .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#kxlfjwsdkv .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#kxlfjwsdkv .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#kxlfjwsdkv .gt_row_group_first td {
  border-top-width: 2px;
}

#kxlfjwsdkv .gt_row_group_first th {
  border-top-width: 2px;
}

#kxlfjwsdkv .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kxlfjwsdkv .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#kxlfjwsdkv .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#kxlfjwsdkv .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kxlfjwsdkv .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kxlfjwsdkv .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#kxlfjwsdkv .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#kxlfjwsdkv .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#kxlfjwsdkv .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kxlfjwsdkv .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kxlfjwsdkv .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kxlfjwsdkv .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kxlfjwsdkv .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kxlfjwsdkv .gt_left {
  text-align: left;
}

#kxlfjwsdkv .gt_center {
  text-align: center;
}

#kxlfjwsdkv .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#kxlfjwsdkv .gt_font_normal {
  font-weight: normal;
}

#kxlfjwsdkv .gt_font_bold {
  font-weight: bold;
}

#kxlfjwsdkv .gt_font_italic {
  font-style: italic;
}

#kxlfjwsdkv .gt_super {
  font-size: 65%;
}

#kxlfjwsdkv .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#kxlfjwsdkv .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#kxlfjwsdkv .gt_indent_1 {
  text-indent: 5px;
}

#kxlfjwsdkv .gt_indent_2 {
  text-indent: 10px;
}

#kxlfjwsdkv .gt_indent_3 {
  text-indent: 15px;
}

#kxlfjwsdkv .gt_indent_4 {
  text-indent: 20px;
}

#kxlfjwsdkv .gt_indent_5 {
  text-indent: 25px;
}

#kxlfjwsdkv .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#kxlfjwsdkv div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 33%">
<col style="width: 33%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug A</strong><br>
N = 98<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug B</strong><br>
N = 102<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">Age</td>
<td class="gt_row gt_center" headers="stat_1">46 (37, 60)</td>
<td class="gt_row gt_center" headers="stat_2">48 (39, 56)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_1">7</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">Marker Level (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_1">0.84 (0.23, 1.60)</td>
<td class="gt_row gt_center" headers="stat_2">0.52 (0.18, 1.21)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_1">6</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">T Stage</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T1</td>
<td class="gt_row gt_center" headers="stat_1">28 (29%)</td>
<td class="gt_row gt_center" headers="stat_2">25 (25%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T2</td>
<td class="gt_row gt_center" headers="stat_1">25 (26%)</td>
<td class="gt_row gt_center" headers="stat_2">29 (28%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T3</td>
<td class="gt_row gt_center" headers="stat_1">22 (22%)</td>
<td class="gt_row gt_center" headers="stat_2">21 (21%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T4</td>
<td class="gt_row gt_center" headers="stat_1">23 (23%)</td>
<td class="gt_row gt_center" headers="stat_2">27 (26%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">Grade</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_1">35 (36%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_1">32 (33%)</td>
<td class="gt_row gt_center" headers="stat_2">36 (35%)</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_1">31 (32%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="3" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Median (Q1, Q3); n (%)</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>Now each characteristic is shown separately for the two treatment groups — the shape every reader expects from a Table 1.</p>
</section>
<section id="step-3-add-a-p-value" class="level2">
<h2 class="anchored" data-anchor-id="step-3-add-a-p-value">Step 3 — add a p-value</h2>
<p>Reviewers usually want a statistical test comparing the groups. <code>add_p()</code> picks an appropriate test for each row automatically — Wilcoxon for continuous, chi-squared or Fisher for categorical:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, marker, stage, grade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> trt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_p</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="umeypfwcam" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#umeypfwcam table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#umeypfwcam thead, #umeypfwcam tbody, #umeypfwcam tfoot, #umeypfwcam tr, #umeypfwcam td, #umeypfwcam th {
  border-style: none;
}

#umeypfwcam p {
  margin: 0;
  padding: 0;
}

#umeypfwcam .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#umeypfwcam .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#umeypfwcam .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#umeypfwcam .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#umeypfwcam .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#umeypfwcam .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#umeypfwcam .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#umeypfwcam .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#umeypfwcam .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#umeypfwcam .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#umeypfwcam .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#umeypfwcam .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#umeypfwcam .gt_spanner_row {
  border-bottom-style: hidden;
}

#umeypfwcam .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#umeypfwcam .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#umeypfwcam .gt_from_md > :first-child {
  margin-top: 0;
}

#umeypfwcam .gt_from_md > :last-child {
  margin-bottom: 0;
}

#umeypfwcam .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#umeypfwcam .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#umeypfwcam .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#umeypfwcam .gt_row_group_first td {
  border-top-width: 2px;
}

#umeypfwcam .gt_row_group_first th {
  border-top-width: 2px;
}

#umeypfwcam .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#umeypfwcam .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#umeypfwcam .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#umeypfwcam .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#umeypfwcam .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#umeypfwcam .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#umeypfwcam .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#umeypfwcam .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#umeypfwcam .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#umeypfwcam .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#umeypfwcam .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#umeypfwcam .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#umeypfwcam .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#umeypfwcam .gt_left {
  text-align: left;
}

#umeypfwcam .gt_center {
  text-align: center;
}

#umeypfwcam .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#umeypfwcam .gt_font_normal {
  font-weight: normal;
}

#umeypfwcam .gt_font_bold {
  font-weight: bold;
}

#umeypfwcam .gt_font_italic {
  font-style: italic;
}

#umeypfwcam .gt_super {
  font-size: 65%;
}

#umeypfwcam .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#umeypfwcam .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#umeypfwcam .gt_indent_1 {
  text-indent: 5px;
}

#umeypfwcam .gt_indent_2 {
  text-indent: 10px;
}

#umeypfwcam .gt_indent_3 {
  text-indent: 15px;
}

#umeypfwcam .gt_indent_4 {
  text-indent: 20px;
}

#umeypfwcam .gt_indent_5 {
  text-indent: 25px;
}

#umeypfwcam .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#umeypfwcam div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug A</strong><br>
N = 98<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug B</strong><br>
N = 102<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="p.value" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>p-value</strong><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">Age</td>
<td class="gt_row gt_center" headers="stat_1">46 (37, 60)</td>
<td class="gt_row gt_center" headers="stat_2">48 (39, 56)</td>
<td class="gt_row gt_center" headers="p.value">0.7</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_1">7</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">Marker Level (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_1">0.84 (0.23, 1.60)</td>
<td class="gt_row gt_center" headers="stat_2">0.52 (0.18, 1.21)</td>
<td class="gt_row gt_center" headers="p.value">0.085</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_1">6</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">T Stage</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T1</td>
<td class="gt_row gt_center" headers="stat_1">28 (29%)</td>
<td class="gt_row gt_center" headers="stat_2">25 (25%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T2</td>
<td class="gt_row gt_center" headers="stat_1">25 (26%)</td>
<td class="gt_row gt_center" headers="stat_2">29 (28%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T3</td>
<td class="gt_row gt_center" headers="stat_1">22 (22%)</td>
<td class="gt_row gt_center" headers="stat_2">21 (21%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T4</td>
<td class="gt_row gt_center" headers="stat_1">23 (23%)</td>
<td class="gt_row gt_center" headers="stat_2">27 (26%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">Grade</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_1">35 (36%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_1">32 (33%)</td>
<td class="gt_row gt_center" headers="stat_2">36 (35%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_1">31 (32%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="4" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Median (Q1, Q3); n (%)</td>
</tr>
<tr class="gt_footnotes even">
<td colspan="4" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span> Wilcoxon rank sum test; Pearson’s Chi-squared test</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>You didn’t choose the tests — gtsummary matched each one to the variable type. That removes a common source of error (running the wrong test on the wrong kind of variable).</p>
</section>
<section id="step-4-an-overall-column" class="level2">
<h2 class="anchored" data-anchor-id="step-4-an-overall-column">Step 4 — an overall column</h2>
<p>To also show the whole sample alongside the groups, add <code>add_overall()</code>:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, marker, stage, grade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> trt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_p</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_overall</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="cipqnfipse" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#cipqnfipse table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#cipqnfipse thead, #cipqnfipse tbody, #cipqnfipse tfoot, #cipqnfipse tr, #cipqnfipse td, #cipqnfipse th {
  border-style: none;
}

#cipqnfipse p {
  margin: 0;
  padding: 0;
}

#cipqnfipse .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#cipqnfipse .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#cipqnfipse .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#cipqnfipse .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#cipqnfipse .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cipqnfipse .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cipqnfipse .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#cipqnfipse .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#cipqnfipse .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#cipqnfipse .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#cipqnfipse .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#cipqnfipse .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#cipqnfipse .gt_spanner_row {
  border-bottom-style: hidden;
}

#cipqnfipse .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#cipqnfipse .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#cipqnfipse .gt_from_md > :first-child {
  margin-top: 0;
}

#cipqnfipse .gt_from_md > :last-child {
  margin-bottom: 0;
}

#cipqnfipse .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#cipqnfipse .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#cipqnfipse .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#cipqnfipse .gt_row_group_first td {
  border-top-width: 2px;
}

#cipqnfipse .gt_row_group_first th {
  border-top-width: 2px;
}

#cipqnfipse .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cipqnfipse .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#cipqnfipse .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#cipqnfipse .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cipqnfipse .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#cipqnfipse .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#cipqnfipse .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#cipqnfipse .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#cipqnfipse .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#cipqnfipse .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cipqnfipse .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cipqnfipse .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#cipqnfipse .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#cipqnfipse .gt_left {
  text-align: left;
}

#cipqnfipse .gt_center {
  text-align: center;
}

#cipqnfipse .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#cipqnfipse .gt_font_normal {
  font-weight: normal;
}

#cipqnfipse .gt_font_bold {
  font-weight: bold;
}

#cipqnfipse .gt_font_italic {
  font-style: italic;
}

#cipqnfipse .gt_super {
  font-size: 65%;
}

#cipqnfipse .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#cipqnfipse .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#cipqnfipse .gt_indent_1 {
  text-indent: 5px;
}

#cipqnfipse .gt_indent_2 {
  text-indent: 10px;
}

#cipqnfipse .gt_indent_3 {
  text-indent: 15px;
}

#cipqnfipse .gt_indent_4 {
  text-indent: 20px;
}

#cipqnfipse .gt_indent_5 {
  text-indent: 25px;
}

#cipqnfipse .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#cipqnfipse div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Overall</strong><br>
N = 200<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug A</strong><br>
N = 98<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug B</strong><br>
N = 102<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="p.value" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>p-value</strong><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">Age</td>
<td class="gt_row gt_center" headers="stat_0">47 (38, 57)</td>
<td class="gt_row gt_center" headers="stat_1">46 (37, 60)</td>
<td class="gt_row gt_center" headers="stat_2">48 (39, 56)</td>
<td class="gt_row gt_center" headers="p.value">0.7</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_0">11</td>
<td class="gt_row gt_center" headers="stat_1">7</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">Marker Level (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_0">0.64 (0.22, 1.41)</td>
<td class="gt_row gt_center" headers="stat_1">0.84 (0.23, 1.60)</td>
<td class="gt_row gt_center" headers="stat_2">0.52 (0.18, 1.21)</td>
<td class="gt_row gt_center" headers="p.value">0.085</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_0">10</td>
<td class="gt_row gt_center" headers="stat_1">6</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">T Stage</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T1</td>
<td class="gt_row gt_center" headers="stat_0">53 (27%)</td>
<td class="gt_row gt_center" headers="stat_1">28 (29%)</td>
<td class="gt_row gt_center" headers="stat_2">25 (25%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T2</td>
<td class="gt_row gt_center" headers="stat_0">54 (27%)</td>
<td class="gt_row gt_center" headers="stat_1">25 (26%)</td>
<td class="gt_row gt_center" headers="stat_2">29 (28%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T3</td>
<td class="gt_row gt_center" headers="stat_0">43 (22%)</td>
<td class="gt_row gt_center" headers="stat_1">22 (22%)</td>
<td class="gt_row gt_center" headers="stat_2">21 (21%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T4</td>
<td class="gt_row gt_center" headers="stat_0">50 (25%)</td>
<td class="gt_row gt_center" headers="stat_1">23 (23%)</td>
<td class="gt_row gt_center" headers="stat_2">27 (26%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">Grade</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">35 (36%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">32 (33%)</td>
<td class="gt_row gt_center" headers="stat_2">36 (35%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_0">64 (32%)</td>
<td class="gt_row gt_center" headers="stat_1">31 (32%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Median (Q1, Q3); n (%)</td>
</tr>
<tr class="gt_footnotes even">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span> Wilcoxon rank sum test; Pearson’s Chi-squared test</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
</section>
<section id="step-5-polish-for-publication" class="level2">
<h2 class="anchored" data-anchor-id="step-5-polish-for-publication">Step 5 — polish for publication</h2>
<p>A few finishing touches make it manuscript-ready: relabel variables, switch continuous summaries to <strong>mean (SD)</strong>, show missing counts explicitly, bold the row labels, and add a caption.</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, marker, stage, grade) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>(</span>
<span id="cb7-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> trt,</span>
<span id="cb7-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age (years)"</span>, marker <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Marker (ng/mL)"</span>),</span>
<span id="cb7-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">statistic =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_continuous</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{mean} ({sd})"</span>),</span>
<span id="cb7-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">missing_text =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Missing"</span></span>
<span id="cb7-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_p</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_overall</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bold_labels</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb7-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">modify_caption</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Table 1. Baseline characteristics by treatment group**"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="nhvytxbgmu" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#nhvytxbgmu table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#nhvytxbgmu thead, #nhvytxbgmu tbody, #nhvytxbgmu tfoot, #nhvytxbgmu tr, #nhvytxbgmu td, #nhvytxbgmu th {
  border-style: none;
}

#nhvytxbgmu p {
  margin: 0;
  padding: 0;
}

#nhvytxbgmu .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#nhvytxbgmu .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#nhvytxbgmu .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#nhvytxbgmu .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#nhvytxbgmu .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#nhvytxbgmu .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#nhvytxbgmu .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#nhvytxbgmu .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#nhvytxbgmu .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#nhvytxbgmu .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#nhvytxbgmu .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#nhvytxbgmu .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#nhvytxbgmu .gt_spanner_row {
  border-bottom-style: hidden;
}

#nhvytxbgmu .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#nhvytxbgmu .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#nhvytxbgmu .gt_from_md > :first-child {
  margin-top: 0;
}

#nhvytxbgmu .gt_from_md > :last-child {
  margin-bottom: 0;
}

#nhvytxbgmu .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#nhvytxbgmu .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#nhvytxbgmu .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#nhvytxbgmu .gt_row_group_first td {
  border-top-width: 2px;
}

#nhvytxbgmu .gt_row_group_first th {
  border-top-width: 2px;
}

#nhvytxbgmu .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#nhvytxbgmu .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#nhvytxbgmu .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#nhvytxbgmu .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#nhvytxbgmu .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#nhvytxbgmu .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#nhvytxbgmu .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#nhvytxbgmu .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#nhvytxbgmu .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#nhvytxbgmu .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#nhvytxbgmu .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#nhvytxbgmu .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#nhvytxbgmu .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#nhvytxbgmu .gt_left {
  text-align: left;
}

#nhvytxbgmu .gt_center {
  text-align: center;
}

#nhvytxbgmu .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#nhvytxbgmu .gt_font_normal {
  font-weight: normal;
}

#nhvytxbgmu .gt_font_bold {
  font-weight: bold;
}

#nhvytxbgmu .gt_font_italic {
  font-style: italic;
}

#nhvytxbgmu .gt_super {
  font-size: 65%;
}

#nhvytxbgmu .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#nhvytxbgmu .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#nhvytxbgmu .gt_indent_1 {
  text-indent: 5px;
}

#nhvytxbgmu .gt_indent_2 {
  text-indent: 10px;
}

#nhvytxbgmu .gt_indent_3 {
  text-indent: 15px;
}

#nhvytxbgmu .gt_indent_4 {
  text-indent: 20px;
}

#nhvytxbgmu .gt_indent_5 {
  text-indent: 25px;
}

#nhvytxbgmu .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#nhvytxbgmu div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<caption><strong>Table 1. Baseline characteristics by treatment group</strong></caption>
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Overall</strong><br>
N = 200<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug A</strong><br>
N = 98<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug B</strong><br>
N = 102<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="p.value" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>p-value</strong><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Age (years)</td>
<td class="gt_row gt_center" headers="stat_0">47 (14)</td>
<td class="gt_row gt_center" headers="stat_1">47 (15)</td>
<td class="gt_row gt_center" headers="stat_2">47 (14)</td>
<td class="gt_row gt_center" headers="p.value">0.7</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Missing</td>
<td class="gt_row gt_center" headers="stat_0">11</td>
<td class="gt_row gt_center" headers="stat_1">7</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Marker (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_0">0.92 (0.86)</td>
<td class="gt_row gt_center" headers="stat_1">1.02 (0.89)</td>
<td class="gt_row gt_center" headers="stat_2">0.82 (0.83)</td>
<td class="gt_row gt_center" headers="p.value">0.085</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Missing</td>
<td class="gt_row gt_center" headers="stat_0">10</td>
<td class="gt_row gt_center" headers="stat_1">6</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">T Stage</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T1</td>
<td class="gt_row gt_center" headers="stat_0">53 (27%)</td>
<td class="gt_row gt_center" headers="stat_1">28 (29%)</td>
<td class="gt_row gt_center" headers="stat_2">25 (25%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T2</td>
<td class="gt_row gt_center" headers="stat_0">54 (27%)</td>
<td class="gt_row gt_center" headers="stat_1">25 (26%)</td>
<td class="gt_row gt_center" headers="stat_2">29 (28%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T3</td>
<td class="gt_row gt_center" headers="stat_0">43 (22%)</td>
<td class="gt_row gt_center" headers="stat_1">22 (22%)</td>
<td class="gt_row gt_center" headers="stat_2">21 (21%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T4</td>
<td class="gt_row gt_center" headers="stat_0">50 (25%)</td>
<td class="gt_row gt_center" headers="stat_1">23 (23%)</td>
<td class="gt_row gt_center" headers="stat_2">27 (26%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Grade</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">35 (36%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">32 (33%)</td>
<td class="gt_row gt_center" headers="stat_2">36 (35%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_0">64 (32%)</td>
<td class="gt_row gt_center" headers="stat_1">31 (32%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Mean (SD); n (%)</td>
</tr>
<tr class="gt_footnotes even">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span> Wilcoxon rank sum test; Pearson’s Chi-squared test</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>Read the pipeline top to bottom and it says exactly what it does: summarise by treatment, add a p-value, add an overall column, bold the labels, add a caption. That readability is the point — anyone can check the table against the code.</p>
</section>
<section id="why-build-it-this-way" class="level2">
<h2 class="anchored" data-anchor-id="why-build-it-this-way">Why build it this way</h2>
<p>The real win isn’t saving a few minutes once. It’s that the table is <strong>code</strong>. Fix a data point, rerun, and every number — including the tests and percentages — updates together. There’s never a moment where the manuscript and the data disagree.</p>
</section>
<section id="try-it-yourself" class="level2">
<h2 class="anchored" data-anchor-id="try-it-yourself">Try it yourself</h2>
<p>Two ways to run gtsummary right now, in your browser, on this same clinical dataset — no R install:</p>
<ul>
<li><strong><a href="../demos.html">Point-and-click Table 1 explorer</a></strong> — choose the grouping variable and which characteristics to include, and watch the table rebuild. A real Shiny app running on WebAssembly.</li>
<li><strong><a href="../gtsummary-live-demo.html">Editable code demo</a></strong> — edit the gtsummary code and run it live, so you can try each verb from this tutorial yourself.</li>
</ul>
<p>Next steps once you’re comfortable: gtsummary also builds <strong>regression tables</strong> in one line — see our posts on <a href="../posts/logistic-regression-odds-ratios-roc.html">logistic regression odds ratios</a> and <a href="../posts/cox-proportional-hazards-r.html">Cox proportional hazards</a>.</p>
<hr>
<p><em>More clinical recipes live in our <a href="../clinical-r.html">Clinical R toolkit</a>. Need publication-ready tables on your own data? <a href="../contact.html">Get in touch</a>.</em></p>
<p><em>Part of our <a href="../biostatistics-in-r.html">guide to biostatistics in R</a>.</em></p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>biostatistics</category>
  <category>gtsummary</category>
  <category>r-tutorial</category>
  <guid>https://rverseanalytics.com/posts/gtsummary-tutorial-table1.html</guid>
  <pubDate>Thu, 09 Jul 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>The Markdown cheat sheet (with a live editor)</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/markdown-cheat-sheet.html</link>
  <description><![CDATA[ 




<p>Markdown is the plain-text writing syntax behind GitHub READMEs, static sites, and the prose in Quarto and R Markdown. You can learn the whole practical core in one sitting. Here’s the reference — bookmark it, and try anything live in our <a href="../tools/markdown-previewer.html">Markdown previewer</a>.</p>
<section id="text-and-headings" class="level2">
<h2 class="anchored" data-anchor-id="text-and-headings">Text and headings</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th style="text-align: left;">You write</th>
<th style="text-align: left;">You get</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;"><code># Heading 1</code></td>
<td style="text-align: left;">a top-level heading</td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>## Heading 2</code></td>
<td style="text-align: left;">a section heading</td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>**bold**</code></td>
<td style="text-align: left;"><strong>bold</strong></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>*italic*</code></td>
<td style="text-align: left;"><em>italic</em></td>
</tr>
<tr class="odd">
<td style="text-align: left;"><code>`code`</code></td>
<td style="text-align: left;"><code>code</code></td>
</tr>
<tr class="even">
<td style="text-align: left;"><code>~~struck~~</code></td>
<td style="text-align: left;"><del>struck</del></td>
</tr>
</tbody>
</table>
<p>Headings go from <code>#</code> (largest) to <code>######</code> (smallest). Leave a blank line between paragraphs — a single line break is usually ignored.</p>
</section>
<section id="lists" class="level2">
<h2 class="anchored" data-anchor-id="lists">Lists</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb1-1"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>Unordered item</span>
<span id="cb1-2"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">- </span>Another item</span>
<span id="cb1-3"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">  - </span>Indented sub-item</span>
<span id="cb1-4"></span>
<span id="cb1-5"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">1. </span>Ordered item</span>
<span id="cb1-6"><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">2. </span>Second item</span></code></pre></div></div>
</section>
<section id="links-and-images" class="level2">
<h2 class="anchored" data-anchor-id="links-and-images">Links and images</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">Link text</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">](https://example.com)</span></span>
<span id="cb2-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">[</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">With a tooltip</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">](https://example.com "Hover title")</span></span>
<span id="cb2-3"><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">![Image alt text](path/to/image.png)</span></span></code></pre></div></div>
<p>The image syntax is just a link with a leading <code>!</code>. Always write meaningful alt text — it matters for accessibility and SEO.</p>
</section>
<section id="code" class="level2">
<h2 class="anchored" data-anchor-id="code">Code</h2>
<p>Inline code uses single backticks: <code>`mean(x)`</code>. For a block, fence it with triple backticks and name the language:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb3-1"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```r</span></span>
<span id="cb3-2">x <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(x)</span>
<span id="cb3-4"><span class="in" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">```</span></span></code></pre></div></div>
</section>
<section id="blockquotes-and-rules" class="level2">
<h2 class="anchored" data-anchor-id="blockquotes-and-rules">Blockquotes and rules</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb4-1"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">&gt; A blockquote for citing a source.</span></span>
<span id="cb4-2"></span>
<span id="cb4-3">---</span></code></pre></div></div>
<p>Three dashes on their own line draw a horizontal rule.</p>
</section>
<section id="tables" class="level2">
<h2 class="anchored" data-anchor-id="tables">Tables</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb5-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Column <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Number <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb5-2"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|:-------|-------:|</span></span>
<span id="cb5-3"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Left   <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span>    3.1 <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb5-4"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span> Aligned<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span>   42.0 <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">|</span></span></code></pre></div></div>
<p>Colons in the separator row set alignment: <code>:---</code> left, <code>---:</code> right, <code>:---:</code> centre.</p>
</section>
<section id="learn-it-interactively" class="level2">
<h2 class="anchored" data-anchor-id="learn-it-interactively">Learn it interactively</h2>
<p>That’s the entire everyday toolkit. To practise with instant feedback, work through the free <a href="../learn-markdown.html">interactive Markdown course</a> — every lesson has a live editor — then move on to <a href="../learn-quarto.html">Learn Quarto</a> to turn Markdown into reproducible reports.</p>
<hr>
<p><em>Building reports or a documentation site in Markdown or Quarto? <a href="../services.html">We design reporting pipelines</a> that go from raw data to a polished document in one command.</em></p>


</section>

 ]]></description>
  <category>markdown</category>
  <category>reference</category>
  <guid>https://rverseanalytics.com/posts/markdown-cheat-sheet.html</guid>
  <pubDate>Wed, 08 Jul 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>Your Table 1, in one line, with gtsummary</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/table1-baseline-characteristics-gtsummary.html</link>
  <description><![CDATA[ 




<p>Every clinical paper opens with the same table: participant characteristics, split by group. Built by hand it is tedious and easy to get wrong — the wrong summary statistic, the wrong test, a percentage that doesn’t add up. <code>gtsummary</code> builds it correctly from one function call, and regenerates it the moment your data changes.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>New to gtsummary? Our <a href="../posts/gtsummary-tutorial-table1.html">step-by-step beginner tutorial</a> builds this table one verb at a time — with a <a href="../gtsummary-live-demo.html">live, editable demo</a> you can run in the browser. This post is the quick reference.</p>
</div>
</div>
<section id="the-one-liner" class="level2">
<h2 class="anchored" data-anchor-id="the-one-liner">The one-liner</h2>
<p>We’ll use the <code>trial</code> dataset that ships with <code>gtsummary</code> — a small clinical-trial example:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(gtsummary)</span>
<span id="cb1-2"></span>
<span id="cb1-3">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, grade, marker, stage) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> trt) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_p</span>()</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fmikthihkw" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#fmikthihkw table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#fmikthihkw thead, #fmikthihkw tbody, #fmikthihkw tfoot, #fmikthihkw tr, #fmikthihkw td, #fmikthihkw th {
  border-style: none;
}

#fmikthihkw p {
  margin: 0;
  padding: 0;
}

#fmikthihkw .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#fmikthihkw .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#fmikthihkw .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#fmikthihkw .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#fmikthihkw .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#fmikthihkw .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#fmikthihkw .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#fmikthihkw .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#fmikthihkw .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#fmikthihkw .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#fmikthihkw .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#fmikthihkw .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#fmikthihkw .gt_spanner_row {
  border-bottom-style: hidden;
}

#fmikthihkw .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#fmikthihkw .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#fmikthihkw .gt_from_md > :first-child {
  margin-top: 0;
}

#fmikthihkw .gt_from_md > :last-child {
  margin-bottom: 0;
}

#fmikthihkw .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#fmikthihkw .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#fmikthihkw .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#fmikthihkw .gt_row_group_first td {
  border-top-width: 2px;
}

#fmikthihkw .gt_row_group_first th {
  border-top-width: 2px;
}

#fmikthihkw .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#fmikthihkw .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#fmikthihkw .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#fmikthihkw .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#fmikthihkw .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#fmikthihkw .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#fmikthihkw .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#fmikthihkw .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#fmikthihkw .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#fmikthihkw .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#fmikthihkw .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#fmikthihkw .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#fmikthihkw .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#fmikthihkw .gt_left {
  text-align: left;
}

#fmikthihkw .gt_center {
  text-align: center;
}

#fmikthihkw .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#fmikthihkw .gt_font_normal {
  font-weight: normal;
}

#fmikthihkw .gt_font_bold {
  font-weight: bold;
}

#fmikthihkw .gt_font_italic {
  font-style: italic;
}

#fmikthihkw .gt_super {
  font-size: 65%;
}

#fmikthihkw .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#fmikthihkw .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#fmikthihkw .gt_indent_1 {
  text-indent: 5px;
}

#fmikthihkw .gt_indent_2 {
  text-indent: 10px;
}

#fmikthihkw .gt_indent_3 {
  text-indent: 15px;
}

#fmikthihkw .gt_indent_4 {
  text-indent: 20px;
}

#fmikthihkw .gt_indent_5 {
  text-indent: 25px;
}

#fmikthihkw .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#fmikthihkw div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<colgroup>
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug A</strong><br>
N = 98<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug B</strong><br>
N = 102<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="p.value" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>p-value</strong><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label">Age</td>
<td class="gt_row gt_center" headers="stat_1">46 (37, 60)</td>
<td class="gt_row gt_center" headers="stat_2">48 (39, 56)</td>
<td class="gt_row gt_center" headers="p.value">0.7</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_1">7</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">Grade</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_1">35 (36%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_1">32 (33%)</td>
<td class="gt_row gt_center" headers="stat_2">36 (35%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_1">31 (32%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">Marker Level (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_1">0.84 (0.23, 1.60)</td>
<td class="gt_row gt_center" headers="stat_2">0.52 (0.18, 1.21)</td>
<td class="gt_row gt_center" headers="p.value">0.085</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Unknown</td>
<td class="gt_row gt_center" headers="stat_1">6</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">T Stage</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T1</td>
<td class="gt_row gt_center" headers="stat_1">28 (29%)</td>
<td class="gt_row gt_center" headers="stat_2">25 (25%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T2</td>
<td class="gt_row gt_center" headers="stat_1">25 (26%)</td>
<td class="gt_row gt_center" headers="stat_2">29 (28%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T3</td>
<td class="gt_row gt_center" headers="stat_1">22 (22%)</td>
<td class="gt_row gt_center" headers="stat_2">21 (21%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;T4</td>
<td class="gt_row gt_center" headers="stat_1">23 (23%)</td>
<td class="gt_row gt_center" headers="stat_2">27 (26%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="4" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Median (Q1, Q3); n (%)</td>
</tr>
<tr class="gt_footnotes even">
<td colspan="4" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span> Wilcoxon rank sum test; Pearson’s Chi-squared test</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>That’s the whole table. <code>tbl_summary()</code> picks median (IQR) for continuous variables and n (%) for categoricals; <code>add_p()</code> chooses an appropriate test for each row (Wilcoxon, chi-squared, Fisher) and reports the p-value. No manual test selection, no transcription.</p>
</section>
<section id="making-it-publication-ready" class="level2">
<h2 class="anchored" data-anchor-id="making-it-publication-ready">Making it publication-ready</h2>
<p>A few extra verbs turn it into something you can paste straight into a manuscript:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">trial <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(trt, age, grade, marker) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tbl_summary</span>(</span>
<span id="cb2-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> trt,</span>
<span id="cb2-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">label =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(age <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age (years)"</span>, marker <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Marker (ng/mL)"</span>),</span>
<span id="cb2-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">statistic =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_continuous</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{mean} ({sd})"</span>),</span>
<span id="cb2-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">missing_text =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Missing"</span></span>
<span id="cb2-8">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_p</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">add_overall</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">bold_labels</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">modify_caption</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"**Table 1. Baseline characteristics by treatment**"</span>)</span></code></pre></div></div>
<div class="cell-output-display">
<div id="kvghylzdeu" style="padding-left:0px;padding-right:0px;padding-top:10px;padding-bottom:10px;overflow-x:auto;overflow-y:auto;width:auto;height:auto;">
<style>#kvghylzdeu table {
  font-family: system-ui, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

#kvghylzdeu thead, #kvghylzdeu tbody, #kvghylzdeu tfoot, #kvghylzdeu tr, #kvghylzdeu td, #kvghylzdeu th {
  border-style: none;
}

#kvghylzdeu p {
  margin: 0;
  padding: 0;
}

#kvghylzdeu .gt_table {
  display: table;
  border-collapse: collapse;
  line-height: normal;
  margin-left: auto;
  margin-right: auto;
  color: #333333;
  font-size: 16px;
  font-weight: normal;
  font-style: normal;
  background-color: #FFFFFF;
  width: auto;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #A8A8A8;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #A8A8A8;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
}

#kvghylzdeu .gt_caption {
  padding-top: 4px;
  padding-bottom: 4px;
}

#kvghylzdeu .gt_title {
  color: #333333;
  font-size: 125%;
  font-weight: initial;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-color: #FFFFFF;
  border-bottom-width: 0;
}

#kvghylzdeu .gt_subtitle {
  color: #333333;
  font-size: 85%;
  font-weight: initial;
  padding-top: 3px;
  padding-bottom: 5px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-color: #FFFFFF;
  border-top-width: 0;
}

#kvghylzdeu .gt_heading {
  background-color: #FFFFFF;
  text-align: center;
  border-bottom-color: #FFFFFF;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kvghylzdeu .gt_bottom_border {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kvghylzdeu .gt_col_headings {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
}

#kvghylzdeu .gt_col_heading {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 6px;
  padding-left: 5px;
  padding-right: 5px;
  overflow-x: hidden;
}

#kvghylzdeu .gt_column_spanner_outer {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: normal;
  text-transform: inherit;
  padding-top: 0;
  padding-bottom: 0;
  padding-left: 4px;
  padding-right: 4px;
}

#kvghylzdeu .gt_column_spanner_outer:first-child {
  padding-left: 0;
}

#kvghylzdeu .gt_column_spanner_outer:last-child {
  padding-right: 0;
}

#kvghylzdeu .gt_column_spanner {
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: bottom;
  padding-top: 5px;
  padding-bottom: 5px;
  overflow-x: hidden;
  display: inline-block;
  width: 100%;
}

#kvghylzdeu .gt_spanner_row {
  border-bottom-style: hidden;
}

#kvghylzdeu .gt_group_heading {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  text-align: left;
}

#kvghylzdeu .gt_empty_group_heading {
  padding: 0.5px;
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  vertical-align: middle;
}

#kvghylzdeu .gt_from_md > :first-child {
  margin-top: 0;
}

#kvghylzdeu .gt_from_md > :last-child {
  margin-bottom: 0;
}

#kvghylzdeu .gt_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  margin: 10px;
  border-top-style: solid;
  border-top-width: 1px;
  border-top-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 1px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 1px;
  border-right-color: #D3D3D3;
  vertical-align: middle;
  overflow-x: hidden;
}

#kvghylzdeu .gt_stub {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
}

#kvghylzdeu .gt_stub_row_group {
  color: #333333;
  background-color: #FFFFFF;
  font-size: 100%;
  font-weight: initial;
  text-transform: inherit;
  border-right-style: solid;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
  padding-left: 5px;
  padding-right: 5px;
  vertical-align: top;
}

#kvghylzdeu .gt_row_group_first td {
  border-top-width: 2px;
}

#kvghylzdeu .gt_row_group_first th {
  border-top-width: 2px;
}

#kvghylzdeu .gt_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kvghylzdeu .gt_first_summary_row {
  border-top-style: solid;
  border-top-color: #D3D3D3;
}

#kvghylzdeu .gt_first_summary_row.thick {
  border-top-width: 2px;
}

#kvghylzdeu .gt_last_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kvghylzdeu .gt_grand_summary_row {
  color: #333333;
  background-color: #FFFFFF;
  text-transform: inherit;
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
}

#kvghylzdeu .gt_first_grand_summary_row {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-top-style: double;
  border-top-width: 6px;
  border-top-color: #D3D3D3;
}

#kvghylzdeu .gt_last_grand_summary_row_top {
  padding-top: 8px;
  padding-bottom: 8px;
  padding-left: 5px;
  padding-right: 5px;
  border-bottom-style: double;
  border-bottom-width: 6px;
  border-bottom-color: #D3D3D3;
}

#kvghylzdeu .gt_striped {
  background-color: rgba(128, 128, 128, 0.05);
}

#kvghylzdeu .gt_table_body {
  border-top-style: solid;
  border-top-width: 2px;
  border-top-color: #D3D3D3;
  border-bottom-style: solid;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
}

#kvghylzdeu .gt_footnotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kvghylzdeu .gt_footnote {
  margin: 0px;
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kvghylzdeu .gt_sourcenotes {
  color: #333333;
  background-color: #FFFFFF;
  border-bottom-style: none;
  border-bottom-width: 2px;
  border-bottom-color: #D3D3D3;
  border-left-style: none;
  border-left-width: 2px;
  border-left-color: #D3D3D3;
  border-right-style: none;
  border-right-width: 2px;
  border-right-color: #D3D3D3;
}

#kvghylzdeu .gt_sourcenote {
  font-size: 90%;
  padding-top: 4px;
  padding-bottom: 4px;
  padding-left: 5px;
  padding-right: 5px;
}

#kvghylzdeu .gt_left {
  text-align: left;
}

#kvghylzdeu .gt_center {
  text-align: center;
}

#kvghylzdeu .gt_right {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

#kvghylzdeu .gt_font_normal {
  font-weight: normal;
}

#kvghylzdeu .gt_font_bold {
  font-weight: bold;
}

#kvghylzdeu .gt_font_italic {
  font-style: italic;
}

#kvghylzdeu .gt_super {
  font-size: 65%;
}

#kvghylzdeu .gt_footnote_marks {
  font-size: 75%;
  vertical-align: 0.4em;
  position: initial;
}

#kvghylzdeu .gt_asterisk {
  font-size: 100%;
  vertical-align: 0;
}

#kvghylzdeu .gt_indent_1 {
  text-indent: 5px;
}

#kvghylzdeu .gt_indent_2 {
  text-indent: 10px;
}

#kvghylzdeu .gt_indent_3 {
  text-indent: 15px;
}

#kvghylzdeu .gt_indent_4 {
  text-indent: 20px;
}

#kvghylzdeu .gt_indent_5 {
  text-indent: 25px;
}

#kvghylzdeu .katex-display {
  display: inline-flex !important;
  margin-bottom: 0.75em !important;
}

#kvghylzdeu div.Reactable > div.rt-table > div.rt-thead > div.rt-tr.rt-tr-group-header > div.rt-th-group:after {
  height: 0px !important;
}
</style>

<table class="gt_table caption-top table table-sm table-striped small" data-quarto-bootstrap="false">
<caption><strong>Table 1. Baseline characteristics by treatment</strong></caption>
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 20%">
</colgroup>
<thead>
<tr class="gt_col_headings header">
<th id="label" class="gt_col_heading gt_columns_bottom_border gt_left" data-quarto-table-cell-role="th" scope="col"><strong>Characteristic</strong></th>
<th id="stat_0" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Overall</strong><br>
N = 200<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_1" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug A</strong><br>
N = 98<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="stat_2" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>Drug B</strong><br>
N = 102<span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span></th>
<th id="p.value" class="gt_col_heading gt_columns_bottom_border gt_center" data-quarto-table-cell-role="th" scope="col"><strong>p-value</strong><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span></th>
</tr>
</thead>
<tbody class="gt_table_body">
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Age (years)</td>
<td class="gt_row gt_center" headers="stat_0">47 (14)</td>
<td class="gt_row gt_center" headers="stat_1">47 (15)</td>
<td class="gt_row gt_center" headers="stat_2">47 (14)</td>
<td class="gt_row gt_center" headers="p.value">0.7</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Missing</td>
<td class="gt_row gt_center" headers="stat_0">11</td>
<td class="gt_row gt_center" headers="stat_1">7</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Grade</td>
<td class="gt_row gt_center" headers="stat_0"><br>
</td>
<td class="gt_row gt_center" headers="stat_1"><br>
</td>
<td class="gt_row gt_center" headers="stat_2"><br>
</td>
<td class="gt_row gt_center" headers="p.value">0.9</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;I</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">35 (36%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;II</td>
<td class="gt_row gt_center" headers="stat_0">68 (34%)</td>
<td class="gt_row gt_center" headers="stat_1">32 (33%)</td>
<td class="gt_row gt_center" headers="stat_2">36 (35%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;III</td>
<td class="gt_row gt_center" headers="stat_0">64 (32%)</td>
<td class="gt_row gt_center" headers="stat_1">31 (32%)</td>
<td class="gt_row gt_center" headers="stat_2">33 (32%)</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
<tr class="odd">
<td class="gt_row gt_left" headers="label" style="font-weight: bold">Marker (ng/mL)</td>
<td class="gt_row gt_center" headers="stat_0">0.92 (0.86)</td>
<td class="gt_row gt_center" headers="stat_1">1.02 (0.89)</td>
<td class="gt_row gt_center" headers="stat_2">0.82 (0.83)</td>
<td class="gt_row gt_center" headers="p.value">0.085</td>
</tr>
<tr class="even">
<td class="gt_row gt_left" headers="label">&nbsp;&nbsp;&nbsp;&nbsp;Missing</td>
<td class="gt_row gt_center" headers="stat_0">10</td>
<td class="gt_row gt_center" headers="stat_1">6</td>
<td class="gt_row gt_center" headers="stat_2">4</td>
<td class="gt_row gt_center" headers="p.value"><br>
</td>
</tr>
</tbody><tfoot>
<tr class="gt_footnotes odd">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>1</sup></span> Mean (SD); n (%)</td>
</tr>
<tr class="gt_footnotes even">
<td colspan="5" class="gt_footnote"><span class="gt_footnote_marks" style="white-space:nowrap;font-style:italic;font-weight:normal;line-height:0;"><sup>2</sup></span> Wilcoxon rank sum test; Pearson’s Chi-squared test</td>
</tr>
</tfoot>

</table>

</div>
</div>
</div>
<p>Here we switched continuous summaries to mean (SD), relabelled variables, added an overall column, and counted missing values explicitly instead of dropping them silently — a small honesty that reviewers notice.</p>
</section>
<section id="why-it-matters" class="level2">
<h2 class="anchored" data-anchor-id="why-it-matters">Why it matters</h2>
<p>The value isn’t saving five minutes once; it’s that the table is <strong>code</strong>. Clean a data point, rerun, and every number updates — including the tests and the percentages. There is no window where the manuscript and the data disagree. That’s the whole philosophy behind <a href="../services.html">how we work</a>, applied to the humblest table in the paper.</p>
<hr>
<p><em>More clinical recipes live in our <a href="../clinical-r.html">Clinical R toolkit</a>. Need this on your own data? <a href="../contact.html">Get in touch</a>.</em></p>


</section>

 ]]></description>
  <category>clinical</category>
  <category>biostatistics</category>
  <category>gtsummary</category>
  <category>reporting</category>
  <guid>https://rverseanalytics.com/posts/table1-baseline-characteristics-gtsummary.html</guid>
  <pubDate>Wed, 08 Jul 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>The Quarto YAML header, explained field by field</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/quarto-yaml-header-explained.html</link>
  <description><![CDATA[ 




<p>Every Quarto document opens with a <strong>YAML header</strong> — the block between two lines of <code>---</code>. It’s the document’s control panel: change a line here and the whole output changes. Here’s what actually goes in it.</p>
<section id="the-essentials" class="level2">
<h2 class="anchored" data-anchor-id="the-essentials">The essentials</h2>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb1-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">title</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monthly Sales Report"</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">author</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Analytics Team"</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">date</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> today</span></span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> html</span></span>
<span id="cb1-6"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
<ul>
<li><strong><code>title</code>, <code>author</code></strong> — printed at the top of the rendered document.</li>
<li><strong><code>date: today</code></strong> — fills in the render date automatically; you can also write a fixed <code>2026-07-08</code>.</li>
<li><strong><code>format</code></strong> — the output type: <code>html</code>, <code>pdf</code>, <code>docx</code>, <code>revealjs</code> (slides), <code>typst</code>, and more.</li>
</ul>
</section>
<section id="format-options" class="level2">
<h2 class="anchored" data-anchor-id="format-options">Format options</h2>
<p>Most of the tuning lives <em>under</em> the format, indented one level:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb2-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb2-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb2-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">toc</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb2-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">number-sections</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb2-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> cosmo</span></span>
<span id="cb2-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">code-fold</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">true</span></span>
<span id="cb2-8"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">    </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fig-width</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span></span>
<span id="cb2-9"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
<ul>
<li><strong><code>toc: true</code></strong> — build a table of contents from your headings.</li>
<li><strong><code>number-sections: true</code></strong> — number the headings.</li>
<li><strong><code>theme</code></strong> — one of 25+ built-in Bootstrap themes.</li>
<li><strong><code>code-fold: true</code></strong> — hide code behind a “Show the code” toggle so readers see results first.</li>
<li><strong><code>fig-width</code> / <code>fig-height</code></strong> — default figure size in inches.</li>
</ul>
</section>
<section id="execution-and-parameters" class="level2">
<h2 class="anchored" data-anchor-id="execution-and-parameters">Execution and parameters</h2>
<p>Two blocks you’ll meet often. <code>execute</code> sets document-wide chunk behaviour; <code>params</code> declares inputs for <a href="../posts/parameterized-quarto-reports.html">parameterised reports</a>:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb3-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">execute</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">echo</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">false</span></span>
<span id="cb3-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">false</span></span>
<span id="cb3-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">params</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-6"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">region</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"West"</span></span>
<span id="cb3-7"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">year</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</span></span>
<span id="cb3-8"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
<p>Here every chunk hides its code and warnings by default, and <code>params$region</code> / <code>params$year</code> are available throughout the document.</p>
</section>
<section id="multiple-outputs-at-once" class="level2">
<h2 class="anchored" data-anchor-id="multiple-outputs-at-once">Multiple outputs at once</h2>
<p>List several formats and one render builds them all:</p>
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb4-1"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb4-3"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">html</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> default</span></span>
<span id="cb4-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pdf</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> default</span></span>
<span id="cb4-5"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">docx</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> default</span></span>
<span id="cb4-6"><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">---</span></span></code></pre></div></div>
</section>
<section id="the-one-rule-that-prevents-most-errors" class="level2">
<h2 class="anchored" data-anchor-id="the-one-rule-that-prevents-most-errors">The one rule that prevents most errors</h2>
<p>YAML is picky about whitespace. <strong>Indent with spaces, never tabs</strong>, and keep a format’s settings indented beneath it — <code>toc</code> under <code>html:</code>, not at the left margin. If a render fails with a cryptic YAML message, a misaligned space is almost always the cause.</p>
<p>Want the interactive version? Work through <a href="../learn-quarto/yaml-header.html">the YAML header lesson</a> in our free Learn Quarto course.</p>
<hr>
<p><em>Need a report template your whole team can render on demand? <a href="../services.html">That’s what we build</a>.</em></p>


</section>

 ]]></description>
  <category>quarto</category>
  <category>workflow</category>
  <guid>https://rverseanalytics.com/posts/quarto-yaml-header-explained.html</guid>
  <pubDate>Tue, 07 Jul 2026 21:00:00 GMT</pubDate>
</item>
<item>
  <title>What to know before hiring an R consultant</title>
  <dc:creator>Rverse Analytics</dc:creator>
  <link>https://rverseanalytics.com/posts/hiring-an-r-consultant.html</link>
  <description><![CDATA[ 




<p>Hiring help with an analysis is easy; hiring help you don’t regret in six months is harder. The difference is rarely statistical skill — it’s how the work is <em>delivered</em>. Here is what we’d ask before signing anything.</p>
<section id="will-you-own-the-code-or-just-the-output" class="level2">
<h2 class="anchored" data-anchor-id="will-you-own-the-code-or-just-the-output">Will you own the code — or just the output?</h2>
<p>A results table is a dead end. A <strong>script that produces the table</strong> is an asset: rerun it on cleaned data, adapt it for the next study, hand it to a colleague. Ask explicitly whether you receive the code, whether it runs end-to-end from raw data, and whether you’re free to reuse it. If the answer is “we’ll send you the figures,” you’re renting, not owning.</p>
</section>
<section id="is-it-reproducible-or-a-pile-of-manual-steps" class="level2">
<h2 class="anchored" data-anchor-id="is-it-reproducible-or-a-pile-of-manual-steps">Is it reproducible, or a pile of manual steps?</h2>
<p>“Reproducible” has a concrete test: can someone else regenerate every number and figure with a single command? A good pipeline makes the cost of a data correction almost zero — rerun and you’re done. A bad one makes it a day of re-clicking. The economics compound over a project’s life:</p>
<div class="cell">
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb1-2"></span>
<span id="cb1-3">reruns <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span></span>
<span id="cb1-4">manual      <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">45</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> reruns          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ~45 min of clicking each time</span></span>
<span id="cb1-5">reproducible <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> reruns     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># one-off setup, then ~1 min per rerun</span></span>
<span id="cb1-6"></span>
<span id="cb1-7">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb1-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">reruns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(reruns, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb1-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">minutes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(manual, reproducible),</span>
<span id="cb1-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">workflow =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Manual"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Reproducible"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">each =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(reruns))</span>
<span id="cb1-11">)</span>
<span id="cb1-12"></span>
<span id="cb1-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(d, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(reruns, minutes, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> workflow)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linewidth =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_colour_manual</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Manual =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#b02a37"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Reproducible =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#2f6fed"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb1-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">title =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Why reproducibility pays off after the first correction"</span>,</span>
<span id="cb1-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of data updates / reruns"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cumulative minutes spent"</span>,</span>
<span id="cb1-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span></span>
<span id="cb1-20">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_family =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sans"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-22">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>,</span>
<span id="cb1-23">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">plot.title =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_text</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">face =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bold"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colour =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#1b2a4a"</span>))</span></code></pre></div></div>
<div class="cell-output-display">
<div id="fig-rerun-cost" class="quarto-float quarto-figure quarto-figure-center anchored" width="720">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-rerun-cost-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rverseanalytics.com/posts/hiring-an-r-consultant_files/figure-html/fig-rerun-cost-1.png" id="fig-rerun-cost" class="img-fluid figure-img" width="720">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig quarto-uncaptioned" id="fig-rerun-cost-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1
</figcaption>
</figure>
</div>
</div>
</div>
<p>The reproducible line starts higher — there’s real setup — and then barely moves. Any project touched more than once comes out ahead.</p>
</section>
<section id="is-the-scope-in-writing" class="level2">
<h2 class="anchored" data-anchor-id="is-the-scope-in-writing">Is the scope in writing?</h2>
<p>Vague scope is where consulting relationships go wrong. Before work starts you should have, in writing: the question, the data, the deliverable, the timeline and the price. Fixed-scope milestones beat an open-ended hourly meter for everyone — you know the cost, and the consultant knows what “done” means.</p>
</section>
<section id="will-there-be-a-handover" class="level2">
<h2 class="anchored" data-anchor-id="will-there-be-a-handover">Will there be a handover?</h2>
<p>The last hour of a good engagement is a walkthrough: here’s the code, here’s how to rerun it, here’s what to change if your data grows. Without it, you inherit a black box. With it, your team is genuinely more capable than before.</p>
<hr>
<p><em>This is exactly how we work — see <a href="../services.html">our services</a> or the <a href="../faq.html">FAQ</a> for the details, then <a href="../contact.html">get in touch</a>.</em></p>


</section>

 ]]></description>
  <category>consulting</category>
  <category>reproducibility</category>
  <category>workflow</category>
  <guid>https://rverseanalytics.com/posts/hiring-an-r-consultant.html</guid>
  <pubDate>Tue, 07 Jul 2026 21:00:00 GMT</pubDate>
</item>
</channel>
</rss>
