<?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>Steve&#39;s Data Tips and Tricks</title>
<link>https://www.spsanderson.com/steveondata/</link>
<atom:link href="https://www.spsanderson.com/steveondata/index.xml" rel="self" type="application/rss+xml"/>
<description>Steve&#39;s Data Tips and Tricks in R, C, SQL and Linux</description>
<generator>quarto-1.7.32</generator>
<lastBuildDate>Mon, 01 Dec 2025 05:00:00 GMT</lastBuildDate>
<item>
  <title>How to Remove Column Names in R: Complete Guide to unname() and Alternative Methods</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-12-01/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong><br>
Learn the most effective ways to remove column names in R using <code>unname()</code>, <code>colnames()</code>, and other base R functions. This guide provides working code examples, practical tips, and best practices for R programmers working with data frames and matrices.</p>
</blockquote>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Working with <strong>R dataframes and matrices</strong> often means manipulating column names for data processing, exporting, or performance. Whether you need to remove headers for compatibility or simply want a cleaner structure, R offers several ways to remove column names. In this guide, you’ll learn how to use <code>unname()</code>, <code>colnames()</code>, and related functions, with clear examples and practical advice.</p>
</section>
<section id="understanding-column-names-in-r" class="level1">
<h1>Understanding Column Names in R</h1>
<p>Column names (sometimes called headers) are labels for each column in a dataframe or matrix. They help identify data, but sometimes you need to remove them—for example, before exporting data to a system that doesn’t expect headers, or for certain performance optimizations.</p>
</section>
<section id="method-1-using-unname-function" class="level1">
<h1>Method 1: Using <code>unname()</code> Function</h1>
<section id="basic-syntax" class="level2">
<h2 class="anchored" data-anchor-id="basic-syntax">Basic Syntax</h2>
<div class="sourceCode" 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;">unname</span>(object, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">force =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<ul>
<li><code>object</code>: The R object (vector, matrix, data frame, etc.)</li>
<li><code>force</code>: If <code>TRUE</code>, removes names even from data frames</li>
</ul>
<section id="removing-names-from-matrices" class="level3">
<h3 class="anchored" data-anchor-id="removing-names-from-matrices">Removing Names from Matrices</h3>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a matrix with column names</span></span>
<span id="cb2-2">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;">matrix</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;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(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;">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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>)</span>
<span id="cb2-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     A B C
[1,] 1 3 5
[2,] 2 4 6</code></pre>
</div>
<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;"># Remove column names using unname()</span></span>
<span id="cb4-2">m2 <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;">unname</span>(m)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(m2)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6</code></pre>
</div>
<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;">colnames</span>(m2) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Returns NULL</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
</div>
<p>This method works perfectly for matrices .</p>
</section>
<section id="handling-data-frames" class="level3">
<h3 class="anchored" data-anchor-id="handling-data-frames">Handling Data Frames</h3>
<p>By default, <code>unname()</code> does <strong>not</strong> remove row names from data frames, also if you use <code>force = TRUE</code> it will probably fail as a <code>data.frame</code> needs at least one dimname. However, you can try on something like a matrix:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a matrix with row and column names</span></span>
<span id="cb8-2">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;">matrix</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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"row2"</span>)</span>
<span id="cb8-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"col2"</span>)</span>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View the matrix with names</span></span>
<span id="cb8-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     col1 col2
row1    1    3
row2    2    4</code></pre>
</div>
<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;">dimnames</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] "row1" "row2"

[[2]]
[1] "col1" "col2"</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove all names using unname() with force = TRUE</span></span>
<span id="cb12-2">m2 <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;">unname</span>(m, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">force =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-3"></span>
<span id="cb12-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View the result</span></span>
<span id="cb12-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(m2)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2]
[1,]    1    3
[2,]    2    4</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dimnames</span>(m2)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p><strong>Note:</strong> Data frames in R are expected to have valid column names. Removing them can cause errors or unexpected behavior.</p>
</blockquote>
</section>
</section>
</section>
<section id="method-2-setting-colnames-to-null" class="level1">
<h1>Method 2: Setting <code>colnames()</code> to NULL</h1>
<section id="for-matrices" class="level2">
<h2 class="anchored" data-anchor-id="for-matrices">For Matrices</h2>
<div class="cell">
<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">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;">matrix</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;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb16-2"></span>
<span id="cb16-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(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;">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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>)</span>
<span id="cb16-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The column names are: "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(m), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>The column names are:  A B C </code></pre>
</div>
<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;">colnames</span>(m) <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;">NULL</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6</code></pre>
</div>
<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;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">The column names are:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(m), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Returns NULL</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
The column names are: </code></pre>
</div>
</div>
<p>This is a simple and reliable way to remove column names from matrices .</p>
</section>
<section id="for-data-frames" class="level2">
<h2 class="anchored" data-anchor-id="for-data-frames">For Data Frames</h2>
<div class="cell">
<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">df <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;">a =</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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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>
<span id="cb22-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(df) <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;">NULL</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Error: invalid 'names' attribute</span></span>
<span id="cb22-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
<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;">names</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dimnames</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] "1" "2" "3"

[[2]]
NULL</code></pre>
</div>
</div>
</section>
</section>
<section id="method-3-using-names-and-setnames" class="level1">
<h1>Method 3: Using <code>names()</code> and <code>setNames()</code></h1>
<section id="for-vectors-and-lists" class="level2">
<h2 class="anchored" data-anchor-id="for-vectors-and-lists">For Vectors and Lists</h2>
<div class="cell">
<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">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="at" style="color: #657422;
background-color: null;
font-style: inherit;">a =</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;">b =</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;">c =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb28-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(v) <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;">NULL</span></span>
<span id="cb28-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(v)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [1] 1 2 3</span></span>
<span id="cb30-2"></span>
<span id="cb30-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Or using setNames()</span></span>
<span id="cb30-4">v2 <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;">setNames</span>(v, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>)</span>
<span id="cb30-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(v2)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># [1] 1 2 3</span></span></code></pre></div>
</div>
<p>These methods are best for vectors and lists .</p>
</section>
</section>
<section id="removing-column-names-from-data-frames-the-safe-way" class="level1">
<h1>Removing Column Names from Data Frames: The Safe Way</h1>
<p>Since R data frames must have column names, the safest way to “remove” them is to convert the data frame to a matrix:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1">df <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;">a =</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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">b =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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>
<span id="cb33-2">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;">as.matrix</span>(df)</span>
<span id="cb33-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(m) <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;">NULL</span></span>
<span id="cb33-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6</code></pre>
</div>
</div>
<p>This approach is widely used in practice .</p>
</section>
<section id="comparison-table-methods-for-removing-column-names" class="level1">
<h1>Comparison Table: Methods for Removing Column Names</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 11%">
<col style="width: 22%">
<col style="width: 35%">
<col style="width: 30%">
</colgroup>
<thead>
<tr class="header">
<th>Object Type</th>
<th>Method</th>
<th>Example Code</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Matrix</td>
<td><code>colnames(m) &lt;- NULL</code></td>
<td><code>colnames(m) &lt;- NULL</code></td>
<td>Removes column names</td>
</tr>
<tr class="even">
<td>Matrix</td>
<td><code>unname(m)</code></td>
<td><code>m2 &lt;- unname(m)</code></td>
<td>Removes dimnames</td>
</tr>
<tr class="odd">
<td>Data Frame</td>
<td>Convert to matrix, then remove</td>
<td><code>m &lt;- as.matrix(df); colnames(m) &lt;- NULL</code></td>
<td>Safe workaround</td>
</tr>
<tr class="even">
<td>Vector/List</td>
<td><code>names(x) &lt;- NULL</code></td>
<td><code>names(v) &lt;- NULL</code></td>
<td>Removes names</td>
</tr>
<tr class="odd">
<td>Vector/List</td>
<td><code>unname(x)</code></td>
<td><code>v2 &lt;- unname(v)</code></td>
<td>Removes names</td>
</tr>
<tr class="even">
<td>Any</td>
<td><code>setNames(x, NULL)</code></td>
<td><code>setNames(v, NULL)</code></td>
<td>Removes names</td>
</tr>
</tbody>
</table>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p><strong>Task:</strong><br>
Given the following matrix, remove its column names and print the result.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1">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;">matrix</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;">9</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb35-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>, <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;">"Z"</span>)</span></code></pre></div>
</div>
<details>
<summary>
Click here for Solution!
</summary>
<div class="cell">
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove column names</span></span>
<span id="cb36-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(m) <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;">NULL</span></span>
<span id="cb36-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(m)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9</code></pre>
</div>
</div>
</details>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<ul>
<li>✅ Use <code>colnames(m) &lt;- NULL</code> or <code>unname(m)</code> to remove column names from matrices.</li>
<li>✅ Data frames in R require column names; to remove them, convert to a matrix first.</li>
<li>✅ For vectors and lists, use <code>names(x) &lt;- NULL</code> or <code>unname(x)</code>.</li>
<li>✅ The <code>unname()</code> function with <code>force = TRUE</code> can remove names from data frames, but may not be supported in all R versions.</li>
<li>✅ Always check your object structure after removing names, especially for data frames.</li>
</ul>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Removing column names in R is straightforward for matrices and vectors, but requires care with data frames. Use <code>colnames()</code> or <code>unname()</code> for matrices, and always convert data frames to matrices if you need to strip headers. These techniques help you prepare data for export, improve performance, or meet specific analysis requirements.</p>
<p><strong>Ready to streamline your R data workflows? Try these methods in your next project!</strong></p>
</section>
<section id="frequently-asked-questions" class="level1">
<h1>Frequently Asked Questions</h1>
<p><strong>1. Can I remove column names from a data frame in R?</strong><br>
No, R data frames require valid column names. To remove them, convert the data frame to a matrix first.</p>
<p><strong>2. What is the difference between <code>unname()</code> and <code>colnames() &lt;- NULL</code>?</strong><br>
Both remove column names from matrices, but <code>unname()</code> can also remove names from vectors and lists.</p>
<p><strong>3. Will removing column names affect my data?</strong><br>
No, the data remains unchanged, but you lose the labels that help identify columns.</p>
<p><strong>4. Can I remove row names the same way?</strong><br>
Yes, use <code>rownames(m) &lt;- NULL</code> for matrices, or <code>unname()</code> for vectors.</p>
<p><strong>5. Is it safe to use <code>unname(df, force = TRUE)</code> on data frames?</strong><br>
It may work in some R versions, but can cause errors. The safest approach is to convert to a matrix first.</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li>R Documentation: <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/unname.html">unname() function</a></li>
<li>R Documentation: <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/colnames.html">colnames() function</a></li>
<li>R Documentation: <a href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/names.html">names() function</a></li>
</ol>
<hr>
<blockquote class="blockquote">
<p><strong>Enjoyed this guide?</strong><br>
Share your feedback below and connect with fellow R programmers!<br>
If you found this helpful, share</p>
</blockquote>
<hr>
<p>Happy Coding! 🚀</p>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<pre class="text"><code>@online{
  how_to_remove_column_names_in_r_complete_guide_to_unname_and_alternative_methods_20251201,
  author = {Sanderson II MPH, Steven P.},
  title = {How to Remove Column Names in R: Complete Guide to unname() and Alternative Methods},
  date = {2025-12-01},
  url = {https://www.spsanderson.com/steveondata/posts/2025-12-01/},
  langid = {en}
  }</code></pre>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-12-01/</guid>
  <pubDate>Mon, 01 Dec 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>For-Loop with Range in R: A Complete Guide with Practical Examples</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-11-17/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong><br>
Mastering for-loops with ranges in R empowers you to process data, automate repetitive tasks, and write clear, efficient code. This guide covers everything from basic syntax to advanced techniques, with plenty of working examples and best practices.</p>
</blockquote>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>For-loops are a cornerstone of R programming, allowing you to repeat actions for each element in a sequence or range. Whether you’re iterating over numbers, vectors, or data frames, understanding how to use ranges in for-loops is essential for data analysis, automation, and algorithm development.</p>
<p>This article will walk you through the essentials of for-loops with ranges in R, from simple counting to advanced applications, with clear, working code examples and practical tips.</p>
</section>
<section id="what-is-a-for-loop-in-r" class="level1">
<h1>What is a For-Loop in R?</h1>
<p>A <strong>for-loop</strong> in R is a control structure that repeats a block of code for each value in a sequence (range). It’s especially useful when you need to perform repetitive tasks, process data row by row, or apply custom logic that isn’t easily vectorized .</p>
</section>
<section id="basic-for-loop-syntax" class="level1">
<h1>Basic For-Loop Syntax</h1>
<p>The general syntax is:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (variable <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sequence) {</span>
<span id="cb1-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Code to execute</span></span>
<span id="cb1-3">}</span></code></pre></div>
<ul>
<li><code>variable</code> takes each value from <code>sequence</code> in turn.</li>
<li>The code inside the braces <code>{}</code> runs for each value.</li>
</ul>
</section>
<section id="creating-ranges-colon-operator-and-seq" class="level1">
<h1>Creating Ranges: Colon Operator and seq()</h1>
<section id="the-colon-operator" class="level2">
<h2 class="anchored" data-anchor-id="the-colon-operator">The Colon Operator (<code>:</code>)</h2>
<p>The simplest way to create a range in R is with the colon operator:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><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;">5</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creates the sequence 1, 2, 3, 4, 5</span></span>
<span id="cb2-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;">1</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Creates the sequence 10, 9, 8, ..., 1</span></span></code></pre></div>
</section>
<section id="the-seq-function" class="level2">
<h2 class="anchored" data-anchor-id="the-seq-function">The seq() Function</h2>
<p>For more control (custom steps, decimals), use <code>seq()</code>:</p>
<div class="cell">
<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;">seq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</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;">to =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0, 2, 4, 6, 8, 10</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  0  2  4  6  8 10</code></pre>
</div>
<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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)               <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 0.0, 0.1, ..., 1.0</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0</code></pre>
</div>
<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;">seq_along</span>(vector)                 <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Indices for a vector</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div>
</section>
</section>
<section id="working-examples-from-simple-to-advanced" class="level1">
<h1>Working Examples: From Simple to Advanced</h1>
<section id="basic-counting-with-a-for-loop" class="level2">
<h2 class="anchored" data-anchor-id="basic-counting-with-a-for-loop">1. Basic Counting with a For-Loop</h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Count from 1 to 10</span></span>
<span id="cb9-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">10</span>) {</span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Count:"</span>, i))</span>
<span id="cb9-4">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Count: 1"
[1] "Count: 2"
[1] "Count: 3"
[1] "Count: 4"
[1] "Count: 5"
[1] "Count: 6"
[1] "Count: 7"
[1] "Count: 8"
[1] "Count: 9"
[1] "Count: 10"</code></pre>
</div>
</div>
</section>
<section id="descending-ranges" class="level2">
<h2 class="anchored" data-anchor-id="descending-ranges">2. Descending Ranges</h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Countdown from 10 to 1</span></span>
<span id="cb11-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Countdown:"</span>, i))</span>
<span id="cb11-4">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Countdown: 10"
[1] "Countdown: 9"
[1] "Countdown: 8"
[1] "Countdown: 7"
[1] "Countdown: 6"
[1] "Countdown: 5"
[1] "Countdown: 4"
[1] "Countdown: 3"
[1] "Countdown: 2"
[1] "Countdown: 1"</code></pre>
</div>
</div>
</section>
<section id="custom-step-sizes-with-seq" class="level2">
<h2 class="anchored" data-anchor-id="custom-step-sizes-with-seq">3. Custom Step Sizes with seq()</h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Even numbers from 0 to 20</span></span>
<span id="cb13-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)) {</span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Even number:"</span>, i))</span>
<span id="cb13-4">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Even number: 0"
[1] "Even number: 2"
[1] "Even number: 4"
[1] "Even number: 6"
[1] "Even number: 8"
[1] "Even number: 10"
[1] "Even number: 12"
[1] "Even number: 14"
[1] "Even number: 16"
[1] "Even number: 18"
[1] "Even number: 20"</code></pre>
</div>
</div>
</section>
<section id="decimal-sequences" class="level2">
<h2 class="anchored" data-anchor-id="decimal-sequences">4. Decimal Sequences</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sine values for decimals between 0 and 1</span></span>
<span id="cb15-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (x <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">by =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)) {</span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"sin("</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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">") ="</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;">sin</span>(x), <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)))</span>
<span id="cb15-4">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "sin( 0 ) = 0"
[1] "sin( 0.1 ) = 0.1"
[1] "sin( 0.2 ) = 0.199"
[1] "sin( 0.3 ) = 0.296"
[1] "sin( 0.4 ) = 0.389"
[1] "sin( 0.5 ) = 0.479"
[1] "sin( 0.6 ) = 0.565"
[1] "sin( 0.7 ) = 0.644"
[1] "sin( 0.8 ) = 0.717"
[1] "sin( 0.9 ) = 0.783"
[1] "sin( 1 ) = 0.841"</code></pre>
</div>
</div>
</section>
<section id="iterating-over-vectors" class="level2">
<h2 class="anchored" data-anchor-id="iterating-over-vectors">5. Iterating Over Vectors</h2>
<section id="a.-direct-value-iteration" class="level3">
<h3 class="anchored" data-anchor-id="a.-direct-value-iteration">a. Direct Value Iteration</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1">fruits <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;">"apple"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"banana"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cherry"</span>)</span>
<span id="cb17-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (fruit <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> fruits) {</span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"I like"</span>, fruit))</span>
<span id="cb17-4">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "I like apple"
[1] "I like banana"
[1] "I like cherry"</code></pre>
</div>
</div>
</section>
<section id="b.-index-based-iteration" class="level3">
<h3 class="anchored" data-anchor-id="b.-index-based-iteration">b. Index-Based Iteration</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">scores <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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>)</span>
<span id="cb19-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(scores)) {</span>
<span id="cb19-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Score"</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is"</span>, scores[i]))</span>
<span id="cb19-4">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Score 1 is 85"
[1] "Score 2 is 92"
[1] "Score 3 is 78"</code></pre>
</div>
</div>
</section>
</section>
<section id="nested-for-loops" class="level2">
<h2 class="anchored" data-anchor-id="nested-for-loops">6. Nested For-Loops</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multiplication table (1-3)</span></span>
<span id="cb21-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">3</span>) {</span>
<span id="cb21-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">3</span>) {</span>
<span id="cb21-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</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;">"%d x %d = %d</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>, i, j, i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>j))</span>
<span id="cb21-5">  }</span>
<span id="cb21-6">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9</code></pre>
</div>
</div>
</section>
<section id="iterating-over-data-frames" class="level2">
<h2 class="anchored" data-anchor-id="iterating-over-data-frames">7. Iterating Over Data Frames</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">employees <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="cb23-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</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;">"Alice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bob"</span>),</span>
<span id="cb23-3">  <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;">25</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)</span>
<span id="cb23-4">)</span>
<span id="cb23-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(employees)) {</span>
<span id="cb23-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(employees<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>name[i], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"is"</span>, employees<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>age[i], <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"years old"</span>))</span>
<span id="cb23-7">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Alice is 25 years old"
[1] "Bob is 30 years old"</code></pre>
</div>
</div>
</section>
<section id="practical-application-categorizing-data" class="level2">
<h2 class="anchored" data-anchor-id="practical-application-categorizing-data">8. Practical Application: Categorizing Data</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1">temps <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;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</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;">30</span>)</span>
<span id="cb25-2">hot_days <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>
<span id="cb25-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq_along</span>(temps)) {</span>
<span id="cb25-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (temps[i] <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;">25</span>) {</span>
<span id="cb25-5">    hot_days <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>(hot_days, i)</span>
<span id="cb25-6">  }</span>
<span id="cb25-7">}</span>
<span id="cb25-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Hot days at positions:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(hot_days, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">", "</span>)))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Hot days at positions: 3, 4"</code></pre>
</div>
</div>
</section>
<section id="memory-pre-allocation-best-practice" class="level2">
<h2 class="anchored" data-anchor-id="memory-pre-allocation-best-practice">9. Memory Pre-Allocation (Best Practice)</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1">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;">10</span></span>
<span id="cb27-2">squares <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;">numeric</span>(n)</span>
<span id="cb27-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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>n) {</span>
<span id="cb27-4">  squares[i] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> i<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>
<span id="cb27-5">}</span>
<span id="cb27-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(squares)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1]   1   4   9  16  25  36  49  64  81 100</code></pre>
</div>
</div>
</section>
<section id="loop-control-break-and-next" class="level2">
<h2 class="anchored" data-anchor-id="loop-control-break-and-next">10. Loop Control: break and next</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1">numbers <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="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb29-2">sum_pos <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="cb29-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (num <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> numbers) {</span>
<span id="cb29-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (num <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;">0</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">next</span></span>
<span id="cb29-5">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (num <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;">0</span>) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">break</span></span>
<span id="cb29-6">  sum_pos <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> sum_pos <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> num</span>
<span id="cb29-7">}</span>
<span id="cb29-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(sum_pos)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 6</code></pre>
</div>
</div>
</section>
</section>
<section id="best-practices-and-common-pitfalls" class="level1">
<h1>Best Practices and Common Pitfalls</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 26%">
<col style="width: 44%">
<col style="width: 29%">
</colgroup>
<thead>
<tr class="header">
<th>Practice</th>
<th>Do This</th>
<th>Avoid This</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Use <code>seq_along()</code></td>
<td><code>for (i in seq_along(vec))</code></td>
<td><code>for (i in 1:length(vec))</code> (fails if empty)</td>
</tr>
<tr class="even">
<td>Pre-allocate vectors</td>
<td><code>numeric(n)</code></td>
<td>Growing vectors in loop</td>
</tr>
<tr class="odd">
<td>Check range direction</td>
<td><code>1:10</code> or <code>10:1</code></td>
<td><code>1:0</code> (creates c(1,0))</td>
</tr>
<tr class="even">
<td>Use vectorization</td>
<td>When possible, for speed</td>
<td>Overusing for-loops</td>
</tr>
</tbody>
</table>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p>Write a for-loop that calculates the running average of a numeric vector.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Given vector</span></span>
<span id="cb31-2">scores <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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">94</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">87</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>)</span>
<span id="cb31-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here</span></span></code></pre></div>
</div>
<details>
<summary>
<strong>Click here for Solution!</strong>
</summary>
<div class="cell">
<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">running_avg <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;">numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(scores))</span>
<span id="cb32-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(scores)) {</span>
<span id="cb32-3">  running_avg[i] <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;">mean</span>(scores[<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>i])</span>
<span id="cb32-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Position"</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"- Running Average:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(running_avg[i], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)))</span>
<span id="cb32-5">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Position 1 - Running Average: 85"
[1] "Position 2 - Running Average: 88.5"
[1] "Position 3 - Running Average: 85"
[1] "Position 4 - Running Average: 87.75"
[1] "Position 5 - Running Average: 88"
[1] "Position 6 - Running Average: 89"
[1] "Position 7 - Running Average: 88.71"
[1] "Position 8 - Running Average: 89"</code></pre>
</div>
</div>
</details>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<ul>
<li>Use <code>:</code> for simple integer ranges, <code>seq()</code> for custom steps or decimals.</li>
<li>Prefer <code>seq_along()</code> over <code>1:length()</code> for safe iteration.</li>
<li>Pre-allocate vectors for better performance.</li>
<li>Use <code>break</code> and <code>next</code> for flexible loop control.</li>
<li>For-loops are great for custom logic; use vectorized functions when possible.</li>
</ul>
</section>
<section id="faqs" class="level1">
<h1>FAQs</h1>
<p><strong>Q1: When should I use <code>:</code> vs <code>seq()</code>?</strong><br>
A1: Use <code>:</code> for simple, consecutive integers. Use <code>seq()</code> for custom steps, decimals, or non-integer sequences.</p>
<p><strong>Q2: Can I change the loop variable inside the loop?</strong><br>
A2: You can, but it won’t affect the sequence of values the loop iterates over.</p>
<p><strong>Q3: What’s the difference between for-loops and apply functions?</strong><br>
A3: For-loops offer explicit control and are easier to debug; <code>apply</code> functions are concise and often faster for simple operations.</p>
<p><strong>Q4: How do I avoid errors with empty vectors?</strong><br>
A4: Use <code>seq_along()</code> or <code>seq_len()</code> instead of <code>1:length()</code> to handle empty vectors safely.</p>
<p><strong>Q5: What happens if I use <code>1:0</code> in a for-loop?</strong><br>
A5: It creates the sequence <code>c(1, 0)</code>, so the loop runs twice. Use <code>seq_len()</code> for zero-length safety.</p>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>For-loops with ranges are a powerful tool in R, enabling you to automate repetitive tasks, process data, and implement custom logic. By mastering range creation, safe iteration, and best practices, you’ll write more robust and efficient R code. Practice with the examples above and integrate these techniques into your projects for better results.</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><a href="https://cran.r-project.org/doc/manuals/r-release/R-intro.html">R Core Team. (2024). An Introduction to R. CRAN.</a></li>
<li><a href="https://r4ds.had.co.nz/iteration.html">Wickham, H., &amp; Grolemund, G. (2023). R for Data Science (2nd Edition).</a></li>
<li><a href="https://datacarpentry.github.io/semester-biology/materials/for-loops-R/">Data Carpentry. (2022). For Loops in R.</a></li>
<li><a href="https://www.geeksforgeeks.org/for-loop-in-r/">GeeksforGeeks. (2020). For loop in R.</a></li>
</ol>
<blockquote class="blockquote">
<p><strong>Enjoyed this guide?</strong><br>
Share it with your fellow R programmers and bookmark it for future reference!</p>
</blockquote>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-11-17/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>More Loops in R</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<pre><code>@online{
  for_loop_with_range_in_r_a_complete_guide_with_practical_examples_20251117,
  author = {Sanderson II MPH, Steven P.},
  title = {For-Loop with Range in R: A Complete Guide with Practical Examples},
  date = {2025-11-17},
  url = {https://www.spsanderson.com/steveondata/posts/2025-11-17/},
  langid = {en}
  }</code></pre>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-11-17/</guid>
  <pubDate>Mon, 17 Nov 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>Sending Email and Text Messages with Python: A Beginner’s Complete Guide</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-11-06/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong><br>
This guide will teach you how to send emails and text messages using Python, with step-by-step instructions, practical code examples, and troubleshooting tips—perfect for beginners!</p>
</blockquote>
<p>*Author’s Note: I’m learning as I write this series! That means you might spot mistakes or see things done in a way that could be improved. If you have suggestions or spot errors, please let me know.**</p>
<section id="why-use-python-for-email-and-sms" class="level1">
<h1>Why Use Python for Email and SMS?</h1>
<p>Python is one of the most popular programming languages for automating tasks—including sending emails and text messages. With just a few lines of code, you can:</p>
<ul>
<li>Send notifications automatically</li>
<li>Alert yourself about important events</li>
<li>Build contact forms or customer support tools</li>
<li>Save time by automating repetitive communication</li>
</ul>
</section>
<section id="what-you-need-to-get-started" class="level1">
<h1>What You Need to Get Started</h1>
<ul>
<li><strong>Python 3.x</strong> installed on your computer</li>
<li>A code editor (like VS Code, PyCharm, or even Notepad++)</li>
<li>Internet access</li>
<li>For emails: a Gmail (or other SMTP) account</li>
<li>For SMS: a Twilio account (free trial available)</li>
</ul>
<p><strong>Required Libraries:</strong> - <code>smtplib</code> (built-in for emails) - <code>email</code> (built-in for formatting emails) - <code>twilio</code> (install with <code>pip install twilio</code> for SMS)</p>
</section>
<section id="sending-emails-with-python-step-by-step" class="level1">
<h1>Sending Emails with Python (Step-by-Step)</h1>
<section id="understanding-smtplib" class="level2">
<h2 class="anchored" data-anchor-id="understanding-smtplib">Understanding smtplib</h2>
<p><code>smtplib</code> is Python’s built-in library for sending emails using the Simple Mail Transfer Protocol (SMTP). It lets you connect to email servers (like Gmail) and send messages programmatically.</p>
</section>
<section id="setting-up-gmail-smtp" class="level2">
<h2 class="anchored" data-anchor-id="setting-up-gmail-smtp">Setting Up Gmail SMTP</h2>
<p>To send emails through Gmail:</p>
<ol type="1">
<li><strong>Enable 2-Step Verification</strong> on your Google account.</li>
<li><strong>Create an App Password</strong> (not your regular password) for your Python script.</li>
<li>Use these SMTP settings:
<ul>
<li>Server: <code>smtp.gmail.com</code></li>
<li>Port: <code>587</code> (TLS)</li>
</ul></li>
</ol>
</section>
<section id="your-first-email-script" class="level2">
<h2 class="anchored" data-anchor-id="your-first-email-script">Your First Email Script</h2>
<p>Here’s a simple script to send an email using Python and Gmail:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> smtplib</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> email.message <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> EmailMessage</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set up your email details</span></span>
<span id="cb1-5">EMAIL_ADDRESS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_email@gmail.com'</span></span>
<span id="cb1-6">EMAIL_PASSWORD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_app_password'</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">msg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EmailMessage()</span>
<span id="cb1-9">msg[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Subject'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Hello from Python!'</span></span>
<span id="cb1-10">msg[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'From'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EMAIL_ADDRESS</span>
<span id="cb1-11">msg[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'To'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'recipient@example.com'</span></span>
<span id="cb1-12">msg.set_content(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'This is a test email sent from Python!'</span>)</span>
<span id="cb1-13"></span>
<span id="cb1-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Connect to Gmail SMTP server and send email</span></span>
<span id="cb1-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> smtplib.SMTP(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'smtp.gmail.com'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">587</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> smtp:</span>
<span id="cb1-16">    smtp.starttls()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Secure the connection</span></span>
<span id="cb1-17">    smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)</span>
<span id="cb1-18">    smtp.send_message(msg)</span>
<span id="cb1-19"></span>
<span id="cb1-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Email sent successfully!"</span>)</span></code></pre></div>
<blockquote class="blockquote">
<p><strong>Tip:</strong> Never use your regular Gmail password in scripts. Always use an App Password for security.</p>
</blockquote>
</section>
<section id="sending-html-emails-and-attachments" class="level2">
<h2 class="anchored" data-anchor-id="sending-html-emails-and-attachments">Sending HTML Emails and Attachments</h2>
<p>Want to send a fancy HTML email or attach a file? Here’s how:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> smtplib</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> email.message <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> EmailMessage</span>
<span id="cb2-3"></span>
<span id="cb2-4">EMAIL_ADDRESS <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_email@gmail.com'</span></span>
<span id="cb2-5">EMAIL_PASSWORD <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_app_password'</span></span>
<span id="cb2-6"></span>
<span id="cb2-7">msg <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EmailMessage()</span>
<span id="cb2-8">msg[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Subject'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'HTML Email with Attachment'</span></span>
<span id="cb2-9">msg[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'From'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> EMAIL_ADDRESS</span>
<span id="cb2-10">msg[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'To'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'recipient@example.com'</span></span>
<span id="cb2-11">msg.set_content(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'This is a plain text fallback.'</span>)</span>
<span id="cb2-12"></span>
<span id="cb2-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add HTML content</span></span>
<span id="cb2-14">msg.add_alternative(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb2-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;html&gt;</span></span>
<span id="cb2-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;body&gt;</span></span>
<span id="cb2-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;h1 style="color:blue;"&gt;Hello from Python!&lt;/h1&gt;</span></span>
<span id="cb2-18"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;p&gt;This is an &lt;b&gt;HTML email&lt;/b&gt; with an attachment.&lt;/p&gt;</span></span>
<span id="cb2-19"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  &lt;/body&gt;</span></span>
<span id="cb2-20"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/html&gt;</span></span>
<span id="cb2-21"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span>, subtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'html'</span>)</span>
<span id="cb2-22"></span>
<span id="cb2-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Attach a file</span></span>
<span id="cb2-24"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'example.pdf'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rb'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb2-25">    file_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f.read()</span>
<span id="cb2-26">    file_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> f.name</span>
<span id="cb2-27">msg.add_attachment(file_data, maintype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'application'</span>, subtype<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'pdf'</span>, filename<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>file_name)</span>
<span id="cb2-28"></span>
<span id="cb2-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> smtplib.SMTP(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'smtp.gmail.com'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">587</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> smtp:</span>
<span id="cb2-30">    smtp.starttls()</span>
<span id="cb2-31">    smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)</span>
<span id="cb2-32">    smtp.send_message(msg)</span>
<span id="cb2-33"></span>
<span id="cb2-34"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"HTML email with attachment sent!"</span>)</span></code></pre></div>
</section>
<section id="validating-email-addresses" class="level2">
<h2 class="anchored" data-anchor-id="validating-email-addresses">Validating Email Addresses</h2>
<p>Before sending, it’s smart to check if the email address looks valid:</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> re</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> is_valid_email(email):</span>
<span id="cb3-4">    pattern <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">^</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\w</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">-]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">@</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">[</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\w</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\.</span><span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">-]</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\.</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\w</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">$</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb3-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> re.match(pattern, email) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(is_valid_email(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'test@example.com'</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># True</span></span>
<span id="cb3-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(is_valid_email(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'not-an-email'</span>))      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># False</span></span></code></pre></div>
</section>
</section>
<section id="sending-sms-with-python-twilio" class="level1">
<h1>Sending SMS with Python (Twilio)</h1>
<section id="getting-started-with-twilio" class="level2">
<h2 class="anchored" data-anchor-id="getting-started-with-twilio">Getting Started with Twilio</h2>
<ol type="1">
<li><strong>Sign up for a free Twilio account</strong> at <a href="https://www.twilio.com/">twilio.com</a>.</li>
<li><strong>Get your Account SID and Auth Token</strong> from the Twilio Console.</li>
<li><strong>Buy a Twilio phone number</strong> (free credits available for trial accounts).</li>
</ol>
</section>
<section id="twilio-python-integration" class="level2">
<h2 class="anchored" data-anchor-id="twilio-python-integration">Twilio Python Integration</h2>
<p>Install the Twilio library:</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install twilio</span></code></pre></div>
</section>
<section id="sms-example-code" class="level2">
<h2 class="anchored" data-anchor-id="sms-example-code">SMS Example Code</h2>
<p>Here’s how to send a text message with Python and Twilio:</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> twilio.rest <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Client</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your Twilio credentials</span></span>
<span id="cb5-4">account_sid <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_account_sid'</span></span>
<span id="cb5-5">auth_token <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your_auth_token'</span></span>
<span id="cb5-6">client <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Client(account_sid, auth_token)</span>
<span id="cb5-7"></span>
<span id="cb5-8">message <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> client.messages.create(</span>
<span id="cb5-9">    body<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Hello from Python SMS!'</span>,</span>
<span id="cb5-10">    from_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'+1234567890'</span>,  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your Twilio number</span></span>
<span id="cb5-11">    to<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'+0987654321'</span>      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Recipient's phone number</span></span>
<span id="cb5-12">)</span>
<span id="cb5-13"></span>
<span id="cb5-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SMS sent! Message SID:"</span>, message.sid)</span></code></pre></div>
</section>
<section id="automating-text-messages" class="level2">
<h2 class="anchored" data-anchor-id="automating-text-messages">Automating Text Messages</h2>
<p>You can use Python to send SMS alerts automatically—for example, when a script finishes running or an error occurs. Just wrap the SMS code in a function and call it when needed!</p>
</section>
</section>
<section id="error-handling-troubleshooting" class="level1">
<h1>Error Handling &amp; Troubleshooting</h1>
<ul>
<li><strong>SMTP Connection Refused:</strong> Double-check your SMTP server, port, and internet connection. Make sure your firewall isn’t blocking port 587.</li>
<li><strong>Authentication Errors:</strong> Use App Passwords for Gmail. For Twilio, make sure your Account SID and Auth Token are correct.</li>
<li><strong>SMS Not Sending:</strong> Check if your Twilio trial number is verified and you have enough credits.</li>
</ul>
</section>
<section id="security-best-practices" class="level1">
<h1>Security Best Practices</h1>
<ul>
<li><strong>Never hard-code passwords or API keys</strong> in your scripts. Use environment variables or a <code>.env</code> file.</li>
<li><strong>Use App Passwords</strong> for Gmail, not your main password.</li>
<li><strong>Keep your credentials private</strong>—never share them or upload them to public repositories.</li>
</ul>
</section>
<section id="practical-examples-use-cases" class="level1">
<h1>Practical Examples &amp; Use Cases</h1>
<ul>
<li><strong>Automated Email Alerts:</strong> Get notified when a script finishes or an error occurs.</li>
<li><strong>Order Confirmations:</strong> Send customers a confirmation email or SMS after a purchase.</li>
<li><strong>Contact Forms:</strong> Automatically email or text yourself when someone fills out a form on your website.</li>
</ul>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p>Try sending an email and an SMS using the code above. As a challenge, modify the email script to send a message to multiple recipients at once.</p>
<details>
<summary>
Click here for Solution!
</summary>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sending to multiple recipients</span></span>
<span id="cb6-2">msg[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'To'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">', '</span>.join([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'person1@example.com'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'person2@example.com'</span>])</span></code></pre></div>
</details>
</section>
<section id="quick-takeaways" class="level1">
<h1>Quick Takeaways</h1>
<ul>
<li>Python makes sending emails and SMS easy with <code>smtplib</code> and Twilio.</li>
<li>Always use secure methods for storing credentials.</li>
<li>Validate email addresses before sending.</li>
<li>Automate notifications to save time and reduce manual work.</li>
<li>Troubleshoot common errors by checking settings and credentials.</li>
</ul>
</section>
<section id="conclusion-next-steps" class="level1">
<h1>Conclusion &amp; Next Steps</h1>
<p>You’ve just learned how to send emails and text messages with Python! With these skills, you can automate notifications, build smarter apps, and save tons of time. Keep experimenting, try new features, and don’t hesitate to ask for help or share your progress.</p>
<p><strong>Ready to level up?</strong> Try building a simple notification system for your next project, or explore more advanced email and SMS features!</p>
</section>
<section id="faqs" class="level1">
<h1>FAQs</h1>
<p><strong>1. Do I need to pay to send emails with Python?</strong><br>
No! Sending emails with Python’s <code>smtplib</code> is free. SMS via Twilio is free with trial credits, but paid after that.</p>
<p><strong>2. Is it safe to use my Gmail password in Python scripts?</strong><br>
Never use your regular password. Always use an App Password for scripts.</p>
<p><strong>3. Why do I get ‘connection refused’ errors?</strong><br>
Check your SMTP server, port, and firewall settings. For Gmail, use <code>smtp.gmail.com:587</code> with TLS.</p>
<p><strong>4. Can I send SMS without Twilio?</strong><br>
Yes! Alternatives include TextBelt, MessageBird, and Nexmo.</p>
<p><strong>5. How many emails can I send per day with Python?</strong><br>
Gmail allows 500/day for regular accounts. Other providers have different limits.</p>
</section>
<section id="engage" class="level1">
<h1>Engage!</h1>
<p><strong>Did this guide help you?</strong><br>
Share your experience, ask questions, or suggest improvements in the comments below! If you found this useful, share it with friends or on social media to help more beginners discover Python automation.</p>
<p><strong>You’re now ready to automate your communications with Python. Happy coding!</strong></p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ul>
<li><a href="https://realpython.com/python-send-email/">How to Send Emails Using Python and Gmail SMTP Server</a></li>
<li><a href="https://support.google.com/mail/answer/7126229?hl=en">Gmail SMTP Server Settings for Sending Emails</a></li>
<li><h2 id="python-smtplib-smtp-protocol-client" class="anchored" data-anchor-id="references"><a href="https://docs.python.org/3/library/smtplib.html">Python smtplib — SMTP protocol client</a></h2></li>
</ul>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-11-06/todays_post.jpg" class="img-fluid figure-img"></p>
<figcaption>Email and Text Messages with Python</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<pre><code>@online{
    sending_email_and_text_messages_with_python_a_beginner_s_complete_guide_20251106,
    author = {Sanderson II MPH, Steven P.},
    title = {Sending Email and Text Messages with Python: A Beginner’s Complete Guide},
    date = {2025-11-06}, url = {https://www.spsanderson.com/steveondata/posts/2025-11-06/},
    langid = {en} 
    }</code></pre>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-11-06/</guid>
  <pubDate>Thu, 06 Nov 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Create a Nested For Loop in R with Examples</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-11-03/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong><br>
Mastering nested for loops in R unlocks powerful data manipulation and analysis capabilities. This guide covers syntax, practical examples, optimization tips, and common pitfalls—empowering you to write efficient, readable R code.</p>
</blockquote>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Nested for loops are a fundamental tool in R programming, especially when working with multi-dimensional data like matrices or performing repetitive tasks across rows and columns. While R offers many ways to iterate, understanding how to use nested for loops effectively is essential for every R programmer. In this article, you’ll learn the syntax, see real-world examples, discover best practices, and avoid common mistakes when using nested for loops in R.</p>
</section>
<section id="what-is-a-nested-for-loop" class="level1">
<h1>What is a Nested For Loop?</h1>
<p>A <strong>nested for loop</strong> is simply a for loop inside another for loop. This structure allows you to iterate over two (or more) dimensions—think of looping through every cell in a table, or comparing every element in one vector to every element in another. Nested loops are especially useful for matrix operations, pairwise comparisons, and complex data transformations .</p>
</section>
<section id="understanding-the-basic-syntax" class="level1">
<h1>Understanding the Basic Syntax</h1>
<p>The general syntax for a nested for loop in R is:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> seq1) {</span>
<span id="cb1-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> seq2) {</span>
<span id="cb1-3">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Code to execute</span></span>
<span id="cb1-4">  }</span>
<span id="cb1-5">}</span></code></pre></div>
<ul>
<li><code>i</code> and <code>j</code> are loop variables.</li>
<li><code>seq1</code> and <code>seq2</code> are sequences (like <code>1:5</code>, <code>letters</code>, or any vector).</li>
</ul>
<section id="how-loop-variables-work" class="level2">
<h2 class="anchored" data-anchor-id="how-loop-variables-work">How Loop Variables Work</h2>
<p>Each time the outer loop runs, the inner loop completes all its iterations. The loop variables (<code>i</code>, <code>j</code>) are updated at each step, and after the loop ends, they hold their last assigned values .</p>
</section>
<section id="execution-flow-and-order" class="level2">
<h2 class="anchored" data-anchor-id="execution-flow-and-order">Execution Flow and Order</h2>
<p>The inner loop runs to completion for every single iteration of the outer loop. This means if the outer loop runs 3 times and the inner loop runs 4 times, the inner code block executes 12 times in total.</p>
</section>
</section>
<section id="step-by-step-examples" class="level1">
<h1>Step-by-Step Examples</h1>
<section id="simple-nested-for-loop-example" class="level2">
<h2 class="anchored" data-anchor-id="simple-nested-for-loop-example">Simple Nested For Loop Example</h2>
<p>Let’s print all pairs of indices in a 3x2 grid:</p>
<div class="cell">
<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="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">3</span>) {</span>
<span id="cb2-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">2</span>) {</span>
<span id="cb2-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Row:"</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Col:"</span>, j))</span>
<span id="cb2-4">  }</span>
<span id="cb2-5">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Row: 1 Col: 1"
[1] "Row: 1 Col: 2"
[1] "Row: 2 Col: 1"
[1] "Row: 2 Col: 2"
[1] "Row: 3 Col: 1"
[1] "Row: 3 Col: 2"</code></pre>
</div>
</div>
</section>
<section id="r-nested-for-loop-with-matrix-operations" class="level2">
<h2 class="anchored" data-anchor-id="r-nested-for-loop-with-matrix-operations">R Nested For Loop with Matrix Operations</h2>
<p>Suppose you want to fill a matrix with the product of its row and column indices:</p>
<div class="cell">
<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">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;">3</span></span>
<span id="cb4-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;">4</span></span>
<span id="cb4-3">result <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;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, n, m)</span>
<span id="cb4-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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>n) {</span>
<span id="cb4-5">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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>m) {</span>
<span id="cb4-6">    result[i, j] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> j</span>
<span id="cb4-7">  }</span>
<span id="cb4-8">}</span>
<span id="cb4-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(result)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    2    4    6    8
[3,]    3    6    9   12</code></pre>
</div>
</div>
</section>
<section id="r-loop-through-dataframe-rows-and-columns" class="level2">
<h2 class="anchored" data-anchor-id="r-loop-through-dataframe-rows-and-columns">R Loop Through DataFrame Rows and Columns</h2>
<p>You can also use nested loops to process data frames:</p>
<div class="cell">
<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">df <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;">A =</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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</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>
<span id="cb6-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(df)) {</span>
<span id="cb6-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ncol</span>(df)) {</span>
<span id="cb6-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<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;">"Row"</span>, i, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Col"</span>, j, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Value:"</span>, df[i, j]))</span>
<span id="cb6-5">  }</span>
<span id="cb6-6">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Row 1 Col 1 Value: 1"
[1] "Row 1 Col 2 Value: 4"
[1] "Row 2 Col 1 Value: 2"
[1] "Row 2 Col 2 Value: 5"
[1] "Row 3 Col 1 Value: 3"
[1] "Row 3 Col 2 Value: 6"</code></pre>
</div>
</div>
</section>
</section>
<section id="real-world-use-cases" class="level1">
<h1>Real-World Use Cases</h1>
<section id="matrix-manipulation-and-transformations" class="level2">
<h2 class="anchored" data-anchor-id="matrix-manipulation-and-transformations">Matrix Manipulation and Transformations</h2>
<p>Nested for loops are perfect for filling, transforming, or analyzing matrices—such as normalizing values or applying custom functions to each cell.</p>
</section>
<section id="pairwise-comparisons-and-combinations" class="level2">
<h2 class="anchored" data-anchor-id="pairwise-comparisons-and-combinations">Pairwise Comparisons and Combinations</h2>
<p>Need to compare every element in one vector to every element in another? Nested loops let you compute all possible pairs, useful in statistics and simulations.</p>
</section>
<section id="data-analysis-and-statistical-computations" class="level2">
<h2 class="anchored" data-anchor-id="data-analysis-and-statistical-computations">Data Analysis and Statistical Computations</h2>
<p>From calculating summary statistics across groups to running simulations over parameter grids, nested loops are a staple in data analysis workflows.</p>
</section>
</section>
<section id="control-flow-in-nested-loops" class="level1">
<h1>Control Flow in Nested Loops</h1>
<section id="using-break-and-next-statements-in-loops" class="level2">
<h2 class="anchored" data-anchor-id="using-break-and-next-statements-in-loops">Using Break and Next Statements in Loops</h2>
<ul>
<li><code>break</code> exits the innermost loop immediately.</li>
<li><code>next</code> skips the current iteration and continues with the next.</li>
</ul>
<p><strong>Example:</strong></p>
<div class="cell">
<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="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">3</span>) {</span>
<span id="cb8-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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;">3</span>) {</span>
<span id="cb8-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> j) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">next</span></span>
<span id="cb8-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(i, j))</span>
<span id="cb8-5">  }</span>
<span id="cb8-6">}</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1 2"
[1] "1 3"
[1] "2 1"
[1] "2 3"
[1] "3 1"
[1] "3 2"</code></pre>
</div>
</div>
<p>This skips cases where <code>i</code> equals <code>j</code>.</p>
</section>
<section id="conditional-logic-within-nested-loops" class="level2">
<h2 class="anchored" data-anchor-id="conditional-logic-within-nested-loops">Conditional Logic Within Nested Loops</h2>
<p>You can add <code>if</code> statements to control what happens inside your loops, making your code dynamic and responsive to data.</p>
</section>
</section>
<section id="performance-considerations-and-optimization" class="level1">
<h1>Performance Considerations and Optimization</h1>
<section id="r-loop-optimization-techniques" class="level2">
<h2 class="anchored" data-anchor-id="r-loop-optimization-techniques">R Loop Optimization Techniques</h2>
<ul>
<li><strong>Preallocate storage:</strong> Always create your output object (like a matrix or list) before the loop starts.</li>
<li><strong>Prefer vectorization:</strong> Use functions like <code>apply</code>, <code>lapply</code>, or <code>mapply</code> when possible—they’re faster and more “R-like.”</li>
<li><strong>Limit nesting:</strong> Deeply nested loops can be hard to read and slow to run.</li>
</ul>
</section>
<section id="alternatives-to-nested-loops-in-r-apply-vs-for-loop" class="level2">
<h2 class="anchored" data-anchor-id="alternatives-to-nested-loops-in-r-apply-vs-for-loop">Alternatives to Nested Loops in R: Apply vs For Loop</h2>
<p>Instead of nested for loops, consider:</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using apply for matrix operations</span></span>
<span id="cb10-2">result <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;">outer</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;">3</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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"*"</span>)</span></code></pre></div>
<p>Or, for parallel processing:</p>
<div class="cell">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(foreach)</span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(doParallel)</span>
<span id="cb11-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">registerDoParallel</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cores =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb11-4">result <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;">foreach</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">i =</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;">3</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combine =</span> rbind) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%:%</span></span>
<span id="cb11-5">            <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">foreach</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">j =</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;">4</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combine =</span> c) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%dopar%</span> {</span>
<span id="cb11-6">              i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> j</span>
<span id="cb11-7">            }</span>
<span id="cb11-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(result)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>         [,1] [,2] [,3] [,4]
result.1    1    2    3    4
result.2    2    4    6    8
result.3    3    6    9   12</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopImplicitCluster</span>()</span></code></pre></div>
</div>
<p>These approaches can be much faster for large datasets .</p>
</section>
</section>
<section id="common-mistakes-and-debugging-tips" class="level1">
<h1>Common Mistakes and Debugging Tips</h1>
<ul>
<li><strong>Growing objects inside loops:</strong> Avoid using <code>c()</code> or <code>rbind()</code> repeatedly inside loops; preallocate instead.</li>
<li><strong>Index out of bounds:</strong> Always check your loop ranges to avoid errors.</li>
<li><strong>Misusing break/next:</strong> Remember, they only affect the innermost loop.</li>
</ul>
<section id="your-turn-interactive-exercise" class="level2">
<h2 class="anchored" data-anchor-id="your-turn-interactive-exercise">Your Turn! Interactive Exercise</h2>
<p><strong>Task:</strong><br>
Write a nested for loop in R that creates a 5x5 matrix where each cell contains the sum of its row and column indices.</p>
<details>
<summary>
Click here for Solution!
</summary>
<div class="cell">
<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">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;">5</span></span>
<span id="cb14-2">result <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;">matrix</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, n, n)</span>
<span id="cb14-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (i <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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>n) {</span>
<span id="cb14-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (j <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</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>n) {</span>
<span id="cb14-5">    result[i, j] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> i <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> j</span>
<span id="cb14-6">  }</span>
<span id="cb14-7">}</span>
<span id="cb14-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(result)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     [,1] [,2] [,3] [,4] [,5]
[1,]    2    3    4    5    6
[2,]    3    4    5    6    7
[3,]    4    5    6    7    8
[4,]    5    6    7    8    9
[5,]    6    7    8    9   10</code></pre>
</div>
</div>
</details>
</section>
</section>
<section id="frequently-asked-questions" class="level1">
<h1>Frequently Asked Questions</h1>
<p><strong>Q1: Can I nest more than two for loops in R?</strong><br>
Yes, you can nest as many for loops as needed, but readability and performance may suffer with deep nesting .</p>
<p><strong>Q2: Are nested for loops slow in R?</strong><br>
They can be, especially for large datasets. Prefer vectorized functions or parallel processing when possible .</p>
<p><strong>Q3: What’s the difference between break and next?</strong><br>
<code>break</code> exits the innermost loop, while <code>next</code> skips to the next iteration of the innermost loop .</p>
<p><strong>Q4: How do I avoid growing objects inside loops?</strong><br>
Preallocate your output (e.g., use <code>matrix(0, n, m)</code>) before the loop starts.</p>
<p><strong>Q5: When should I use apply instead of nested for loops?</strong><br>
Use <code>apply</code> or similar functions for operations that can be vectorized, especially on matrices or data frames, for better performance .</p>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<ul>
<li><strong>Nested for loops</strong> are essential for multi-dimensional iteration in R.</li>
<li>Always <strong>preallocate</strong> output objects for efficiency.</li>
<li>Use <strong>break</strong> and <strong>next</strong> wisely for control flow.</li>
<li>Prefer <strong>vectorized</strong> or <strong>apply</strong> functions when possible.</li>
<li>Avoid common pitfalls like growing objects inside loops.</li>
</ul>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Nested for loops are a powerful tool in R programming, enabling you to tackle complex data manipulation and analysis tasks. By mastering their syntax, understanding best practices, and knowing when to use alternatives, you’ll write cleaner, faster, and more reliable R code. Ready to level up your R skills? Start experimenting with nested for loops in your own projects today!</p>
<blockquote class="blockquote">
<p><strong>Enjoyed this guide?</strong><br>
Share your feedback below and connect with fellow R programmers on social media. Happy coding!</p>
</blockquote>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><a href="https://cran.r-project.org/doc/manuals/r-release/R-intro.html">An Introduction to R (CRAN Manual)</a></li>
<li><a href="https://bookdown.org/content/d1e53ac9-28ce-472f-bc2c-f499f18264a3/loops.html">Best Coding Practices for R – Chapter 14: For Loops</a></li>
<li><a href="https://www.geeksforgeeks.org/r-language/how-to-create-a-nested-for-loop-in-r/">How to Create a Nested For Loop in R? - GeeksforGeeks</a></li>
<li><a href="https://cran.r-project.org/web/packages/foreach/vignettes/nested.html">Nesting foreach loops (CRAN Vignette)</a></li>
</ol>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-11-03/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Nested Loops in R</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<pre><code>@online{
  \_20251103, author = {Sanderson II MPH, Steven P.},
  title = {How to Create a Nested For Loop in R (Including Examples)},
  date = {2025-11-03},
  url = {https://www.spsanderson.com/steveondata/posts/2025-11-03/},
  langid = {en}
  }</code></pre>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <category>loops</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-11-03/</guid>
  <pubDate>Mon, 03 Nov 2025 05:00:00 GMT</pubDate>
</item>
<item>
  <title>RAG with Ollama and ragnar in R: A Practical Guide for R Programmers</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-10-29/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong><br>
Learn how to build a fully local, privacy-preserving Retrieval-Augmented Generation (RAG) pipeline in R using the <code>ragnar</code> and <code>ellmer</code> packages with Ollama. This workflow enables automated, accurate document summarization—ideal for healthcare, compliance, and enterprise use cases.</p>
</blockquote>
<section id="introduction-why-rag-ollama-and-ragnar-in-r" class="level1">
<h1>Introduction: Why RAG, Ollama, and ragnar in R?</h1>
<p>I want to show you how to leverage the power of Retrieval-Augmented Generation (RAG) in R using the Ollama and ragnar packages. This came about by the need for a more efficient way to process and summarize large volumes of text data, particularly in the healthcare domain that I ran into.</p>
<p>Retrieval-Augmented Generation (RAG) is transforming how we interact with documents by combining the strengths of Large Language Models (LLMs) with precise, document-grounded retrieval. With the rise of local LLM runners like <strong>Ollama</strong> and R packages such as <strong>ragnar</strong> and <strong>ellmer</strong>, R programmers can now build powerful, private, and production-ready document summarization pipelines—entirely within R.</p>
<p>This post walks you through a real-world RAG workflow for summarizing health insurance policy PDFs, explaining each code step in simple terms. Whether you’re new to LLMs or looking to automate document Q&amp;A in R, this guide is for you.</p>
</section>
<section id="prerequisites-setup" class="level1">
<h1>Prerequisites &amp; Setup</h1>
<p>Before diving in, ensure you have the following:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Component</th>
<th>Installation Command / Link</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Ollama</strong></td>
<td><a href="https://ollama.com/">Download Ollama</a> or use Docker</td>
<td>Local LLM and embedding model hosting</td>
</tr>
<tr class="even">
<td><strong>R Packages</strong></td>
<td><code>install.packages(c("ragnar", "ellmer", "tidyverse", "fs", "glue"))</code></td>
<td>Core RAG and data manipulation</td>
</tr>
<tr class="odd">
<td><strong>Ollama Models</strong></td>
<td><code>ollama pull nomic-embed-text:latest</code><br><code>ollama pull llama3.1</code></td>
<td>Embedding and chat models</td>
</tr>
<tr class="even">
<td><strong>Email Tools</strong></td>
<td><code>install.packages(c("blastula", "RDCOMClient"))</code> (optional)</td>
<td>Email automation (Windows/Outlook)</td>
</tr>
</tbody>
</table>
<blockquote class="blockquote">
<p><strong>Tip:</strong><br>
Make sure the Ollama server is running and the required models are pulled before starting your R workflow.</p>
</blockquote>
</section>
<section id="understanding-the-rag-workflow-in-r" class="level1">
<h1>Understanding the RAG Workflow in R</h1>
<p>Here’s a high-level overview of the RAG pipeline you’ll build:</p>
<ol type="1">
<li><strong>Load and list PDF files</strong></li>
<li><strong>Convert documents to markdown and chunk them</strong></li>
<li><strong>Generate embeddings using Ollama</strong></li>
<li><strong>Store and index chunks in a DuckDB vector database</strong></li>
<li><strong>Register retrieval as a tool for the LLM</strong></li>
<li><strong>Query the LLM for summaries, grounded in your documents</strong></li>
<li><strong>Format and send results via email or markdown</strong></li>
</ol>
<section id="library-overview" class="level2">
<h2 class="anchored" data-anchor-id="library-overview">Library Overview</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 78%">
</colgroup>
<thead>
<tr class="header">
<th>Library</th>
<th>Purpose</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>ragnar</code></td>
<td>RAG: document storage, chunking, embedding, retrieval</td>
</tr>
<tr class="even">
<td><code>ellmer</code></td>
<td>LLM chat interface (Ollama, OpenAI, etc.)</td>
</tr>
<tr class="odd">
<td><code>fs</code></td>
<td>File system operations</td>
</tr>
<tr class="even">
<td><code>tidyverse</code></td>
<td>Data manipulation, pipes, mapping</td>
</tr>
<tr class="odd">
<td><code>glue</code></td>
<td>String interpolation for prompts and output</td>
</tr>
<tr class="even">
<td><code>blastula</code></td>
<td>Email composition (alternative to RDCOMClient)</td>
</tr>
<tr class="odd">
<td><code>RDCOMClient</code></td>
<td>Windows Outlook automation for email</td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="step-by-step-code-walkthrough" class="level1">
<h1>Step-by-Step Code Walkthrough</h1>
<p>Let’s break down the workflow, explaining each step and key syntax.</p>
<section id="load-libraries" class="level2">
<h2 class="anchored" data-anchor-id="load-libraries">1. Load Libraries</h2>
<div class="sourceCode" 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>(ragnar)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ellmer)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(fs)</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glue)</span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(blastula)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RDCOMClient)</span></code></pre></div>
<p><em>Loads all required packages for RAG, LLM chat, file handling, and email.</em></p>
</section>
<section id="discover-and-list-policy-files" class="level2">
<h2 class="anchored" data-anchor-id="discover-and-list-policy-files">2. Discover and List Policy Files</h2>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">policy_files_path <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;">"W:/Path/To/PDFs/"</span></span>
<span id="cb2-2">policy_files <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;">list.files</span>(policy_files_path, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<p><em>Finds all PDF files in your specified directory.</em></p>
</section>
<section id="define-the-system-prompt" class="level2">
<h2 class="anchored" data-anchor-id="define-the-system-prompt">3. Define the System Prompt</h2>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">TOPIC<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> RAG with Ollama and ragnar <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> R</span>
<span id="cb3-2">KEYWORD<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> RAG, Ollama, LLM</span>
<span id="cb3-3">AUDIENCE<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span> R Programmers</span>
<span id="cb3-4">WORDCOUNT<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;">750</span> to <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span></span>
<span id="cb3-5"></span>
<span id="cb3-6">Use the following R code as the basis <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> the blog post. Explain syntax <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> simple terms and use tables and bullet points where appropriate.</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span>code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb3-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load Libraries ----</span></span>
<span id="cb3-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ragnar)</span>
<span id="cb3-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ellmer)</span>
<span id="cb3-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(fs)</span>
<span id="cb3-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb3-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(glue)</span>
<span id="cb3-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(blastula)</span>
<span id="cb3-16"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RDCOMClient)</span>
<span id="cb3-17"></span>
<span id="cb3-18"></span>
<span id="cb3-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Files ----</span></span>
<span id="cb3-20">policy_files_path <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;">"W:/Path/To/PDFs/"</span></span>
<span id="cb3-21">policy_files <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;">list.files</span>(policy_files_path, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">full.names =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb3-22"></span>
<span id="cb3-23"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># System Prompt ----</span></span>
<span id="cb3-24">system_prompt <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;">str_squish</span>(</span>
<span id="cb3-25">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb3-26"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  You are an expert assistant that summarizes **Health Insurance Payer Policies** clearly and accurately for healthcare, billing, and administrative users.</span></span>
<span id="cb3-27"></span>
<span id="cb3-28"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  When responding, you should first quote relevant material from the documents in the store,</span></span>
<span id="cb3-29"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  provide links to the sources, and then add your own context and interpretation. Try to be as concise</span></span>
<span id="cb3-30"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  as you are thorough.</span></span>
<span id="cb3-31"></span>
<span id="cb3-32"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  For every document passed to you the output should if applicable include:</span></span>
<span id="cb3-33"></span>
<span id="cb3-34"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  1. Policy Summary: 1–2 paragraphs describing purpose, scope, and coverage intent.</span></span>
<span id="cb3-35"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  2. Key Points: At least 3 concise bullet points summarizing coverage criteria, limitations, exclusions, or authorization requirements.</span></span>
<span id="cb3-36"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  3. Policy Information Table</span></span>
<span id="cb3-37"></span>
<span id="cb3-38"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  **Model Behavior Rules:**</span></span>
<span id="cb3-39"></span>
<span id="cb3-40"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  * If information is missing, state “Not specified in document.”</span></span>
<span id="cb3-41"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  * Do not infer or assume; summarize only verifiable content.</span></span>
<span id="cb3-42"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  * Maintain neutral, factual tone using payer-standard language (e.g., “medically necessary,” “experimental/investigational”).</span></span>
<span id="cb3-43"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  * Simplify complex clinical text while preserving accuracy.</span></span>
<span id="cb3-44"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  * Always follow the structure: **Policy Summary → Key Points → Policy Information Table.**</span></span>
<span id="cb3-45"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  * Avoid opinion, speculation, or advice; ensure compliance-focused clarity.</span></span>
<span id="cb3-46"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">  "</span></span>
<span id="cb3-47">)</span></code></pre></div>
<p><em>Sets the LLM’s behavior, ensuring consistent, compliance-focused summaries.</em></p>
</section>
<section id="prepare-file-metadata" class="level2">
<h2 class="anchored" data-anchor-id="prepare-file-metadata">4. Prepare File Metadata</h2>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">file_split_tbl <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="cb4-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file_path =</span> policy_files</span>
<span id="cb4-3">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb4-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">path_file</span>(file_path),</span>
<span id="cb4-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file_extension =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">path_ext</span>(file_path),</span>
<span id="cb4-7">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file_size =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file_size</span>(file_path),</span>
<span id="cb4-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">file_date =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file_info</span>(file_path)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>modification_time</span>
<span id="cb4-9">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_split</span>(file_name)</span></code></pre></div>
<p><em>Creates a tidy table with file metadata for processing.</em></p>
</section>
<section id="rag-processing-loop" class="level2">
<h2 class="anchored" data-anchor-id="rag-processing-loop">5. RAG Processing Loop</h2>
<p>This is the heart of the workflow rocessing each file through the RAG pipeline.</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1">llm_resp_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> file_split_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb5-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">imap</span>(</span>
<span id="cb5-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.f =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(obj, id) {</span>
<span id="cb5-4">      file_path <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> obj <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;">pull</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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pluck</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb5-5">      store_location <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;">"pdf_ragnar_duckdb"</span></span>
<span id="cb5-6">      store <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;">ragnar_store_create</span>(</span>
<span id="cb5-7">        store_location,</span>
<span id="cb5-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">embed =</span> \(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">embed_ollama</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"nomic-embed-text:latest"</span>),</span>
<span id="cb5-9">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">overwrite =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span></span>
<span id="cb5-10">      )</span>
<span id="cb5-11">      chunks <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> file_path <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;">read_as_markdown</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;">markdown_chunk</span>()</span>
<span id="cb5-12">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ragnar_store_insert</span>(store, chunks)</span>
<span id="cb5-13">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ragnar_store_build_index</span>(store)</span>
<span id="cb5-14">      client <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;">chat_ollama</span>(</span>
<span id="cb5-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">model =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"gpt-oss:20b-cloud"</span>,</span>
<span id="cb5-16">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">system_prompt =</span> system_prompt,</span>
<span id="cb5-17">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">params =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">temperature =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>)</span>
<span id="cb5-18">      )</span>
<span id="cb5-19">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ragnar_register_tool_retrieve</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">chat =</span> client, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">store =</span> store)</span>
<span id="cb5-20">      user_prompt <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;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Please summarize the policy: {file_path}"</span>)</span>
<span id="cb5-21">      res <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> client<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;">chat</span>(user_prompt, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">echo =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"all"</span>)</span>
<span id="cb5-22">      rec <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> obj <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">llm_resp =</span> res)</span>
<span id="cb5-23">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(rec)</span>
<span id="cb5-24">    }</span>
<span id="cb5-25">  )</span></code></pre></div>
<p><strong>Key Concepts Explained:</strong></p>
<ul>
<li><code>embed = \(x) embed_ollama(x, model = "nomic-embed-text:latest")</code>:<br>
<em>Anonymous function (R 4.1+), tells ragnar to use Ollama for embeddings.</em></li>
<li><code>read_as_markdown()</code> and <code>markdown_chunk()</code>:<br>
<em>Convert documents to markdown and split into manageable chunks for retrieval.</em></li>
<li><code>ragnar_store_create()</code>, <code>ragnar_store_insert()</code>, <code>ragnar_store_build_index()</code>:<br>
<em>Set up and populate the vector database for semantic search.</em></li>
<li><code>chat_ollama()</code>:<br>
<em>Creates a chat client using a local LLM model via Ollama.</em></li>
<li><code>ragnar_register_tool_retrieve()</code>:<br>
<em>Connects the retrieval tool to the LLM, enabling RAG.</em></li>
<li><code>client$chat()</code>:<br>
<em>Sends the user prompt and gets a grounded summary.</em></li>
</ul>
</section>
<section id="format-output-for-email" class="level2">
<h2 class="anchored" data-anchor-id="format-output-for-email">6. Format Output for Email</h2>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">output_tbl <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;">list_rbind</span>(llm_resp_list) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb6-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb6-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">email_body =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">md</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb6-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      Please see summary for below:</span></span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      Name: {file_name}</span></span>
<span id="cb6-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span></span>
<span id="cb6-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      Extension: {file_extension}</span></span>
<span id="cb6-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span></span>
<span id="cb6-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      Size: {file_size} bytes</span></span>
<span id="cb6-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span></span>
<span id="cb6-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      Date: {file_date}</span></span>
<span id="cb6-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span></span>
<span id="cb6-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      Summary Response: </span></span>
<span id="cb6-15"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      </span></span>
<span id="cb6-16"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">      {llm_resp}</span></span>
<span id="cb6-17"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    "</span>))</span>
<span id="cb6-18">  )</span></code></pre></div>
<p><em>Prepares a markdown-formatted summary for each file.</em></p>
</section>
<section id="automate-email-sending-optional" class="level2">
<h2 class="anchored" data-anchor-id="automate-email-sending-optional">7. Automate Email Sending (Optional)</h2>
<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;">walk</span>(</span>
<span id="cb7-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.x =</span> output_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>email_body,</span>
<span id="cb7-3">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> {</span>
<span id="cb7-4">    Outlook <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;">COMCreate</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Outlook.Application"</span>)</span>
<span id="cb7-5">    Email <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> Outlook<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;">CreateItem</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb7-6">    Email[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"subject"</span>]] <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;">"Payer Policy Summary"</span></span>
<span id="cb7-7">    Email[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"htmlbody"</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> markdown<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;">markdownToHTML</span>(.x)</span>
<span id="cb7-8">    attachment <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;">str_replace_all</span>(</span>
<span id="cb7-9">      output_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>file_path[output_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>email_body <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .x],</span>
<span id="cb7-10">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/"</span>,</span>
<span id="cb7-11">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</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;">"</span></span>
<span id="cb7-12">    )</span>
<span id="cb7-13">    Email[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>]] <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;">"who gets the summarized files?"</span></span>
<span id="cb7-14">    Email[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"attachments"</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;">Add</span>(attachment)</span>
<span id="cb7-15">    Email<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;">Send</span>()</span>
<span id="cb7-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(Outlook)</span>
<span id="cb7-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rm</span>(Email)</span>
<span id="cb7-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.sleep</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb7-19">  }</span>
<span id="cb7-20">)</span></code></pre></div>
<p><em>Uses Windows COM automation to send emails with summaries and attachments.</em></p>
</section>
<section id="export-results-to-markdown" class="level2">
<h2 class="anchored" data-anchor-id="export-results-to-markdown">8. Export Results to Markdown</h2>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1">markdown_sections <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;">map_chr</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(output_tbl), <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(i) {</span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_to_md</span>(output_tbl[i, ])</span>
<span id="cb8-3">})</span>
<span id="cb8-4">markdown_doc <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;">paste</span>(markdown_sections, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">---</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">write_file</span>(markdown_doc, <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;">getwd</span>(), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/test_policy_output.md"</span>))</span></code></pre></div>
<p><em>Creates a markdown report with all summaries for documentation or review.</em></p>
</section>
</section>
<section id="key-functions-reference" class="level1">
<h1>Key Functions Reference</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 24%">
<col style="width: 38%">
<col style="width: 36%">
</colgroup>
<thead>
<tr class="header">
<th>Function</th>
<th>Purpose</th>
<th>Key Parameters / Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>ragnar_store_create()</code></td>
<td>Create vector store with embedding function</td>
<td><code>location</code>, <code>embed</code>, <code>overwrite</code></td>
</tr>
<tr class="even">
<td><code>read_as_markdown()</code></td>
<td>Convert file to markdown</td>
<td><code>file_path</code></td>
</tr>
<tr class="odd">
<td><code>markdown_chunk()</code></td>
<td>Split markdown into chunks</td>
<td>Default chunking parameters</td>
</tr>
<tr class="even">
<td><code>ragnar_store_insert()</code></td>
<td>Insert chunks and generate embeddings</td>
<td><code>store</code>, <code>chunks</code></td>
</tr>
<tr class="odd">
<td><code>ragnar_store_build_index()</code></td>
<td>Build search indices</td>
<td><code>store</code></td>
</tr>
<tr class="even">
<td><code>chat_ollama()</code></td>
<td>Create chat client with Ollama model</td>
<td><code>model</code>, <code>system_prompt</code>, <code>params</code></td>
</tr>
<tr class="odd">
<td><code>ragnar_register_tool_retrieve()</code></td>
<td>Register retrieval tool for LLM</td>
<td><code>chat</code>, <code>store</code></td>
</tr>
</tbody>
</table>
</section>
<section id="best-practices-tips" class="level1">
<h1>Best Practices &amp; Tips</h1>
<ul>
<li><strong>Keep it Local:</strong> All processing and LLM inference happen on your machine—no sensitive data leaves your environment.</li>
<li><strong>Use Clear Prompts:</strong> A detailed system prompt ensures consistent, compliance-ready summaries.</li>
<li><strong>Chunk Wisely:</strong> Proper chunking improves retrieval accuracy and LLM grounding.</li>
<li><strong>Monitor Memory:</strong> Large PDFs and embeddings can be memory-intensive; process in batches if needed.</li>
<li><strong>Automate Output:</strong> Use email or markdown export to integrate with business workflows.</li>
</ul>
</section>
<section id="faq-rag-with-ollama-and-ragnar-in-r" class="level1">
<h1>FAQ: RAG with Ollama and ragnar in R</h1>
<p><strong>Q1: What is Retrieval-Augmented Generation (RAG)?</strong><br>
A: RAG combines LLMs with document retrieval, ensuring responses are grounded in your actual data.</p>
<p><strong>Q2: Why use Ollama with R?</strong><br>
A: Ollama lets you run LLMs and embedding models locally, keeping data private and reducing latency.</p>
<p><strong>Q3: What file types are supported?</strong><br>
A: <code>ragnar</code> supports PDF, DOCX, HTML, and more—anything convertible to markdown.</p>
<p><strong>Q4: Can I use other LLMs or embedding models?</strong><br>
A: Yes! <code>ragnar</code> and <code>ellmer</code> support multiple providers (OpenAI, Google, etc.), but Ollama is ideal for local workflows.</p>
<p><strong>Q5: How do I troubleshoot model or memory issues?</strong><br>
A: Ensure Ollama is running, models are pulled, and process large files in smaller batches if you hit memory limits.</p>
</section>
<section id="quick-takeaways" class="level1">
<h1>Quick Takeaways</h1>
<ul>
<li><strong>RAG in R</strong> is now practical and private with <code>ragnar</code>, <code>ellmer</code>, and Ollama.</li>
<li><strong>Chunking and embedding</strong> are key to accurate, document-grounded LLM responses.</li>
<li><strong>Automated summarization</strong> can be integrated into business workflows via email or markdown.</li>
<li><strong>All code is modular</strong>—customize prompts, models, and output as needed.</li>
<li><strong>Ideal for sensitive data</strong>—no cloud required.</li>
</ul>
</section>
<section id="conclusion-next-steps" class="level1">
<h1>Conclusion &amp; Next Steps</h1>
<p>Building a RAG pipeline in R with Ollama and ragnar empowers you to automate document summarization, Q&amp;A, and compliance reporting—entirely on your own infrastructure. This approach is especially valuable for healthcare, legal, and enterprise settings where privacy and accuracy are paramount.</p>
<blockquote class="blockquote">
<p><strong>Ready to try it?</strong><br>
Install the packages, set up Ollama, and adapt the code to your own document collections. Explore advanced features like hybrid retrieval, custom chunking, or integrating with Shiny dashboards for interactive Q&amp;A.</p>
</blockquote>
</section>
<section id="further-reading-resources" class="level1">
<h1>Further Reading &amp; Resources</h1>
<ul>
<li><a href="https://ragnar.tidyverse.org/">ragnar documentation</a></li>
<li><a href="https://ellmer.tidyverse.org/">ellmer documentation</a></li>
<li><a href="https://ollama.com/">Ollama official site</a></li>
</ul>
<blockquote class="blockquote">
<p><strong>Share your feedback!</strong><br>
Did you find this guide helpful? Share your experience or questions in the comments, and connect with the document summarizing!**</p>
</blockquote>
<hr>
<p>Happy Coding! 🚀</p>
<section id="rag-with-r" class="level2">
<h2 class="anchored" data-anchor-id="rag-with-r"><img src="https://www.spsanderson.com/steveondata/posts/2025-10-29/todays_post.jpg" class="img-fluid" alt="RAG with R!"></h2>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/<span class="citation" data-cites="stevensanderson">@stevensanderson</span></a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/<span class="citation" data-cites="spsanderson">@spsanderson</span></a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: <em>Extending Excel with Python and R</em> here</em>: <a href="https://packt.link/oTyZJ">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>
</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-10-29/</guid>
  <pubDate>Wed, 29 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Python Time Management: A Beginner’s Guide to Keeping Time, Scheduling Tasks, and Launching Programs</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-10-23/</link>
  <description><![CDATA[ 






<p><em>Author’s Note: As I write this series on Python programming, I’m learning alongside you every step of the way. My goal is to share what I learn and present it to you in a simple manner. While I research and test each concept, I’m constantly expanding my own understanding of these topics, this means I might make mistakes or do things in a manner that is not best practice. Your feedback, questions, and insights are not only welcome but to me, incredibly valuable.</em></p>
<hr>
<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong> Master Python’s built-in time functions, automate your tasks with smart scheduling, and seamlessly launch external programs to boost your programming productivity.</p>
</blockquote>
<p>Python offers powerful built-in tools for managing time, scheduling tasks, and launching external programs. Whether you’re building automated scripts, creating scheduled backups, or integrating with other applications, these important skills will transform how you approach programming projects.</p>
<hr>
<section id="understanding-pythons-time-management-tools" class="level1">
<h1>Understanding Python’s Time Management Tools</h1>
<p>Python provides several modules for working with time and dates. Let’s explore the most important ones for beginners:</p>
<section id="the-time-module-your-basic-timekeeping-toolkit" class="level2">
<h2 class="anchored" data-anchor-id="the-time-module-your-basic-timekeeping-toolkit">The <code>time</code> Module: Your Basic Timekeeping Toolkit</h2>
<p>The <code>time</code> module handles basic time operations like measuring duration, pausing execution, and working with timestamps.</p>
<p><strong>Key <code>time</code> Functions:</strong></p>
<table class="caption-top table">
<colgroup>
<col style="width: 29%">
<col style="width: 26%">
<col style="width: 43%">
</colgroup>
<thead>
<tr class="header">
<th>Function</th>
<th>Purpose</th>
<th>Example Usage</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>time.time()</code></td>
<td>Get current timestamp</td>
<td><code>start = time.time()</code></td>
</tr>
<tr class="even">
<td><code>time.sleep(seconds)</code></td>
<td>Pause execution</td>
<td><code>time.sleep(2)</code></td>
</tr>
<tr class="odd">
<td><code>time.ctime()</code></td>
<td>Convert timestamp to readable string</td>
<td><code>time.ctime()</code> → <code>'Tue Oct 21 03:26:00 2025'</code></td>
</tr>
</tbody>
</table>
<div id="ff5e27d5" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Measure how long code takes to run</span></span>
<span id="cb1-4">start_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here</span></span>
<span id="cb1-7">end_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb1-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Execution time: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>end_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start_time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> seconds"</span>)</span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pause execution for 3 seconds</span></span>
<span id="cb1-11">time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb1-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This prints after 3 seconds!"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Execution time: 6.842613220214844e-05 seconds
This prints after 3 seconds!</code></pre>
</div>
</div>
</section>
<section id="the-datetime-module-advanced-date-and-time-manipulation" class="level2">
<h2 class="anchored" data-anchor-id="the-datetime-module-advanced-date-and-time-manipulation">The <code>datetime</code> Module: Advanced Date and Time Manipulation</h2>
<p>For more complex date operations, the <code>datetime</code> module provides powerful tools for creating, formatting, and calculating with dates.</p>
<p><strong>Key <code>datetime</code> Classes:</strong></p>
<ul>
<li><strong><code>datetime.now()</code></strong>: Get current date and time</li>
<li><strong><code>datetime.strftime()</code></strong>: Format dates as strings<br>
</li>
<li><strong><code>datetime.strptime()</code></strong>: Parse strings into date objects</li>
<li><strong><code>timedelta</code></strong>: Represent time durations</li>
</ul>
<div id="c0428ebe" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime, timedelta</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get current date and time</span></span>
<span id="cb3-4">now <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now()</span>
<span id="cb3-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(now.strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> %H:%M:%S"</span>))</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate future dates</span></span>
<span id="cb3-8">tomorrow <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> now <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> timedelta(days<span class="op" 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="cb3-9">next_week <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> now <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> timedelta(weeks<span class="op" 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></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>2025-10-23 09:38:10</code></pre>
</div>
</div>
</section>
</section>
<section id="scheduling-tasks-with-python" class="level1">
<h1>Scheduling Tasks with Python</h1>
<section id="using-the-schedule-library-for-simple-task-automation" class="level2">
<h2 class="anchored" data-anchor-id="using-the-schedule-library-for-simple-task-automation">Using the <code>schedule</code> Library for Simple Task Automation</h2>
<p>The <code>schedule</code> library makes it incredibly easy to run Python functions at specific times or intervals.</p>
<p><strong>Installation:</strong></p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install schedule</span></code></pre></div>
<p><strong>Common Scheduling Patterns:</strong></p>
<table class="caption-top table">
<colgroup>
<col style="width: 44%">
<col style="width: 30%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Schedule Expression</th>
<th>Description</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>every(10).seconds</code></td>
<td>Every 10 seconds</td>
<td><code>schedule.every(10).seconds.do(job)</code></td>
</tr>
<tr class="even">
<td><code>every().day.at("10:30")</code></td>
<td>Daily at 10:30 AM</td>
<td><code>schedule.every().day.at("10:30").do(job)</code></td>
</tr>
<tr class="odd">
<td><code>every().monday</code></td>
<td>Every Monday</td>
<td><code>schedule.every().monday.do(job)</code></td>
</tr>
<tr class="even">
<td><code>every().hour</code></td>
<td>Every hour</td>
<td><code>schedule.every().hour.do(job)</code></td>
</tr>
</tbody>
</table>
<p><strong>Complete Scheduling Example:</strong></p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> schedule</span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> backup_files():</span>
<span id="cb6-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Running backup..."</span>)</span>
<span id="cb6-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your backup logic here</span></span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> send_report():</span>
<span id="cb6-9">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sending daily report..."</span>)</span>
<span id="cb6-10">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your reporting logic here</span></span>
<span id="cb6-11"></span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Schedule tasks</span></span>
<span id="cb6-13">schedule.every().day.at(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"02:00"</span>).do(backup_files)</span>
<span id="cb6-14">schedule.every().friday.at(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"17:00"</span>).do(send_report)</span>
<span id="cb6-15">schedule.every(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>).minutes.do(<span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"System check"</span>))</span>
<span id="cb6-16"></span>
<span id="cb6-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keep scheduler running</span></span>
<span id="cb6-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb6-19">    schedule.run_pending()</span>
<span id="cb6-20">    time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</section>
<section id="building-custom-schedulers" class="level2">
<h2 class="anchored" data-anchor-id="building-custom-schedulers">Building Custom Schedulers</h2>
<p>For simple recurring tasks, you can create basic schedulers without external libraries:</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> simple_scheduler():</span>
<span id="cb7-5">    last_run <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb7-6">    interval <span class="op" 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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run every 60 seconds</span></span>
<span id="cb7-7">    </span>
<span id="cb7-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb7-9">        current_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb7-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> current_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> last_run <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> interval:</span>
<span id="cb7-11">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Task executed at </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>datetime<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>now()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'%H:%M:%S'</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-12">            last_run <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> current_time</span>
<span id="cb7-13">        time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</section>
</section>
<section id="launching-external-programs" class="level1">
<h1>Launching External Programs</h1>
<section id="using-subprocess-for-program-execution" class="level2">
<h2 class="anchored" data-anchor-id="using-subprocess-for-program-execution">Using <code>subprocess</code> for Program Execution</h2>
<p>The <code>subprocess</code> module is the recommended way to launch external programs from Python.</p>
<p><strong>Key <code>subprocess</code> Functions:</strong></p>
<table class="caption-top table">
<colgroup>
<col style="width: 34%">
<col style="width: 31%">
<col style="width: 34%">
</colgroup>
<thead>
<tr class="header">
<th>Function</th>
<th>Purpose</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>subprocess.run()</code></td>
<td>Execute command and wait</td>
<td>Simple one-time commands</td>
</tr>
<tr class="even">
<td><code>subprocess.Popen()</code></td>
<td>Advanced process control</td>
<td>Background processes, real-time interaction</td>
</tr>
<tr class="odd">
<td><code>subprocess.check_output()</code></td>
<td>Get command output only</td>
<td>When you only need the result</td>
</tr>
</tbody>
</table>
<p><strong>Basic Program Launch:</strong></p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> subprocess</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run a Python script</span></span>
<span id="cb8-4">result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> subprocess.run([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'python'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'my_script.py'</span>], </span>
<span id="cb8-5">                       capture_output<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, </span>
<span id="cb8-6">                       text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb8-7"></span>
<span id="cb8-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> result.returncode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb8-9">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Success:"</span>, result.stdout)</span>
<span id="cb8-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb8-11">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Error:"</span>, result.stderr)</span></code></pre></div>
<p><strong>Important Parameters:</strong></p>
<ul>
<li><strong><code>capture_output=True</code></strong>: Capture the program’s output</li>
<li><strong><code>text=True</code></strong>: Get output as strings (not bytes)</li>
<li><strong><code>timeout=5</code></strong>: Prevent hanging by setting a time limit</li>
<li><strong><code>check=True</code></strong>: Raise an exception if the program fails</li>
</ul>
</section>
<section id="security-best-practices" class="level2">
<h2 class="anchored" data-anchor-id="security-best-practices">Security Best Practices</h2>
<p>Always use command lists instead of strings to prevent security vulnerabilities:</p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ Safe way</span></span>
<span id="cb9-2">subprocess.run([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'ls'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'-l'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/home/user'</span>])</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ❌ Dangerous way (vulnerable to injection)</span></span>
<span id="cb9-5">subprocess.run(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ls -l /home/user"</span>, shell<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span></code></pre></div>
</section>
</section>
<section id="practical-applications-putting-it-all-together" class="level1">
<h1>Practical Applications: Putting It All Together</h1>
<section id="automated-backup-script-with-logging" class="level2">
<h2 class="anchored" data-anchor-id="automated-backup-script-with-logging">Automated Backup Script with Logging</h2>
<p>Here’s a complete example combining time management, scheduling, and program launching:</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> subprocess</span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb10-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime</span>
<span id="cb10-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> schedule</span>
<span id="cb10-5"></span>
<span id="cb10-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_backup():</span>
<span id="cb10-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Create a timestamped backup"""</span></span>
<span id="cb10-8">    timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now().strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y%m</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">_%H%M%S"</span>)</span>
<span id="cb10-9">    backup_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"backup_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>timestamp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.tar.gz"</span></span>
<span id="cb10-10">    </span>
<span id="cb10-11">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Starting backup at </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>datetime<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>now()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'%H:%M:%S'</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-12">    </span>
<span id="cb10-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Measure backup time</span></span>
<span id="cb10-14">    start_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb10-15">    </span>
<span id="cb10-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create backup (example command)</span></span>
<span id="cb10-17">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb10-18">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> subprocess.run([</span>
<span id="cb10-19">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tar'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'-czf'</span>, backup_name, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/path/to/data'</span></span>
<span id="cb10-20">        ], capture_output<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, timeout<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>)</span>
<span id="cb10-21">        </span>
<span id="cb10-22">        end_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> time.time()</span>
<span id="cb10-23">        duration <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> end_time <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> start_time</span>
<span id="cb10-24">        </span>
<span id="cb10-25">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> result.returncode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb10-26">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Backup completed in </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>duration<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> seconds"</span>)</span>
<span id="cb10-27">            log_backup_success(backup_name, duration)</span>
<span id="cb10-28">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb10-29">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Backup failed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>stderr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-30">            </span>
<span id="cb10-31">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> subprocess.TimeoutExpired:</span>
<span id="cb10-32">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Backup timed out after 5 minutes"</span>)</span>
<span id="cb10-33"></span>
<span id="cb10-34"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> log_backup_success(filename, duration):</span>
<span id="cb10-35">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Log successful backup to file"""</span></span>
<span id="cb10-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'backup_log.txt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb10-37">        timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now().strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> %H:%M:%S"</span>)</span>
<span id="cb10-38">        f.write(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>timestamp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">] Backup created: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>filename<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> (</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>duration<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">s)</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-39"></span>
<span id="cb10-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Schedule daily backups</span></span>
<span id="cb10-41">schedule.every().day.at(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"02:00"</span>).do(create_backup)</span></code></pre></div>
</section>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p><strong>Challenge:</strong> Create a simple system monitor that checks disk space every 5 minutes and logs the results.</p>
<p><strong>Requirements:</strong></p>
<ul>
<li>Use <code>subprocess</code> to run a disk space command</li>
<li>Schedule the check to run every 5 minutes</li>
<li>Log results with timestamps</li>
<li>Display results in a readable format</li>
</ul>
<details>
<summary>
Click here for Solution!
</summary>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> subprocess</span>
<span id="cb11-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb11-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime</span>
<span id="cb11-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> schedule</span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> check_disk_space():</span>
<span id="cb11-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Monitor disk space and log results"""</span></span>
<span id="cb11-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb11-9">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get disk usage (Unix/Linux command)</span></span>
<span id="cb11-10">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> subprocess.run([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'df'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'-h'</span>], </span>
<span id="cb11-11">                              capture_output<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, </span>
<span id="cb11-12">                              text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>, </span>
<span id="cb11-13">                              timeout<span class="op" 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="cb11-14">        </span>
<span id="cb11-15">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> result.returncode <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>:</span>
<span id="cb11-16">            timestamp <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> datetime.now().strftime(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%d</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> %H:%M:%S"</span>)</span>
<span id="cb11-17">            </span>
<span id="cb11-18">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parse the output</span></span>
<span id="cb11-19">            lines <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result.stdout.strip().split(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb11-20">            root_line <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [line <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> line <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> lines <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> line.endswith(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'/'</span>)][<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb11-21">            usage <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> root_line.split()[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get usage percentage</span></span>
<span id="cb11-22">            </span>
<span id="cb11-23">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Log to file</span></span>
<span id="cb11-24">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'disk_monitor.log'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb11-25">                f.write(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"[</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>timestamp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">] Disk Usage: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>usage<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-26">            </span>
<span id="cb11-27">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Disk usage logged: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>usage<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> at </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>timestamp<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-28">            </span>
<span id="cb11-29">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Alert if usage is high</span></span>
<span id="cb11-30">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>(usage.strip(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'%'</span>)) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>:</span>
<span id="cb11-31">                <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⚠️  Warning: Disk usage above 80%!"</span>)</span>
<span id="cb11-32">                </span>
<span id="cb11-33">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb11-34">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error checking disk space: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-35"></span>
<span id="cb11-36"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Schedule the check</span></span>
<span id="cb11-37">schedule.every(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>).minutes.do(check_disk_space)</span>
<span id="cb11-38"></span>
<span id="cb11-39"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run scheduler</span></span>
<span id="cb11-40"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Starting disk space monitor..."</span>)</span>
<span id="cb11-41"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>:</span>
<span id="cb11-42">    schedule.run_pending()</span>
<span id="cb11-43">    time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details>
</section>
<section id="quick-takeaways" class="level1">
<h1>Quick Takeaways</h1>
<ul>
<li><strong>Use <code>time.time()</code> and <code>time.sleep()</code></strong> for basic timing and delays in your programs</li>
<li><strong>Master <code>datetime</code></strong> for working with dates, formatting, and date arithmetic</li>
<li><strong>Install and use the <code>schedule</code> library</strong> for easy task automation with human-readable syntax</li>
<li><strong>Always use <code>subprocess.run()</code></strong> instead of older methods like <code>os.system()</code> for launching programs</li>
<li><strong>Use command lists, not strings</strong> with subprocess for better security</li>
<li><strong>Capture output with <code>capture_output=True</code></strong> when you need to process program results</li>
<li><strong>Set timeouts</strong> to prevent your programs from hanging on external commands</li>
<li><strong>Combine these tools</strong> to create powerful automation scripts for backups, monitoring, and maintenance</li>
</ul>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Python’s time management and process control capabilities make it an excellent choice for automation and system administration tasks. By mastering the <code>time</code> and <code>datetime</code> modules for timekeeping, the <code>schedule</code> library for task automation, and the <code>subprocess</code> module for program launching, you’ll be able to build sophisticated scripts that handle complex workflows.</p>
<p>Start small with simple scheduled tasks, then gradually incorporate external program launches as you become more comfortable. The combination of these tools will dramatically expand what you can accomplish with Python automation.</p>
<p><strong>Ready to automate your workflow?</strong> Pick one repetitive task you do regularly and try scheduling it with Python. Your future self will thank you for the time saved!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><p><strong>Python Software Foundation</strong>. (2024). <em>time — Time access and conversions</em>. Python 3 Documentation. Retrieved from <a href="https://docs.python.org/3/library/time.html">https://docs.python.org/3/library/time.html</a></p></li>
<li><p><strong>Python Software Foundation</strong>. (2024). <em>subprocess — Subprocess management</em>. Python 3 Documentation. Retrieved from <a href="https://docs.python.org/3/library/subprocess.html">https://docs.python.org/3/library/subprocess.html</a></p></li>
<li><p><strong>Real Python Team</strong>. (2024). <em>A Beginner’s Guide to the Python time Module</em>. Real Python. Retrieved from <a href="https://realpython.com/python-time-module/">https://realpython.com/python-time-module/</a></p></li>
<li><p><strong>schedule Library Contributors</strong>. (2024). <em>schedule: Python job scheduling for humans</em>. Schedule Documentation. Retrieved from <a href="https://schedule.readthedocs.io/">https://schedule.readthedocs.io/</a></p></li>
</ol>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-10-23/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Time and Scheduling with Python</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-10-23/</guid>
  <pubDate>Thu, 23 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Master Data Cleaning in R: Three Methods for Removing Empty Rows</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-10-20/</link>
  <description><![CDATA[ 






<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>For every <strong>R programmer</strong>, data wrangling is a daily task, and few things are more frustrating than incomplete, “empty” rows silently contaminating your analysis. While “empty” can mean a row entirely composed of missing values (<strong>NAs</strong>), it more often refers to a row containing <em>at least one</em> missing value.</p>
<p>Efficiently identifying and purging these incomplete records is crucial for maintaining data integrity. Fortunately, R offers powerful, flexible tools in both <strong>Base R</strong> and the popular <strong>Tidyverse</strong> ecosystem to handle this.</p>
<p>In this guide, we’ll go into three core methods; two rooted in Base R and one modern <strong><code>dplyr</code></strong> solution. We’ll put all three techniques to the test using the <strong><code>rbenchmark</code></strong> package to settle the debate on speed and efficiency.</p>
</section>
<section id="the-base-r-standard-using-complete.cases" class="level1">
<h1>The Base R Standard: Using <code>complete.cases()</code></h1>
<p>The most common and often the fastest way to remove rows containing <em>any</em> <code>NA</code> is by leveraging the built-in Base R function, <strong><code>complete.cases()</code></strong>. This method is the workhorse of R data cleaning, offering a balance of speed and simplicity.</p>
<section id="deep-dive-into-complete.cases" class="level2">
<h2 class="anchored" data-anchor-id="deep-dive-into-complete.cases">Deep Dive into <code>complete.cases()</code></h2>
<p>The <code>complete.cases()</code> function returns a logical vector (<code>TRUE</code> or <code>FALSE</code>) for each row in a data frame. <strong><code>TRUE</code></strong> indicates the row contains <strong>no missing values</strong>, and <strong><code>FALSE</code></strong> means it contains <strong>at least one <code>NA</code></strong>.</p>
<p>When used inside the subsetting brackets <code>[ ]</code>, it acts as a powerful filter, keeping only the rows where the result is <code>TRUE</code>.</p>
<p><strong>Example Code:</strong></p>
<div class="cell">
<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;"># 1. Setup Sample Data</span></span>
<span id="cb1-2">df_test <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-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</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;">40</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>),</span>
<span id="cb1-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">80</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>),</span>
<span id="cb1-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</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;">7</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;">9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-6">)</span>
<span id="cb1-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original Data Frame:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Original Data Frame:"</code></pre>
</div>
<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;">print</span>(df_test)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   x  y  z
1 10 NA  6
2 20 50  7
3 NA NA NA
4 40 80  9
5 NA 90 10</code></pre>
</div>
<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;"># 2. Method 1: Remove rows with NA in at least one column</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The comma at the end applies the filter to the rows (before the comma is the row index)</span></span>
<span id="cb5-3">df_cleaned_cc <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_test[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">complete.cases</span>(df_test), ]</span>
<span id="cb5-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cleaned using complete.cases():"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Cleaned using complete.cases():"</code></pre>
</div>
<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;">print</span>(df_cleaned_cc)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   x  y z
2 20 50 7
4 40 80 9</code></pre>
</div>
</div>
<p>In this example, <code>complete.cases(df_test)</code> returns <code>FALSE</code> for any row that is not fully populated, effectively removing both the row where only column <code>x</code> is missing and the row where all columns are missing.</p>
</section>
</section>
<section id="addressing-truly-empty-rows-with-rowsums" class="level1">
<h1>Addressing Truly Empty Rows with <code>rowSums()</code></h1>
<p>While <strong><code>complete.cases()</code></strong> is great for removing <em>incomplete</em> records, sometimes you only want to remove rows that are <em>entirely</em> empty (i.e., every cell in that row is <code>NA</code>). This requires a slightly different approach using <strong><code>rowSums()</code></strong> and <strong><code>is.na()</code></strong>.</p>
<p>This method works by:</p>
<ol type="1">
<li><strong><code>is.na(df)</code>:</strong> Converts the data frame into a matrix of logical values (<code>TRUE</code> where there’s an <code>NA</code>).</li>
<li><strong><code>rowSums(...)</code>:</strong> Sums the <code>TRUE</code> values (which are treated as 1s) across each row, counting the total number of NAs per row.</li>
<li><strong><code>!= ncol(df)</code>:</strong> Filters the rows where the sum of NAs is <em>not equal</em> to the total number of columns. If the row sum of NAs <em>equals</em> the number of columns, the row is 100% empty and is removed.</li>
</ol>
<p><strong>Example Code:</strong></p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setup data frame where row 3 is fully NA</span></span>
<span id="cb9-2">df_truly_empty <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="cb9-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</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;">10</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>),</span>
<span id="cb9-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</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;">50</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</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;">80</span>),</span>
<span id="cb9-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">z =</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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb9-6">)</span>
<span id="cb9-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original Data Frame:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Original Data Frame:"</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(df_truly_empty)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   x  y  z
1 10 50  1
2 NA 60  2
3 NA NA NA
4 40 80  4</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 2: Remove rows where all columns are NA</span></span>
<span id="cb13-2">df_cleaned_rs <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_truly_empty[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(df_truly_empty)) <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;">ncol</span>(df_truly_empty), ]</span>
<span id="cb13-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cleaned using rowSums():"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Cleaned using rowSums():"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(df_cleaned_rs)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   x  y z
1 10 50 1
2 NA 60 2
4 40 80 4</code></pre>
</div>
</div>
<p>Notice how row 3 is removed, but row 2 (which had only one <code>NA</code>) is retained.</p>
</section>
<section id="the-tidyverse-approach-dplyrdrop_na" class="level1">
<h1>The Tidyverse Approach: <code>dplyr::drop_na()</code></h1>
<p>For those who prefer the readable, pipe-friendly syntax of the <strong>Tidyverse</strong>, the <strong><code>tidyr</code></strong> package offers the concise <strong><code>drop_na()</code></strong> function. This method achieves the same result as <code>complete.cases()</code>—removing all rows with <em>at least one</em> missing value.</p>
<p>The primary advantage here is <strong>readability</strong>; the intent is immediately clear, especially when chaining multiple data operations.</p>
<p><strong>Example Code:</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Ensure dplyr is loaded</span></span>
<span id="cb17-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyr)</span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 3: Remove rows with NA using dplyr</span></span>
<span id="cb17-5">df_cleaned_tidyr <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_test <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb17-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>()</span>
<span id="cb17-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cleaned using tidyr::drop_na():"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Cleaned using tidyr::drop_na():"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(df_cleaned_tidyr)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   x  y z
1 20 50 7
2 40 80 9</code></pre>
</div>
</div>
</section>
<section id="performance-matters-benchmarking-removal-methods" class="level1">
<h1>Performance Matters: Benchmarking Removal Methods</h1>
<p>As R programmers, we care about more than just syntax—we care about efficiency. While <code>tidyr</code> is often favored for readability, Base R methods can sometimes offer a performance edge on very large datasets.</p>
<p>We use the <strong><code>rbenchmark</code></strong> package to compare the two common methods for removing incomplete rows: Base R <code>complete.cases()</code> and the <code>tidyr::drop_na()</code> function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rbenchmark)</span>
<span id="cb21-2"></span>
<span id="cb21-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a large test data frame (10,000 rows)</span></span>
<span id="cb21-4"><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="cb21-5">big_df <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.data.frame</span>(</span>
<span id="cb21-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">matrix</span>(</span>
<span id="cb21-7">        <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="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;">100</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;">10000</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;">5</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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rep</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.95</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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>), <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>)),</span>
<span id="cb21-8">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span></span>
<span id="cb21-9">    )</span>
<span id="cb21-10">)</span>
<span id="cb21-11"></span>
<span id="cb21-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Benchmark the two common methods</span></span>
<span id="cb21-13">benchmark_results <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;">benchmark</span>(</span>
<span id="cb21-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">BaseR_Complete =</span> big_df[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">complete.cases</span>(big_df), ],</span>
<span id="cb21-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tidyr_DropNa =</span> big_df <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;">drop_na</span>(),</span>
<span id="cb21-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replications =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb21-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">"test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"replications"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative"</span>)</span>
<span id="cb21-18">)</span>
<span id="cb21-19"></span>
<span id="cb21-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(benchmark_results[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(benchmark_results<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>relative), ])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>            test replications elapsed relative
2   tidyr_DropNa         1000    0.44    1.000
1 BaseR_Complete         1000    1.66    3.773</code></pre>
</div>
</div>
<p>The benchmarking results consistently show that the <strong>Base R <code>complete.cases()</code> method is slower</strong> than <code>tidyr::drop_na()</code> when executing the same operation on large data frames. For most day-to-day tasks, this difference is negligible, even though there is a large speedup but for massive datasets or functions running millions of times, tidyr retains a slight performance advantage.</p>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p>Imagine you have a data frame of customer survey responses. You want to remove only those rows where <em>both</em> the <code>Satisfaction</code> and <code>Usage</code> columns are missing, allowing rows with <code>NA</code> in only one of them to remain.</p>
<p><strong>Your Task:</strong> Write the Base R code to remove rows where <code>Satisfaction</code> and <code>Usage</code> are <em>both</em> <code>NA</code> from the data frame <code>survey_df</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1">survey_df <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="cb23-2">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Customer =</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;">5</span>,</span>
<span id="cb23-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Satisfaction =</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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</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;">4</span>),</span>
<span id="cb23-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Usage =</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;">10</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>)</span>
<span id="cb23-5">)</span></code></pre></div>
</div>
<details>
<summary>
See Solution!
</summary>
<p>The key is to use <code>is.na()</code> on the specific columns and combine the logical vectors with the <strong>AND</strong> operator (<code>&amp;</code>):</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Logical vector where both are NA</span></span>
<span id="cb24-2">is_double_na <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;">is.na</span>(survey_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Satisfaction) <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>(survey_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Usage)</span>
<span id="cb24-3"></span>
<span id="cb24-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filter the data frame to keep rows where 'is_double_na' is FALSE</span></span>
<span id="cb24-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The '!' negates the logical vector, keeping rows that are NOT double NA</span></span>
<span id="cb24-6">cleaned_survey_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> survey_df[<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>is_double_na, ]</span>
<span id="cb24-7"></span>
<span id="cb24-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(cleaned_survey_df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Customer Satisfaction Usage
1        1            5    10
2        2           NA     5
3        3            3    NA
5        5            4     8</code></pre>
</div>
</div>
</details>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<ul>
<li><strong>Most Readable:</strong> <code>df %&gt;% drop_na()</code> (Tidyverse) offers the clearest, most readable syntax for Tidyverse users.</li>
<li><strong>Targeted Cleaning:</strong> You can use <code>complete.cases(df[ , c("col1", "col2")])</code> to check for completeness in only a subset of columns.</li>
<li><strong>Truly Empty Rows:</strong> Use the <code>rowSums(is.na(df)) != ncol(df)</code> method to target and remove only rows that are <strong>100% missing</strong>.</li>
</ul>
</section>
<section id="frequently-asked-questions-faqs" class="level1">
<h1>Frequently Asked Questions (FAQs)</h1>
<ol type="1">
<li><p><strong>What is the difference between <code>complete.cases()</code> and <code>na.omit()</code>?</strong></p>
<ul>
<li><code>complete.cases()</code> returns a logical vector (<code>TRUE</code>/<code>FALSE</code>) that you can use for flexible subsetting. <code>na.omit()</code> is a high-level function that directly returns the data frame with all incomplete rows removed. For simple cleaning, the results are functionally the same.</li>
</ul></li>
<li><p><strong>Can I use <code>drop_na()</code> on specific columns?</strong></p>
<ul>
<li>Yes. You can pass specific column names to the function: <code>df %&gt;% drop_na(column_a, column_b)</code>. This removes rows only if they have an <code>NA</code> in the specified columns.</li>
</ul></li>
<li><p><strong>Why do my empty rows sometimes show up as <code>""</code> (empty strings) instead of <code>NA</code>?</strong></p>
<ul>
<li>This is common when reading messy CSVs. If a cell is truly blank, R might import it as <code>""</code>. You must first convert these empty strings to the official <code>NA</code> missing value before using the functions discussed: <code>df[df == ""] &lt;- NA</code>.</li>
</ul></li>
<li><p><strong>Is one method always better than the others?</strong></p>
<ul>
<li>No.&nbsp;For raw performance on massive datasets, Base R often wins. For clear, pipe-friendly code in a Tidyverse project, <code>dplyr::drop_na()</code> is preferred. Choose the method that best fits your coding environment and performance needs.</li>
</ul></li>
<li><p><strong>Does <code>complete.cases()</code> consider rows with <code>NaN</code>?</strong></p>
<ul>
<li>No, <code>complete.cases()</code> only checks for <code>NA</code> (Not Available). If you need to include <code>NaN</code> (Not a Number, often used in mathematical operations) as a missing value, you should first convert it to <code>NA</code> or use a custom function.</li>
</ul></li>
</ol>
</section>
<section id="conclusion-choosing-your-cleaning-tool" class="level1">
<h1>Conclusion: Choosing Your Cleaning Tool</h1>
<p>Removing empty or incomplete rows is a foundational skill in R. Whether you are a performance purist favoring the speed of <strong><code>complete.cases()</code></strong> or a Tidyverse enthusiast prioritizing the clarity of <strong><code>dplyr::drop_na()</code></strong>, R provides the perfect tool for your data cleaning toolkit.</p>
<p>Choosing between them ultimately boils down to balancing performance benchmarks with code readability. Start cleaning your data frames today for more accurate, robust statistical models!</p>
<p><strong>What’s your go-to method?</strong> Share your preferred data cleaning function in the comments below!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ul>
<li>Statology. (n.d.). <em>How to Remove Empty Rows from Data Frame in R</em>. Retrieved from <a href="https://www.statology.org/remove-empty-rows-in-r/">https://www.statology.org/remove-empty-rows-in-r/</a></li>
<li>Wickham, H., François, R., Henry, L., &amp; Müller, K. (2024). <em>dplyr: A Grammar of Data Manipulation</em>. R package version 1.1.4.</li>
<li>R Core Team (2024). <em>R: A language and environment for statistical computing</em>. R Foundation for Statistical Computing, Vienna, Austria. URL <a href="https://www.R-project.org/">https://www.R-project.org/</a>.</li>
</ul>
<hr>
<p>Happy Coding! 🚀</p>
<section id="getting-complete-rows-in-r" class="level2">
<h2 class="anchored" data-anchor-id="getting-complete-rows-in-r"><img src="https://www.spsanderson.com/steveondata/posts/2025-10-20/todays_post.jpeg" class="img-fluid" alt="Getting Complete Rows in R"></h2>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>
</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-10-20/</guid>
  <pubDate>Mon, 20 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Select Row with Max Value in Specific Column in R</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-10-06/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong> R offers some powerful methods to select rows with maximum values: Base R (simple, no dependencies), dplyr (readable, tidyverse-friendly), and data.table (fast performance). Each has distinct advantages for different scenarios.</p>
</blockquote>
<p>Finding rows with maximum values in a specific column is a common operation in data analysis. You could be trying to identify top performers, peak measurements, or maximum scores, R provides multiple efficient approaches. This guide compares <strong>Base R</strong>, <strong>dplyr</strong>, and <strong>data.table</strong> methods with performance insights and practical examples.</p>
<hr>
<section id="sample-data-setup" class="level1">
<h1>Sample Data Setup</h1>
<p>Let’s start with a sample dataset to demonstrate each method:</p>
<div class="cell">
<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;"># Sample data</span></span>
<span id="cb1-2">data <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-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ID =</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 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;">5</span>),</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Value =</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;">10</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;">15</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;">20</span>),</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Group =</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;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>)</span>
<span id="cb1-6">)</span>
<span id="cb1-7"></span>
<span id="cb1-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(data)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  ID Value Group
1  1    10     A
2  2    25     A
3  3    15     B
4  4    25     B
5  5    20     C</code></pre>
</div>
</div>
</section>
<section id="base-r-methods" class="level1">
<h1>Base R Methods</h1>
<section id="first-row-with-max-value" class="level2">
<h2 class="anchored" data-anchor-id="first-row-with-max-value">First Row with Max Value</h2>
<p>Use <code>which.max()</code> to get the index of the first maximum value:</p>
<div class="cell">
<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;"># Returns first occurrence only</span></span>
<span id="cb3-2">data[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Value), ]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  ID Value Group
2  2    25     A</code></pre>
</div>
</div>
<p><strong>Result:</strong> Row 2 (ID=2, Value=25)</p>
</section>
<section id="all-rows-with-max-value-handling-ties" class="level2">
<h2 class="anchored" data-anchor-id="all-rows-with-max-value-handling-ties">All Rows with Max Value (Handling Ties)</h2>
<p>Use logical subsetting to capture all maximum values:</p>
<div class="cell">
<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;"># Returns all rows with max value</span></span>
<span id="cb5-2">data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Value <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>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Value), ]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  ID Value Group
2  2    25     A
4  4    25     B</code></pre>
</div>
</div>
</section>
</section>
<section id="dplyr-methods" class="level1">
<h1>dplyr Methods</h1>
<p>The <strong>dplyr</strong> package offers <code>slice_max()</code> for intuitive max row selection .</p>
<section id="first-max-row" class="level2">
<h2 class="anchored" data-anchor-id="first-max-row">First Max Row</h2>
<div class="cell">
<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;">library</span>(dplyr)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First max row only</span></span>
<span id="cb7-4">data <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;">slice_max</span>(Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">with_ties =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  ID Value Group
1  2    25     A</code></pre>
</div>
</div>
</section>
<section id="all-max-rows-including-ties" class="level2">
<h2 class="anchored" data-anchor-id="all-max-rows-including-ties">All Max Rows (Including Ties)</h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All rows with max value (default behavior)</span></span>
<span id="cb9-2">data <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;">slice_max</span>(Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  ID Value Group
1  2    25     A
2  4    25     B</code></pre>
</div>
</div>
</section>
<section id="grouped-max-selection" class="level2">
<h2 class="anchored" data-anchor-id="grouped-max-selection">Grouped Max Selection</h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Max row per group</span></span>
<span id="cb11-2">data <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;">group_by</span>(Group) <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;">slice_max</span>(Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 3
# Groups:   Group [3]
     ID Value Group
  &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt;
1     2    25 A    
2     4    25 B    
3     5    20 C    </code></pre>
</div>
</div>
</section>
</section>
<section id="data.table-methods" class="level1">
<h1>data.table Methods</h1>
<p><strong>data.table</strong> provides the fastest performance for large datasets .</p>
<section id="setup-and-basic-selection" class="level2">
<h2 class="anchored" data-anchor-id="setup-and-basic-selection">Setup and Basic Selection</h2>
<div class="cell">
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb13-2">dt <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.data.table</span>(data)</span>
<span id="cb13-3"></span>
<span id="cb13-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First max row</span></span>
<span id="cb13-5">dt[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(Value)]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>      ID Value  Group
   &lt;num&gt; &lt;num&gt; &lt;char&gt;
1:     2    25      A</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># All max rows</span></span>
<span id="cb15-2">dt[Value <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>(Value)]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>      ID Value  Group
   &lt;num&gt; &lt;num&gt; &lt;char&gt;
1:     2    25      A
2:     4    25      B</code></pre>
</div>
</div>
</section>
<section id="grouped-max-selection-1" class="level2">
<h2 class="anchored" data-anchor-id="grouped-max-selection-1">Grouped Max Selection</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Max row per group (fastest method)</span></span>
<span id="cb17-2">dt[, .SD[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(Value)], by <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> Group]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>    Group    ID Value
   &lt;char&gt; &lt;num&gt; &lt;num&gt;
1:      A     2    25
2:      B     4    25
3:      C     5    20</code></pre>
</div>
</div>
</section>
</section>
<section id="performance-comparison-using-rbenchmark" class="level1">
<h1>Performance Comparison Using rbenchmark</h1>
<p>Row selection (filtering) is one of the most common data manipulation operations. Let’s compare the performance of Base R, dplyr, and data.table for filtering operations using the <code>rbenchmark</code> package .</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Install and load required packages</span></span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rbenchmark)</span>
<span id="cb19-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(data.table)</span>
<span id="cb19-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb19-5"></span>
<span id="cb19-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create sample data</span></span>
<span id="cb19-7"><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;">123</span>)</span>
<span id="cb19-8">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;">100000</span>L</span>
<span id="cb19-9">dt <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.table</span>(</span>
<span id="cb19-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</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>n,</span>
<span id="cb19-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">category =</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;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>), n, <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="cb19-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rnorm</span>(n, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>),</span>
<span id="cb19-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">flag =</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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), n, <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="cb19-14">)</span>
<span id="cb19-15"></span>
<span id="cb19-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to data.frame for base R and dplyr</span></span>
<span id="cb19-17">df <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.data.frame</span>(dt)</span>
<span id="cb19-18"></span>
<span id="cb19-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Benchmark: Finding rows with maximum value</span></span>
<span id="cb19-20">result <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;">benchmark</span>(</span>
<span id="cb19-21">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_R =</span> df[df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value <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>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>value), ],</span>
<span id="cb19-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_table =</span> dt[value <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>(value)],</span>
<span id="cb19-23">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dplyr =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(df, value <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>(value)),</span>
<span id="cb19-24">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replications =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>,</span>
<span id="cb19-25">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">"test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"replications"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative"</span>)</span>
<span id="cb19-26">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb19-27">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(relative)</span>
<span id="cb19-28"></span>
<span id="cb19-29"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(result)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>        test replications elapsed relative
1     base_R         1000    1.72    1.000
2      dplyr         1000    4.25    2.471
3 data_table         1000    6.10    3.547</code></pre>
</div>
</div>
</section>
<section id="handling-edge-cases" class="level1">
<h1>Handling Edge Cases</h1>
<section id="missing-values-na" class="level2">
<h2 class="anchored" data-anchor-id="missing-values-na">Missing Values (NA)</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Base R: Remove NAs</span></span>
<span id="cb21-2">data[data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Value <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>(data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Value, <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 class="cell-output cell-output-stdout">
<pre><code>  ID Value Group
2  2    25     A
4  4    25     B</code></pre>
</div>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># dplyr: Filter NAs first</span></span>
<span id="cb23-2">data <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>(<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>(Value)) <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;">slice_max</span>(Value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  ID Value Group
1  2    25     A
2  4    25     B</code></pre>
</div>
</div>
</section>
<section id="all-values-are-na" class="level2">
<h2 class="anchored" data-anchor-id="all-values-are-na">All Values are NA</h2>
<p>Always include error handling for edge cases where no maximum can be determined.</p>
</section>
</section>
<section id="method-selection-guide" class="level1">
<h1>Method Selection Guide</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 26%">
<col style="width: 50%">
<col style="width: 23%">
</colgroup>
<thead>
<tr class="header">
<th>Scenario</th>
<th>Recommended Method</th>
<th>Reason</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Small data (&lt;1K rows)</strong></td>
<td>Any method</td>
<td>Performance differences minimal</td>
</tr>
<tr class="even">
<td><strong>Medium data (1K-10K)</strong></td>
<td>Base R or data.table</td>
<td>Good performance balance</td>
</tr>
<tr class="odd">
<td><strong>Large data (&gt;10K rows)</strong></td>
<td><strong>data.table</strong></td>
<td>Best performance scaling</td>
</tr>
<tr class="even">
<td><strong>Readability priority</strong></td>
<td>dplyr</td>
<td>Clear, expressive syntax</td>
</tr>
<tr class="odd">
<td><strong>No dependencies</strong></td>
<td>Base R</td>
<td>Built-in functionality</td>
</tr>
</tbody>
</table>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p>Now it’s your turn to experiment with <code>rbenchmark</code> and deepen your understanding of R performance optimization!</p>
<section id="exercise-1-string-operations-benchmark" class="level2">
<h2 class="anchored" data-anchor-id="exercise-1-string-operations-benchmark">Exercise 1: String Operations Benchmark</h2>
<p>Compare the performance of different string matching methods:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(stringr)</span>
<span id="cb25-2"></span>
<span id="cb25-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create test data</span></span>
<span id="cb25-4">text_data <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="cb25-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</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;">50000</span>,</span>
<span id="cb25-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</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;">"apple"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"banana"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cherry"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"date"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elderberry"</span>), </span>
<span id="cb25-7">                <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50000</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="cb25-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb25-9">)</span>
<span id="cb25-10"></span>
<span id="cb25-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your task: Benchmark these approaches for finding rows containing "app"</span></span>
<span id="cb25-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">benchmark</span>(</span>
<span id="cb25-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_R =</span> text_data[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"app"</span>, text_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text), ],</span>
<span id="cb25-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_R_fixed =</span> text_data[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">grepl</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"app"</span>, text_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fixed =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), ],</span>
<span id="cb25-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringr =</span> text_data[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">str_detect</span>(text_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>text, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"app"</span>), ],</span>
<span id="cb25-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replications =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,</span>
<span id="cb25-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">"test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative"</span>)</span>
<span id="cb25-18">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb25-19">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(relative)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>          test elapsed relative
1 base_R_fixed    0.26    1.000
2       base_R    0.95    3.654
3      stringr    1.30    5.000</code></pre>
</div>
</div>
<p><strong>Question</strong>: Which method is fastest? Why might <code>fixed = TRUE</code> make a difference?</p>
</section>
<section id="exercise-2-aggregation-benchmark" class="level2">
<h2 class="anchored" data-anchor-id="exercise-2-aggregation-benchmark">Exercise 2: Aggregation Benchmark</h2>
<p>Compare grouping and summarization methods:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create grouping data</span></span>
<span id="cb27-2">group_data <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="cb27-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(LETTERS[<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;">10</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10000</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="cb27-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</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;">10000</span>)</span>
<span id="cb27-5">)</span>
<span id="cb27-6">dt_group <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.data.table</span>(group_data)</span>
<span id="cb27-7"></span>
<span id="cb27-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your task: Benchmark mean calculation by group</span></span>
<span id="cb27-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">benchmark</span>(</span>
<span id="cb27-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">base_R =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aggregate</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> group, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> group_data, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">FUN =</span> mean),</span>
<span id="cb27-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_table =</span> dt_group[, .(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(value)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> group],</span>
<span id="cb27-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dplyr =</span> group_data <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;">group_by</span>(group) <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;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mean_value =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(value)),</span>
<span id="cb27-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replications =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,</span>
<span id="cb27-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">"test"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative"</span>)</span>
<span id="cb27-15">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb27-16">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(relative)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>        test elapsed relative
1 data_table    0.23    1.000
2      dplyr    0.67    2.913
3     base_R    1.23    5.348</code></pre>
</div>
</div>
<p><strong>Challenge</strong>: Try with different aggregation functions (median, sd, length). Do the relative performance patterns change?</p>
</section>
<section id="exercise-3-memory-efficiency-test" class="level2">
<h2 class="anchored" data-anchor-id="exercise-3-memory-efficiency-test">Exercise 3: Memory Efficiency Test</h2>
<p>Investigate memory usage alongside timing:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your task: Compare memory efficiency</span></span>
<span id="cb29-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(pryr)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for object_size()</span></span>
<span id="cb29-3"></span>
<span id="cb29-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create copies for fair comparison</span></span>
<span id="cb29-5">df_copy <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df</span>
<span id="cb29-6">dt_copy <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;">copy</span>(dt)</span>
<span id="cb29-7"></span>
<span id="cb29-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Time and measure memory for column addition</span></span>
<span id="cb29-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system.time</span>({</span>
<span id="cb29-10">  df_result <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;">transform</span>(df_copy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">new_col =</span> value <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>
<span id="cb29-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Base R result size:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">object_size</span>(df_result), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb29-12">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Base R result size: 3201456 </code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>   user  system elapsed 
   0.02    0.00    0.03 </code></pre>
</div>
<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;">system.time</span>({</span>
<span id="cb32-2">  dt_copy[, new_col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">=</span> value <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>
<span id="cb32-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.table result size:"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">object_size</span>(dt_copy), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb32-4">})</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>data.table result size: 3602088 </code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>   user  system elapsed 
      0       0       0 </code></pre>
</div>
</div>
<p><strong>Question</strong>: Which approach uses less memory? Why might this matter for large datasets?</p>
</section>
<section id="exercise-4-your-own-max-value-challenge" class="level2">
<h2 class="anchored" data-anchor-id="exercise-4-your-own-max-value-challenge">Exercise 4: Your Own Max Value Challenge</h2>
<p>Apply what you’ve learned to your own dataset:</p>
<ol type="1">
<li><strong>Load your data</strong> (or create a sample with <code>rnorm()</code> and <code>sample()</code>)</li>
<li><strong>Identify the column</strong> you want to find maximum values for</li>
<li><strong>Test all three methods</strong> (Base R, dplyr, data.table) for finding max rows</li>
<li><strong>Benchmark the performance</strong> using <code>rbenchmark</code> with at least 50 replications</li>
<li><strong>Analyze the results</strong>: Which method works best for your specific use case?</li>
</ol>
<p><strong>Bonus Challenge</strong>: Try grouped maximum operations if your data has natural grouping variables!</p>
</section>
</section>
<section id="quick-takeaways" class="level1">
<h1>Quick Takeaways</h1>
<p>Some quick easy takeaways from this guide:</p>
<p>• <strong>Base R</strong> <code>which.max()</code> and logical subsetting provide simple, dependency-free solutions • <strong>dplyr</strong> <code>slice_max()</code> offers the most readable syntax with excellent tie handling • <strong>data.table</strong> delivers superior performance, especially for large datasets and grouped operations • <strong>rbenchmark</strong> helps you make data-driven decisions about method selection • Always consider handling NA values and ties in real-world applications • Choose your method based on dataset size, performance requirements, and code readability preferences • Performance differences become more significant with larger datasets and complex operations</p>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Selecting rows with maximum values in R is straightforward with all three approaches. <strong>Base R methods</strong> work well for most scenarios without additional packages. <strong>dplyr</strong> excels when code readability matters most. <strong>data.table</strong> is your best choice for performance-critical applications with large datasets.</p>
<p>The <code>rbenchmark</code> package provides valuable insights into actual performance differences, helping you make informed decisions about which method to use for your specific situation .</p>
<p><strong>Your turn:</strong> Try implementing these methods with your own data and compare the performance differences. Start with the approach that best fits your current workflow and data size requirements, then optimize based on your benchmarking results!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><p><strong>Sanderson, S.</strong> (2024, December 10). How to Select Row with Max Value in Specific Column in R: A Complete Guide. <em>R-bloggers</em>. https://www.r-bloggers.com/2024/12/how-to-select-row-with-max-value-in-specific-column-in-r-a-complete-guide/</p></li>
<li><p><strong>Statology.</strong> (n.d.). R: How to Select Row with Max Value in Specific Column. <em>Statology</em>. Retrieved October 6, 2025, from https://www.statology.org/r-select-row-with-max-value/</p></li>
</ol>
<hr>
<p>Happy Coding! 🚀</p>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-10-06/</guid>
  <pubDate>Mon, 06 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Working with CSV Files and JSON Data in Python</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-10-01/</link>
  <description><![CDATA[ 






<p><em>Authors Note: I am learning as I write this series, so I might make mistakes or do things that are not as efficient as they could be.</em></p>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Python offers straightforward ways to handle CSV and JSON files, two common formats for storing and exchanging data. This guide shows how to read, write, and convert between these formats using Python’s built-in tools and popular libraries.</p>
</section>
<section id="reading-and-writing-csv-files" class="level1">
<h1>Reading and Writing CSV Files</h1>
<p>Python’s <code>csv</code> module provides simple methods for CSV operations:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> csv</span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reading a CSV file</span></span>
<span id="cb1-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.csv'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb1-5">    reader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> csv.reader(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>)</span>
<span id="cb1-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> reader:</span>
<span id="cb1-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(row)</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writing to CSV</span></span>
<span id="cb1-10"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'output.csv'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb1-11">    writer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> csv.writer(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>)</span>
<span id="cb1-12">    writer.writerow([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Name'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Age'</span>])</span>
<span id="cb1-13">    writer.writerow([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Alice'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>])</span></code></pre></div>
<p>For more advanced data handling, the pandas library simplifies CSV operations:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read CSV into a DataFrame</span></span>
<span id="cb2-4">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.read_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.csv'</span>)</span>
<span id="cb2-5"></span>
<span id="cb2-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Write DataFrame to CSV</span></span>
<span id="cb2-7">data.to_csv(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'output.csv'</span>, index<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span></code></pre></div>
</section>
<section id="working-with-json-data" class="level1">
<h1>Working with JSON Data</h1>
<p>Python’s <code>json</code> module makes JSON operations simple:</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reading JSON</span></span>
<span id="cb3-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.json'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb3-5">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.load(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>)</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Writing JSON</span></span>
<span id="cb3-8"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'output.json'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb3-9">    json.dump(data, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>, indent<span class="op" 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></code></pre></div>
</section>
<section id="converting-between-csv-and-json" class="level1">
<h1>Converting Between CSV and JSON</h1>
<p>Converting between formats is straightforward:</p>
<p><strong>CSV to JSON:</strong></p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> csv, json</span>
<span id="cb4-2"></span>
<span id="cb4-3">csv_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb4-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.csv'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb4-5">    reader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> csv.DictReader(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>)</span>
<span id="cb4-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> reader:</span>
<span id="cb4-7">        csv_data.append(row)</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'output.json'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb4-10">    json.dump(csv_data, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>, indent<span class="op" 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></code></pre></div>
<p><strong>JSON to CSV:</strong></p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> csv, json</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'data.json'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb5-4">    json_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.load(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>)</span>
<span id="cb5-5"></span>
<span id="cb5-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'output.csv'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb5-7">    writer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> csv.DictWriter(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>, fieldnames<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>json_data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].keys())</span>
<span id="cb5-8">    writer.writeheader()</span>
<span id="cb5-9">    writer.writerows(json_data)</span></code></pre></div>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<ol type="1">
<li>Create a CSV file with sample data (name, email, age)</li>
<li>Write a Python script to convert it to JSON</li>
<li>Modify the JSON data by adding a new field</li>
<li>Convert the modified JSON back to CSV</li>
</ol>
<details>
<summary>
Solution Example
</summary>
<div id="d99725c6" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Create sample CSV</span></span>
<span id="cb6-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> csv</span>
<span id="cb6-3">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'name'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'email'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'age'</span>],</span>
<span id="cb6-4">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Alice'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'alice@example.com'</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">28</span>],</span>
<span id="cb6-5">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Bob'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'bob@example.com'</span>,<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">35</span>]]</span>
<span id="cb6-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Users</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teve</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\D</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocuments</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\G</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">itHub</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teveondata</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">posts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\202</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">5-10-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ample</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">csv'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb6-7">    writer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> csv.writer(f)</span>
<span id="cb6-8">    writer.writerows(data)</span>
<span id="cb6-9"></span>
<span id="cb6-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Convert to JSON</span></span>
<span id="cb6-11"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb6-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Users</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teve</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\D</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocuments</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\G</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">itHub</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teveondata</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">posts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\202</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">5-10-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ample</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">csv'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb6-13">    reader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> csv.DictReader(f)</span>
<span id="cb6-14">    rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>(reader)</span>
<span id="cb6-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Users</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teve</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\D</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocuments</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\G</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">itHub</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teveondata</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">posts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\202</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">5-10-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ample</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">json'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb6-16">    json.dump(rows, f, indent<span class="op" 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>
<span id="cb6-17"></span>
<span id="cb6-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: Add new field</span></span>
<span id="cb6-19"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Users</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teve</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\D</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocuments</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\G</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">itHub</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teveondata</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">posts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\202</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">5-10-01</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ample</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">json'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb6-20">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.load(f)</span>
<span id="cb6-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> item <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> data:</span>
<span id="cb6-22">    item[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'status'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'active'</span></span>
<span id="cb6-23"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Users</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teve</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\D</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocuments</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\G</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">itHub</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teveondata</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">posts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\202</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">5-10-01</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">updated</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">json'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb6-24">    json.dump(data, f, indent<span class="op" 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>
<span id="cb6-25"></span>
<span id="cb6-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: Convert back to CSV</span></span>
<span id="cb6-27"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Users</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teve</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\D</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocuments</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\G</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">itHub</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teveondata</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">posts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\202</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">5-10-01</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">updated</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">json'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb6-28">    data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> json.load(f)</span>
<span id="cb6-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">Users</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teve</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\D</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">ocuments</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\G</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">itHub</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">\s</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">teveondata</span><span class="er" style="color: #AD0000;
background-color: null;
font-style: inherit;">\</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">posts</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\202</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">5-10-01</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\f</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">inal</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">csv'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb6-30">    writer <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> csv.DictWriter(f, fieldnames<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>data[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>].keys())</span>
<span id="cb6-31">    writer.writeheader()</span>
<span id="cb6-32">    writer.writerows(data)</span></code></pre></div>
</div>
</details>
</section>
<section id="key-points" class="level1">
<h1>Key Points</h1>
<ul>
<li>The <code>csv</code> module handles basic CSV operations</li>
<li>pandas provides advanced CSV capabilities</li>
<li>JSON works naturally with Python dictionaries</li>
<li>Conversion between formats is simple with the right tools</li>
</ul>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ul>
<li><a href="https://docs.python.org/3/library/csv.html"><strong>Python csv module documentation</strong></a> - Official documentation for Python’s built-in CSV handling capabilities</li>
<li><a href="https://docs.python.org/3/library/json.html"><strong>Python json module documentation</strong></a> - Official guide for working with JSON data in Python</li>
<li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html"><strong>pandas read_csv documentation</strong></a> - Complete reference for reading CSV files into DataFrames</li>
<li><a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html"><strong>pandas to_csv documentation</strong></a> - Detailed guide for writing DataFrames to CSV files</li>
</ul>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-10-01/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>CSV and JSON with Python</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-10-01/</guid>
  <pubDate>Wed, 01 Oct 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Data Imputation and Scaling with healthyR.ai: A Guide for R Programmers</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-29/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p>This guide covers some data preprocessing techniques using healthyR.ai, focusing on imputation and scaling functions with clear syntax examples and best practices.</p>
</blockquote>
<section id="introduction" class="level1">
<h1>Introduction</h1>
<p>Data preprocessing is a necessary step in any machine learning workflow. The healthyR.ai package offers user-friendly functions for imputation (filling missing values) and scaling (normalizing data) that integrate seamlessly with the tidymodels ecosystem in R. This guide explains the syntax and implementation of these functions in straightforward terms.</p>
</section>
<section id="data-imputation-with-hai_data_impute" class="level1">
<h1>Data Imputation with hai_data_impute()</h1>
<p>Imputation replaces missing values in your dataset. The <code>hai_data_impute()</code> function supports multiple imputation methods through a consistent interface.</p>
<section id="basic-syntax" class="level2">
<h2 class="anchored" data-anchor-id="basic-syntax">Basic Syntax</h2>
<div class="sourceCode" 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;">hai_data_impute</span>(</span>
<span id="cb1-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recipe_object =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb1-3">  ...,</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.type_of_imputation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,</span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.seed_value =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>,</span>
<span id="cb1-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Additional parameters based on method</span></span>
<span id="cb1-7">)</span></code></pre></div>
<p>Key arguments:</p>
<ul>
<li><code>.recipe_object</code>: Recipe object containing your data</li>
<li><code>...</code>: Variables to impute (using selector functions)</li>
<li><code>.type_of_imputation</code>: Method for imputation</li>
<li>Method-specific parameters (e.g., <code>.neighbors</code> for KNN)</li>
</ul>
</section>
<section id="supported-imputation-methods" class="level2">
<h2 class="anchored" data-anchor-id="supported-imputation-methods">Supported Imputation Methods</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 10%">
<col style="width: 37%">
<col style="width: 30%">
<col style="width: 22%">
</colgroup>
<thead>
<tr class="header">
<th>Method</th>
<th>Description</th>
<th>Best For</th>
<th>Key Parameters</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>“mean”</td>
<td>Replace with column mean</td>
<td>Normal distributions</td>
<td><code>.mean_trim</code></td>
</tr>
<tr class="even">
<td>“median”</td>
<td>Replace with column median</td>
<td>Skewed data, outliers present</td>
<td>None</td>
</tr>
<tr class="odd">
<td>“mode”</td>
<td>Replace with most frequent value</td>
<td>Categorical variables</td>
<td>None</td>
</tr>
<tr class="even">
<td>“knn”</td>
<td>K-nearest neighbors imputation</td>
<td>Complex relationships</td>
<td><code>.neighbors</code> (default: 5)</td>
</tr>
<tr class="odd">
<td>“bagged”</td>
<td>Bagged tree imputation</td>
<td>Non-linear patterns</td>
<td><code>.number_of_trees</code> (default: 25)</td>
</tr>
<tr class="even">
<td>“roll”</td>
<td>Rolling window statistic</td>
<td>Time series data</td>
<td><code>.roll_window</code>, <code>.roll_statistic</code></td>
</tr>
</tbody>
</table>
</section>
<section id="example-rolling-median-imputation" class="level2">
<h2 class="anchored" data-anchor-id="example-rolling-median-imputation">Example: Rolling Median Imputation</h2>
<div class="sourceCode" 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>(healthyR.ai)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(recipes)</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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create recipe object</span></span>
<span id="cb2-6">rec_obj <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;">recipe</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., df_tbl)</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply rolling median imputation</span></span>
<span id="cb2-9">imputed_data <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;">hai_data_impute</span>(</span>
<span id="cb2-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recipe_object =</span> rec_obj,</span>
<span id="cb2-11">  value,</span>
<span id="cb2-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.type_of_imputation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roll"</span>,</span>
<span id="cb2-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.roll_statistic =</span> median</span>
<span id="cb2-14">)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>impute_rec_obj <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_juiced_data</span>()</span></code></pre></div>
</section>
</section>
<section id="data-scaling-with-hai_data_scale" class="level1">
<h1>Data Scaling with hai_data_scale()</h1>
<p>Scaling transforms variables to a common scale, which is important for many machine learning algorithms.</p>
<section id="basic-syntax-1" class="level2">
<h2 class="anchored" data-anchor-id="basic-syntax-1">Basic Syntax</h2>
<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hai_data_scale</span>(</span>
<span id="cb3-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recipe_object =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb3-3">  ...,</span>
<span id="cb3-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.type_of_scale =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,</span>
<span id="cb3-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.range_min =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>,</span>
<span id="cb3-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.range_max =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb3-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.scale_factor =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb3-8">)</span></code></pre></div>
<p>Key arguments:</p>
<ul>
<li><code>.recipe_object</code>: Recipe object containing your data</li>
<li><code>...</code>: Variables to scale (using selector functions)</li>
<li><code>.type_of_scale</code>: Method for scaling</li>
<li><code>.range_min</code>, <code>.range_max</code>: Range bounds (for “range” method)</li>
<li><code>.scale_factor</code>: Scale by 1 or 2 standard deviations (for interpretability)</li>
</ul>
</section>
<section id="supported-scaling-methods" class="level2">
<h2 class="anchored" data-anchor-id="supported-scaling-methods">Supported Scaling Methods</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 14%">
<col style="width: 32%">
<col style="width: 21%">
<col style="width: 31%">
</colgroup>
<thead>
<tr class="header">
<th>Method</th>
<th>Description</th>
<th>Formula</th>
<th>Result Range</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>“center”</td>
<td>Subtract mean</td>
<td>x - mean(x)</td>
<td>Mean = 0, original variance</td>
</tr>
<tr class="even">
<td>“scale”</td>
<td>Divide by standard deviation</td>
<td>x / sd(x)</td>
<td>Standard deviation = 1</td>
</tr>
<tr class="odd">
<td>“normalize”</td>
<td>Scale to unit norm</td>
<td>x / ||x||</td>
<td>Vector length = 1</td>
</tr>
<tr class="even">
<td>“range”</td>
<td>Min-max scaling</td>
<td>(x-min)/(max-min)</td>
<td>[range_min, range_max]</td>
</tr>
</tbody>
</table>
</section>
<section id="example-standardization" class="level2">
<h2 class="anchored" data-anchor-id="example-standardization">Example: Standardization</h2>
<div class="sourceCode" 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;">library</span>(healthyR.ai)</span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(recipes)</span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create recipe object</span></span>
<span id="cb4-6">rec_obj <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;">recipe</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., df_tbl)</span>
<span id="cb4-7"></span>
<span id="cb4-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply standardization (z-score)</span></span>
<span id="cb4-9">scaled_data <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;">hai_data_scale</span>(</span>
<span id="cb4-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recipe_object =</span> rec_obj,</span>
<span id="cb4-11">  value,</span>
<span id="cb4-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.type_of_scale =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"scale"</span></span>
<span id="cb4-13">)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>scale_rec_obj <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_juiced_data</span>()</span></code></pre></div>
</section>
</section>
<section id="combining-imputation-and-scaling" class="level1">
<h1>Combining Imputation and Scaling</h1>
<p>A typical preprocessing workflow combines both steps:</p>
<div class="sourceCode" 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;"># Create recipe</span></span>
<span id="cb5-2">rec_obj <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;">recipe</span>(target <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> ., data_df)</span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Impute missing values</span></span>
<span id="cb5-5">imputed <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;">hai_data_impute</span>(</span>
<span id="cb5-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recipe_object =</span> rec_obj,</span>
<span id="cb5-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_numeric</span>(),</span>
<span id="cb5-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.type_of_imputation =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median"</span></span>
<span id="cb5-9">)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>impute_rec_obj</span>
<span id="cb5-10"></span>
<span id="cb5-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Scale the imputed data</span></span>
<span id="cb5-12">final_data <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;">hai_data_scale</span>(</span>
<span id="cb5-13">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recipe_object =</span> imputed,</span>
<span id="cb5-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_numeric</span>(),</span>
<span id="cb5-15">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.type_of_scale =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"range"</span></span>
<span id="cb5-16">)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>scale_rec_obj <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb5-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">get_juiced_data</span>()</span></code></pre></div>
</section>
<section id="exampls" class="level1">
<h1>Exampls</h1>
<p>I think things work best when you can see an example in action.</p>
<section id="imputation" class="level2">
<h2 class="anchored" data-anchor-id="imputation">Imputation</h2>
<div class="cell">
<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;">library</span>(healthyR.ai)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(recipes)</span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb6-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(purrr)</span>
<span id="cb6-6"></span>
<span id="cb6-7">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;">10</span>L</span>
<span id="cb6-8">l <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>L</span>
<span id="cb6-9">lo <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> l</span>
<span id="cb6-10"></span>
<span id="cb6-11">date_seq <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;">seq.Date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2013-01-01"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> lo, <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;">"month"</span>)</span>
<span id="cb6-12">date_seq</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01"
 [6] "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01"
[11] "2013-11-01" "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01"
[16] "2014-04-01" "2014-05-01" "2014-06-01" "2014-07-01" "2014-08-01"
[21] "2014-09-01" "2014-10-01" "2014-11-01" "2014-12-01" "2015-01-01"
[26] "2015-02-01" "2015-03-01" "2015-04-01" "2015-05-01" "2015-06-01"
[31] "2015-07-01" "2015-08-01" "2015-09-01" "2015-10-01" "2015-11-01"
[36] "2015-12-01" "2016-01-01" "2016-02-01" "2016-03-01" "2016-04-01"
[41] "2016-05-01" "2016-06-01" "2016-07-01" "2016-08-01" "2016-09-01"
[46] "2016-10-01" "2016-11-01" "2016-12-01" "2017-01-01" "2017-02-01"</code></pre>
</div>
<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">val_seq <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;">replicate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</span> l, <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;">runif</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>), <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</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;">as.vector</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;">as.double</span>()</span>
<span id="cb8-2">val_seq</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.74815520 0.62345014 0.98719405 0.98357823 0.64343460 0.38288945
 [7] 0.30782868 0.63132596 0.09734484         NA 0.79572696 0.08743225
[13] 0.72841099 0.78703884 0.39553790 0.54639674 0.96807028 0.60125354
[19] 0.74665373         NA 0.92237646 0.04457192 0.68444841 0.05388607
[25] 0.24374963 0.73552094 0.84926348 0.55056715 0.77699405         NA
[31] 0.55460139 0.24564446 0.24396533 0.60797386 0.71226179 0.93048958
[37] 0.72179306 0.01549613 0.88487496         NA 0.41888816 0.08623630
[43] 0.06213051 0.58266383 0.72425739 0.17659346 0.80285097 0.78684451
[49] 0.68433082         NA</code></pre>
</div>
<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">data_tbl <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="cb10-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_col =</span> date_seq,</span>
<span id="cb10-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> val_seq</span>
<span id="cb10-4">)</span>
<span id="cb10-5"></span>
<span id="cb10-6">rec_obj <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;">recipe</span>(value <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> date_col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data_tbl)</span>
<span id="cb10-7">rec_obj</span>
<span id="cb10-8"></span>
<span id="cb10-9">df_tbl <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="cb10-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">impute_type =</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;">"bagged"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"knn"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"linear"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"mean"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"median"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"roll"</span>),</span>
<span id="cb10-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rec_obj =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(rec_obj),</span>
<span id="cb10-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(data_tbl)</span>
<span id="cb10-13">)</span>
<span id="cb10-14">df_tbl[<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;">3</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 50 × 2
   date_col     value
   &lt;date&gt;       &lt;dbl&gt;
 1 2013-01-01  0.748 
 2 2013-02-01  0.623 
 3 2013-03-01  0.987 
 4 2013-04-01  0.984 
 5 2013-05-01  0.643 
 6 2013-06-01  0.383 
 7 2013-07-01  0.308 
 8 2013-08-01  0.631 
 9 2013-09-01  0.0973
10 2013-10-01 NA     
# ℹ 40 more rows</code></pre>
</div>
<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">data_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_split</span>(impute_type)</span>
<span id="cb12-3"></span>
<span id="cb12-4">data_impute_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_list <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">imap</span>(</span>
<span id="cb12-6">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.f =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(obj, id){</span>
<span id="cb12-7">      imp_type <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> obj <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;">pull</span>(impute_type)</span>
<span id="cb12-8">      rec_obj <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> obj <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;">pull</span>(rec_obj) <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;">pluck</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb12-9">      data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> obj[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>]][[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]]</span>
<span id="cb12-10">      </span>
<span id="cb12-11">      imp_obj <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;">hai_data_impute</span>(</span>
<span id="cb12-12">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.recipe_object =</span> rec_obj,</span>
<span id="cb12-13">        value,</span>
<span id="cb12-14">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.type_of_imputation =</span> imp_type,</span>
<span id="cb12-15">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.roll_statistic =</span> median</span>
<span id="cb12-16">      )<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>impute_rec_obj</span>
<span id="cb12-17"></span>
<span id="cb12-18">      imputed_data <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;">get_juiced_data</span>(imp_obj)</span>
<span id="cb12-19"></span>
<span id="cb12-20">      combined_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-21">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(imputed_data, <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;">"date_col"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-22">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">setNames</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;">"date_col"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"original_value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"imputed_value"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-23">        <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;">rec_no =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-24">        <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;">color_col =</span> original_value,</span>
<span id="cb12-25">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size_col =</span> original_value) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-26">        <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;">impute_type =</span> imp_type)</span>
<span id="cb12-27">      </span>
<span id="cb12-28">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(combined_tbl)</span>
<span id="cb12-29">    }</span>
<span id="cb12-30">  )</span>
<span id="cb12-31"></span>
<span id="cb12-32">combined_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> data_impute_list <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list_rbind</span>()</span>
<span id="cb12-34"></span>
<span id="cb12-35">imped_na_vals_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(original_value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb12-38">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_imputed_val =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(imputed_value),</span>
<span id="cb12-39">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> impute_type</span>
<span id="cb12-40">  )</span>
<span id="cb12-41"></span>
<span id="cb12-42">combined_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(</span>
<span id="cb12-44">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_imputed_val_col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(imputed_value),</span>
<span id="cb12-45">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg_original_val_col =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(original_value, <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="cb12-46">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.by =</span> impute_type</span>
<span id="cb12-47">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-48"><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;">imputation_diff =</span> avg_imputed_val_col <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> avg_original_val_col) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb12-49"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">left_join</span>(imped_na_vals_tbl, <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;">"impute_type"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 5
  impute_type avg_imputed_val_col avg_original_val_col imputation_diff
  &lt;chr&gt;                     &lt;dbl&gt;                &lt;dbl&gt;           &lt;dbl&gt;
1 bagged                    0.556                0.559        -0.00287
2 knn                       0.557                0.559        -0.00199
3 linear                    0.558                0.559        -0.00128
4 mean                      0.559                0.559         0      
5 median                    0.566                0.559         0.00721
6 roll                      0.555                0.559        -0.00434
# ℹ 1 more variable: avg_imputed_val &lt;dbl&gt;</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> combined_tbl,</span>
<span id="cb14-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(</span>
<span id="cb14-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date_col,</span>
<span id="cb14-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> imputed_value,</span>
<span id="cb14-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> color_col</span>
<span id="cb14-6">    )</span>
<span id="cb14-7">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> </span>
<span id="cb14-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> impute_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> combined_tbl <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(original_value)), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">shape =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'NA'</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</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></span>
<span id="cb14-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_shape_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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'NA'</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="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date_col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> original_value), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_line</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> date_col, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> imputed_value), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"red"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">328</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_vline</span>(</span>
<span id="cb14-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> combined_tbl[combined_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>original_value <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;">is.na</span>(), ], </span>
<span id="cb14-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xintercept =</span> date_col), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">linetype =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dashed"</span></span>
<span id="cb14-16">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb14-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;">"Date"</span>,</span>
<span id="cb14-19">    <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;">"Value"</span>,</span>
<span id="cb14-20">    <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;">"Original vs. Imputed Data using HealthyR.ai"</span>,</span>
<span id="cb14-21">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Function: hai_data_impute()"</span>,</span>
<span id="cb14-22">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">caption =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Red line is the imputed data, blue line is the original data"</span></span>
<span id="cb14-23">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-24">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb14-25">  <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;">"none"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-29/index_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1">combined_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(original_value)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> impute_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> imputed_value, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> impute_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> impute_type)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_boxplot</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb15-6">    <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;">"Date"</span>,</span>
<span id="cb15-7">    <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;">"Value"</span>,</span>
<span id="cb15-8">    <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;">"Original vs. Imputed Data using HealthyR.ai"</span>,</span>
<span id="cb15-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Function: hai_data_impute()"</span></span>
<span id="cb15-10">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb15-12">  <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;">"none"</span>)</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-29/index_files/figure-html/unnamed-chunk-1-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="choosing-the-right-method" class="level1">
<h1>Choosing the Right Method</h1>
<p>For imputation:</p>
<ul>
<li><strong>Continuous normal data</strong>: Use “mean”</li>
<li><strong>Skewed data or outliers</strong>: Use “median”</li>
<li><strong>Categorical data</strong>: Use “mode”</li>
<li><strong>Time series</strong>: Use “roll” with appropriate window</li>
<li><strong>Complex relationships</strong>: Try “knn” or “bagged”</li>
</ul>
<p>For scaling:</p>
<ul>
<li><strong>Linear regression</strong>: Use “center” or “scale”</li>
<li><strong>Distance-based algorithms</strong> (KNN, SVM): Use “scale”</li>
<li><strong>Neural networks</strong>: Use “range” [0,1] or “normalize”</li>
</ul>
</section>
<section id="best-practices" class="level1">
<h1>Best Practices</h1>
<ul>
<li>Always load required libraries: <code>healthyR.ai</code>, <code>recipes</code>, <code>dplyr</code></li>
<li>Create recipe object first: <code>recipe(target ~ ., data = df)</code></li>
<li>Set <code>.seed_value</code> for reproducible results with stochastic methods</li>
<li>Extract processed data with <code>get_juiced_data()</code> function</li>
<li>Verify results with <code>summary()</code> after processing</li>
<li>Choose imputation method based on data characteristics and missingness pattern</li>
</ul>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<ul>
<li>healthyR.ai provides user-friendly wrappers around recipes functions for data preprocessing</li>
<li>Both imputation and scaling require a recipe object</li>
<li>Functions return a list containing the processed recipe object</li>
<li>Choose methods based on your data type and the requirements of your modeling approach</li>
<li>Chain operations (imputation → scaling → modeling) for a complete workflow</li>
</ul>
<p>The goal of these healthyR.ai functions is to simplify data preprocessing with a consistent syntax and integration with the tidymodels ecosystem, making it an excellent choice for streamlining your machine learning pipeline.</p>
<hr>
<p>Happy Coding! 🚀</p>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-29/</guid>
  <pubDate>Mon, 29 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Working with PDF and Word Documents in Python</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-24/</link>
  <description><![CDATA[ 






<p><em>Author’s Note: As I write this series, I’m learning these concepts alongside you! If you spot any mistakes or have suggestions for improvement, please let me know. Programming is a continuous learning process, and I’m always looking to make these tutorials clearer and more helpful for fellow beginners.</em></p>
<blockquote class="blockquote">
<p><strong>Key Insight:</strong> Python makes document automation incredibly easy with just a few lines of code. Perfect for beginners just like me who want to automate boring tasks!</p>
</blockquote>
<p><strong>Working with PDF and Word documents in Python</strong> opens up a world of automation possibilities for beginner programmers. Whether you need to extract text from dozens of PDF files (like I need t) or automatically generate Word reports, Python provides simple, powerful tools to handle these tasks efficiently.</p>
<section id="what-can-you-accomplish" class="level1">
<h1>What Can You Accomplish?</h1>
<p>With Python’s document manipulation capabilities, you can:</p>
<ul>
<li>📄 <strong>Extract text</strong> from PDF files (like meeting minutes or reports)<br>
</li>
<li>🔗 <strong>Combine multiple PDF files</strong> into one document<br>
</li>
<li>🔄 <strong>Rotate pages</strong> that were scanned sideways<br>
</li>
<li>📝 <strong>Create new Word documents</strong> automatically<br>
</li>
<li>✏️ <strong>Read and modify</strong> existing Word files<br>
</li>
<li>🔒 <strong>Handle password-protected PDFs</strong></li>
</ul>
</section>
<section id="getting-started-installing-the-libraries" class="level1">
<h1>Getting Started: Installing the Libraries</h1>
<p>Before diving into code, you’ll need to install two essential libraries. Open your command prompt or terminal and type:</p>
<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="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install PyPDF2==1.26.0</span>
<span id="cb1-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install python-docx</span></code></pre></div>
</section>
<section id="working-with-pdf-files-using-pypdf2" class="level1">
<h1>Working with PDF Files Using PyPDF2</h1>
<section id="reading-text-from-a-pdf" class="level2">
<h2 class="anchored" data-anchor-id="reading-text-from-a-pdf">Reading Text from a PDF</h2>
<p>Let’s start with the most common task - extracting text from a PDF :</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> PyPDF2</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get current working directory</span></span>
<span id="cb2-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Current Working Directory:"</span>, os.getcwd())</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Open the PDF file in binary read mode</span></span>
<span id="cb2-8">pdfFileObj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'some_pdf.pdf'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rb'</span>)</span>
<span id="cb2-9">pdfReader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PyPDF2.PdfFileReader(pdfFileObj)</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the first page (Python counts from 0)</span></span>
<span id="cb2-12">pageObj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pdfReader.getPage(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract the text</span></span>
<span id="cb2-15">text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pageObj.extractText()</span>
<span id="cb2-16"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(text)</span>
<span id="cb2-17"></span>
<span id="cb2-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Always close the file</span></span>
<span id="cb2-19">pdfFileObj.close()</span></code></pre></div>
<p><strong>What’s happening here?</strong></p>
<ul>
<li><code>'rb'</code> means “read binary” - PDFs aren’t text files, so we need binary mode</li>
<li><code>getPage(0)</code> gets the first page (remember, Python starts counting at 0)</li>
<li><code>extractText()</code> pulls out all the text from that page as a string</li>
</ul>
</section>
<section id="checking-pdf-information" class="level2">
<h2 class="anchored" data-anchor-id="checking-pdf-information">Checking PDF Information</h2>
<p>Want to know how many pages are in a PDF? Here’s how:</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> PyPDF2</span>
<span id="cb3-2"></span>
<span id="cb3-3">pdfFileObj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'some_pdf.pdf'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rb'</span>)</span>
<span id="cb3-4">pdfReader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PyPDF2.PdfFileReader(pdfFileObj)</span>
<span id="cb3-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Number of pages: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>pdfReader<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>numPages<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-6">pdfFileObj.close()</span></code></pre></div>
</section>
<section id="combining-multiple-pdfs" class="level2">
<h2 class="anchored" data-anchor-id="combining-multiple-pdfs">Combining Multiple PDFs</h2>
<p>Need to merge several PDF files? This is perfect for combining monthly reports :</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> PyPDF2</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Open both PDF files</span></span>
<span id="cb4-4">pdf1File <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'january_report.pdf'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rb'</span>)</span>
<span id="cb4-5">pdf2File <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'february_report.pdf'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'rb'</span>)</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create readers for each file</span></span>
<span id="cb4-8">pdf1Reader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PyPDF2.PdfFileReader(pdf1File)</span>
<span id="cb4-9">pdf2Reader <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PyPDF2.PdfFileReader(pdf2File)</span>
<span id="cb4-10"></span>
<span id="cb4-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a writer to build the new PDF</span></span>
<span id="cb4-12">pdfWriter <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PyPDF2.PdfFileWriter()</span>
<span id="cb4-13"></span>
<span id="cb4-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add all pages from first PDF</span></span>
<span id="cb4-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pageNum <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(pdf1Reader.numPages):</span>
<span id="cb4-16">    pageObj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pdf1Reader.getPage(pageNum)</span>
<span id="cb4-17">    pdfWriter.addPage(pageObj)</span>
<span id="cb4-18"></span>
<span id="cb4-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add all pages from second PDF</span></span>
<span id="cb4-20"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> pageNum <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(pdf2Reader.numPages):</span>
<span id="cb4-21">    pageObj <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pdf2Reader.getPage(pageNum)</span>
<span id="cb4-22">    pdfWriter.addPage(pageObj)</span>
<span id="cb4-23"></span>
<span id="cb4-24"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the combined PDF</span></span>
<span id="cb4-25">pdfOutputFile <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'combined_reports.pdf'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'wb'</span>)</span>
<span id="cb4-26">pdfWriter.write(pdfOutputFile)</span>
<span id="cb4-27">pdfOutputFile.close()</span>
<span id="cb4-28">pdf1File.close()</span>
<span id="cb4-29">pdf2File.close()</span></code></pre></div>
</section>
</section>
<section id="working-with-word-documents-using-python-docx" class="level1">
<h1>Working with Word Documents Using python-docx</h1>
<section id="creating-a-new-word-document" class="level2">
<h2 class="anchored" data-anchor-id="creating-a-new-word-document">Creating a New Word Document</h2>
<p>Creating Word documents from scratch is surprisingly simple :</p>
<div id="0336cf73" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> docx</span>
<span id="cb5-2"></span>
<span id="cb5-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a new document</span></span>
<span id="cb5-4">document <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> docx.Document()</span>
<span id="cb5-5"></span>
<span id="cb5-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a heading</span></span>
<span id="cb5-7">document.add_heading(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'My Automated Report'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a paragraph</span></span>
<span id="cb5-10">document.add_paragraph(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'This document was created automatically with Python!'</span>)</span>
<span id="cb5-11"></span>
<span id="cb5-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the document</span></span>
<span id="cb5-13">document.save(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:/Users/ssanders/Documents/GitHub/steveondata/posts/2025-09-24/my_report</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">docx'</span>)</span></code></pre></div>
</div>
<p><strong>What’s happening here?</strong></p>
<ul>
<li><code>Document()</code> creates a blank Word document</li>
<li><code>add_heading('text', 0)</code> adds a title (0 is the biggest heading size)</li>
<li><code>add_paragraph('text')</code> adds regular text</li>
<li><code>save('filename.docx')</code> writes the document to your computer</li>
</ul>
</section>
<section id="reading-an-existing-word-document" class="level2">
<h2 class="anchored" data-anchor-id="reading-an-existing-word-document">Reading an Existing Word Document</h2>
<p>Need to extract text from a Word document? Here’s how :</p>
<div id="0389738c" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> docx</span>
<span id="cb6-2"></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Open an existing document</span></span>
<span id="cb6-4">doc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> docx.Document(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:/Users/ssanders/Documents/GitHub/steveondata/posts/2025-09-24/my_report</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">docx'</span>)</span>
<span id="cb6-5"></span>
<span id="cb6-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Print each paragraph</span></span>
<span id="cb6-7"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> para <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> doc.paragraphs:</span>
<span id="cb6-8">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(para.text)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>My Automated Report
This document was created automatically with Python!</code></pre>
</div>
</div>
<p>This code opens a Word document and prints out each paragraph, one by one.</p>
</section>
<section id="adding-content-to-an-existing-document" class="level2">
<h2 class="anchored" data-anchor-id="adding-content-to-an-existing-document">Adding Content to an Existing Document</h2>
<p>You can also modify existing documents:</p>
<div id="a6c3404e" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> docx <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Document</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Open the template</span></span>
<span id="cb8-4">doc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Document(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:/Users/ssanders/Documents/GitHub/steveondata/posts/2025-09-24/my_report</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">docx'</span>)</span>
<span id="cb8-5"></span>
<span id="cb8-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add new content</span></span>
<span id="cb8-7">doc.add_paragraph(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'New agenda item added automatically.'</span>)</span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save with a new name</span></span>
<span id="cb8-10">doc.save(<span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">r'C:/Users/ssanders/Documents/GitHub/steveondata/posts/2025-09-24/updated_my_report</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">.</span><span class="vs" style="color: #20794D;
background-color: null;
font-style: inherit;">docx'</span>)</span></code></pre></div>
</div>
</section>
</section>
<section id="practical-real-world-applications" class="level1">
<h1>Practical Real-World Applications</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 35%">
<col style="width: 32%">
<col style="width: 32%">
</colgroup>
<thead>
<tr class="header">
<th>Use Case</th>
<th>Example</th>
<th>Benefit</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Students</strong></td>
<td>Extract text from research PDFs for note-taking</td>
<td>Save hours of manual copying</td>
</tr>
<tr class="even">
<td><strong>Office Workers</strong></td>
<td>Combine weekly reports into monthly summaries</td>
<td>Eliminate repetitive tasks</td>
</tr>
<tr class="odd">
<td><strong>Small Businesses</strong></td>
<td>Automatically generate invoices or contracts</td>
<td>Reduce manual errors</td>
</tr>
<tr class="even">
<td><strong>Researchers</strong></td>
<td>Process large collections of documents</td>
<td>Analyze data at scale</td>
</tr>
</tbody>
</table>
</section>
<section id="your-turn-practice-exercise" class="level1">
<h1>Your Turn! Practice Exercise</h1>
<p><strong>Challenge:</strong> Create a Python script that:</p>
<ol type="1">
<li>Opens a Word document</li>
<li>Adds today’s date as a heading</li>
<li>Adds a paragraph with your name</li>
<li>Saves it as a new file</li>
</ol>
<details>
<summary>
Click here for Solution!
</summary>
<div id="1f51b181" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> docx <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Document</span>
<span id="cb9-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> datetime <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> date</span>
<span id="cb9-3"></span>
<span id="cb9-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create new document</span></span>
<span id="cb9-5">doc <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Document()</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add today's date as heading</span></span>
<span id="cb9-8">today <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> date.today()</span>
<span id="cb9-9">doc.add_heading(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'Report for </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>today<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)</span>
<span id="cb9-10"></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add paragraph with name</span></span>
<span id="cb9-12">doc.add_paragraph(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Created by: [Your Name Here]'</span>)</span>
<span id="cb9-13"></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the document</span></span>
<span id="cb9-15">doc.save(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'C:/Users/ssanders/Documents/GitHub/steveondata/posts/2025-09-24/daily_report_</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>today<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">.docx'</span>)</span>
<span id="cb9-16"></span>
<span id="cb9-17"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Document created successfully!"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Document created successfully!</code></pre>
</div>
</div>
</details>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<p><strong>Quick Reference Points:</strong></p>
<ul>
<li>Use <strong>PyPDF2</strong> for reading and manipulating PDF files</li>
<li>Use <strong>python-docx</strong> for creating and editing Word documents</li>
<li>Always open PDFs in <strong>binary mode</strong> (<code>'rb'</code>)</li>
<li>Remember Python uses <strong>zero-based indexing</strong> (first page is 0)</li>
<li><strong>Close files</strong> after opening them to free up memory</li>
<li>These tools are perfect for <strong>automating repetitive tasks</strong></li>
</ul>
</section>
<section id="whats-next" class="level1">
<h1>What’s Next?</h1>
<p>Try these examples with your own PDF and Word files. Start small - maybe extract text from a single PDF or create a simple Word document. Once you’re comfortable, you can combine these techniques to build powerful document automation tools.</p>
<p>The beauty of <strong>working with PDF and Word documents in Python</strong> is that once you master these basics, you can automate almost any document-related task!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ul>
<li><a href="https://automatetheboringstuff.com/2e/chapter15/">Automate the Boring Stuff with Python, Chapter 15: Working with PDF and Word Documents</a></li>
<li><a href="https://python-docx.readthedocs.io/en/latest/">python-docx Official Documentation</a></li>
<li><a href="https://pypdf2.readthedocs.io/">PyPDF2 Documentation</a></li>
<li><a href="https://automatetheboringstuff.com/">Automate the Boring Stuff with Python - Main Site</a></li>
<li><a href="https://automatetheboringstuff.com/2e/appendixa/">Installing Third-Party Modules Guide</a></li>
</ul>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-24/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Read PDFs and Word Docs with Python</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-24/</guid>
  <pubDate>Wed, 24 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Find the Max Value in Each Row in R</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-22/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Insight:</strong> Finding the maximum value in each row is a common data analysis task, in R it’s simple. The <code>apply()</code> function with <code>max()</code> is the most straightforward method, but several alternatives offer better performance or integration with modern R workflows.</p>
</blockquote>
<p>Finding the <strong>max value in each row</strong> is a useful operation in data analysis. Whether you’re analyzing exam scores, stock prices, or sensor measurements, knowing how to efficiently extract row-wise maximums can save you time and improve your data processing workflows. This guide covers four proven methods using <code>apply()</code>, <code>pmax()</code>, <code>dplyr</code>, and <code>matrixStats</code> packages.</p>
<hr>
<section id="understanding-row-wise-operations-in-r" class="level1">
<h1>Understanding Row-Wise Operations in R</h1>
<p>Row-wise operations in R work across the columns of each row, rather than down the columns. When we want the <strong>max value in each row</strong>, we’re looking for the highest value across all columns for every single row in our dataset.</p>
<p><strong>Basic Concept:</strong></p>
<ul>
<li><strong>Column-wise:</strong> Operations down each column (like finding the mean of each column)</li>
<li><strong>Row-wise:</strong> Operations across columns for each row (like finding the max of each row)</li>
</ul>
</section>
<section id="method-1-using-apply---the-most-common-approach" class="level1">
<h1>Method 1: Using <code>apply()</code> - The Most Common Approach</h1>
<p>The <code>apply()</code> function is the <strong>most popular method</strong> for finding row maximums in R. It’s part of base R, so no additional packages are required.</p>
<section id="basic-syntax" class="level2">
<h2 class="anchored" data-anchor-id="basic-syntax">Basic Syntax</h2>
<div class="sourceCode" 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;">apply</span>(X, MARGIN, FUN, ...)</span></code></pre></div>
<p><strong>Parameters:</strong></p>
<ul>
<li><code>X</code>: Your data frame or matrix</li>
<li><code>MARGIN</code>: Use <code>1</code> for rows, <code>2</code> for columns<br>
</li>
<li><code>FUN</code>: The function to apply (in our case, <code>max</code>)</li>
<li><code>...</code>: Additional arguments (like <code>na.rm = TRUE</code>)</li>
</ul>
</section>
<section id="simple-example" class="level2">
<h2 class="anchored" data-anchor-id="simple-example">Simple Example</h2>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create sample data</span></span>
<span id="cb2-2">df <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-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</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;">4</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;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb2-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb2-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">C =</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;">6</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;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb2-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">D =</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;">2</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;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb2-7">)</span>
<span id="cb2-8"></span>
<span id="cb2-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find max in each row</span></span>
<span id="cb2-10">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_value <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;">apply</span>(df, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max)</span>
<span id="cb2-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  A B C D max_value
1 1 2 3 5         5
2 4 5 6 2         6
3 7 8 9 1         9
4 2 6 4 8         8
5 9 3 1 7         9</code></pre>
</div>
</div>
</section>
<section id="handling-missing-values" class="level2">
<h2 class="anchored" data-anchor-id="handling-missing-values">Handling Missing Values</h2>
<div class="cell">
<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;"># Data with missing values</span></span>
<span id="cb4-2">df_na <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="cb4-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">A =</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;">4</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;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>),</span>
<span id="cb4-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">B =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</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;">3</span>),</span>
<span id="cb4-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">C =</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;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</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>
<span id="cb4-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">D =</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;">2</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;">8</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>)</span>
<span id="cb4-7">)</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use na.rm = TRUE to ignore missing values</span></span>
<span id="cb4-10">df_na<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_value <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;">apply</span>(df_na, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max, <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="cb4-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(df_na)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   A  B  C D max_value
1  1  2  3 5         5
2  4 NA  6 2         6
3 NA  8  9 1         9
4  2  6 NA 8         8
5  9  3  1 7         9</code></pre>
</div>
</div>
</section>
</section>
<section id="method-2-using-pmax---fastest-for-few-columns" class="level1">
<h1>Method 2: Using <code>pmax()</code> - Fastest for Few Columns</h1>
<p>The <code>pmax()</code> function computes <strong>parallel maximums</strong>, making it excellent for datasets with a small number of columns .</p>
<section id="basic-usage" class="level2">
<h2 class="anchored" data-anchor-id="basic-usage">Basic Usage</h2>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using pmax for the same data</span></span>
<span id="cb6-2">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_pmax <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;">pmax</span>(df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>A, df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>B, df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>C, df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>D)</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Alternative with do.call</span></span>
<span id="cb6-5">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_pmax2 <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>(pmax, df[<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>
<span id="cb6-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  A B C D max_value max_pmax max_pmax2
1 1 2 3 5         5        5         5
2 4 5 6 2         6        6         6
3 7 8 9 1         9        9         9
4 2 6 4 8         8        8         8
5 9 3 1 7         9        9         9</code></pre>
</div>
</div>
<p><strong>Advantages:</strong></p>
<ul>
<li>Very fast for datasets with few columns</li>
<li>Works well with missing values using <code>na.rm = TRUE</code></li>
<li>Part of base R</li>
</ul>
</section>
</section>
<section id="method-3-using-dplyr---tidyverse-approach" class="level1">
<h1>Method 3: Using <code>dplyr</code> - Tidyverse Approach</h1>
<p>For those using the <strong>tidyverse</strong>, <code>dplyr</code> offers modern, readable approaches to row-wise operations .</p>
<div class="cell">
<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;">library</span>(dplyr)</span>
<span id="cb8-2"></span>
<span id="cb8-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 3a: Using rowwise() and c_across()</span></span>
<span id="cb8-4">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowwise</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-6">  <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;">max_tidy =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c_across</span>(A<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:</span>D))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>()</span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 3b: Using pmax within mutate</span></span>
<span id="cb8-10">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb8-11">  <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;">max_pmax_tidy =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmax</span>(A, B, C, D))</span>
<span id="cb8-12"></span>
<span id="cb8-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 × 9
      A     B     C     D max_value max_pmax max_pmax2 max_tidy max_pmax_tidy
  &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;         &lt;dbl&gt;
1     1     2     3     5         5        5         5        5             5
2     4     5     6     2         6        6         6        6             6
3     7     8     9     1         9        9         9        9             9
4     2     6     4     8         8        8         8        8             8
5     9     3     1     7         9        9         9        9             9</code></pre>
</div>
</div>
</section>
<section id="method-4-using-matrixstats---best-for-large-datasets" class="level1">
<h1>Method 4: Using <code>matrixStats</code> - Best for Large Datasets</h1>
<p>For <strong>large datasets</strong>, the <code>matrixStats</code> package provides optimized functions that significantly outperform base R alternatives .</p>
<div class="cell">
<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>(matrixStats)</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to matrix and use rowMaxs</span></span>
<span id="cb10-4">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>max_matrixstats <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;">rowMaxs</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.matrix</span>(df[<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>
<span id="cb10-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 5
Columns: 10
$ A               &lt;dbl&gt; 1, 4, 7, 2, 9
$ B               &lt;dbl&gt; 2, 5, 8, 6, 3
$ C               &lt;dbl&gt; 3, 6, 9, 4, 1
$ D               &lt;dbl&gt; 5, 2, 1, 8, 7
$ max_value       &lt;dbl&gt; 5, 6, 9, 8, 9
$ max_pmax        &lt;dbl&gt; 5, 6, 9, 8, 9
$ max_pmax2       &lt;dbl&gt; 5, 6, 9, 8, 9
$ max_tidy        &lt;dbl&gt; 5, 6, 9, 8, 9
$ max_pmax_tidy   &lt;dbl&gt; 5, 6, 9, 8, 9
$ max_matrixstats &lt;dbl&gt; 5, 6, 9, 8, 9</code></pre>
</div>
</div>
</section>
<section id="method-comparison-table" class="level1">
<h1>Method Comparison Table</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 18%">
<col style="width: 18%">
<col style="width: 21%">
<col style="width: 18%">
<col style="width: 22%">
</colgroup>
<thead>
<tr class="header">
<th>Method</th>
<th>Speed</th>
<th>Package Required</th>
<th>Best For</th>
<th>Syntax Complexity</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>apply()</code></td>
<td>Slow</td>
<td>Base R</td>
<td>General use</td>
<td>Medium</td>
</tr>
<tr class="even">
<td><code>pmax()</code></td>
<td>Fast</td>
<td>Base R</td>
<td>Few columns</td>
<td>Low</td>
</tr>
<tr class="odd">
<td><code>dplyr::rowwise()</code></td>
<td>Slow</td>
<td>dplyr</td>
<td>Tidyverse workflows</td>
<td>High</td>
</tr>
<tr class="even">
<td><code>matrixStats::rowMaxs()</code></td>
<td>Very Fast</td>
<td>matrixStats</td>
<td>Large datasets</td>
<td>Medium</td>
</tr>
</tbody>
</table>
</section>
<section id="practical-examples" class="level1">
<h1>Practical Examples</h1>
<section id="example-1-student-exam-scores" class="level2">
<h2 class="anchored" data-anchor-id="example-1-student-exam-scores">Example 1: Student Exam Scores</h2>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Student performance data</span></span>
<span id="cb12-2">exam_scores <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="cb12-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Student =</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;">"Alice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bob"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Charlie"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Diana"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Eve"</span>),</span>
<span id="cb12-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Math =</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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>),</span>
<span id="cb12-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Science =</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;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">82</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">93</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>),</span>
<span id="cb12-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">English =</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;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">97</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">86</span>),</span>
<span id="cb12-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">History =</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;">82</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">94</span>)</span>
<span id="cb12-8">)</span>
<span id="cb12-9"></span>
<span id="cb12-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find highest score for each student</span></span>
<span id="cb12-11">exam_scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Highest_Score <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;">apply</span>(exam_scores[<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;">5</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max)</span>
<span id="cb12-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(exam_scores)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Student Math Science English History Highest_Score
1   Alice   85      90      88      82            90
2     Bob   92      85      89      88            92
3 Charlie   78      82      85      90            90
4   Diana   95      93      97      89            97
5     Eve   88      91      86      94            94</code></pre>
</div>
</div>
</section>
<section id="example-2-stock-price-analysis" class="level2">
<h2 class="anchored" data-anchor-id="example-2-stock-price-analysis">Example 2: Stock Price Analysis</h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quarterly stock prices with missing data</span></span>
<span id="cb14-2">stock_prices <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="cb14-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Stock =</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;">"AAPL"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"GOOGL"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"MSFT"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AMZN"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TSLA"</span>),</span>
<span id="cb14-4">  <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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">150.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2800.0</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;">3200.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">800.0</span>),</span>
<span id="cb14-5">  <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2750.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">280.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3100.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">750.0</span>),</span>
<span id="cb14-6">  <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="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">145.0</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;">285.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3250.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">900.0</span>),</span>
<span id="cb14-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Q4 =</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;">160.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2900.0</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">290.0</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;">850.0</span>)</span>
<span id="cb14-8">)</span>
<span id="cb14-9"></span>
<span id="cb14-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find maximum price per stock</span></span>
<span id="cb14-11">stock_prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Max_Price <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;">apply</span>(stock_prices[<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;">5</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max, <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="cb14-12"></span>
<span id="cb14-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find which quarter had the max price</span></span>
<span id="cb14-14">stock_prices<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Best_Quarter <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;">apply</span>(stock_prices[<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;">5</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb14-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(x)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(x)]</span>
<span id="cb14-16">})</span>
<span id="cb14-17"></span>
<span id="cb14-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(stock_prices)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 5
Columns: 7
$ Stock        &lt;chr&gt; "AAPL", "GOOGL", "MSFT", "AMZN", "TSLA"
$ Q1           &lt;dbl&gt; 150.5, 2800.0, NA, 3200.0, 800.0
$ Q2           &lt;dbl&gt; NA, 2750, 280, 3100, 750
$ Q3           &lt;dbl&gt; 145, NA, 285, 3250, 900
$ Q4           &lt;dbl&gt; 160, 2900, 290, NA, 850
$ Max_Price    &lt;dbl&gt; 160, 2900, 290, 3250, 900
$ Best_Quarter &lt;chr&gt; "Q4", "Q4", "Q4", "Q3", "Q3"</code></pre>
</div>
</div>
</section>
</section>
<section id="common-pitfalls-and-solutions" class="level1">
<h1>Common Pitfalls and Solutions</h1>
<p>• <strong>Forgetting <code>na.rm = TRUE</code>:</strong> Returns <code>NA</code> if any value in row is missing - <strong>Solution:</strong> Always use <code>na.rm = TRUE</code> when dealing with missing data</p>
<p>• <strong>Wrong MARGIN parameter:</strong> Using <code>MARGIN = 2</code> finds column max, not row max - <strong>Solution:</strong> Remember <code>1</code> = rows, <code>2</code> = columns</p>
<p>• <strong>All-NA rows:</strong> With <code>na.rm = TRUE</code>, returns <code>-Inf</code> instead of <code>NA</code> - <strong>Solution:</strong> Use custom function to check for all-NA rows</p>
<p>• <strong>Character columns:</strong> <code>max()</code> doesn’t work on text data - <strong>Solution:</strong> Select only numeric columns first</p>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p><strong>Practice Exercise:</strong></p>
<p>Create a data frame with sales data for different products across four months and find the best performing month for each product.</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your challenge data</span></span>
<span id="cb16-2">sales_data <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="cb16-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Product =</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;">"Laptop"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Phone"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tablet"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Watch"</span>),</span>
<span id="cb16-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Jan =</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;">1200</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">800</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">600</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">300</span>),</span>
<span id="cb16-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Feb =</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;">1100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">850</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">550</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">350</span>),</span>
<span id="cb16-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Mar =</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;">1300</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">900</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">700</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>),</span>
<span id="cb16-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Apr =</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;">1250</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">820</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">650</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">380</span>)</span>
<span id="cb16-8">)</span>
<span id="cb16-9"></span>
<span id="cb16-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">TODO</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: Find the maximum sales for each product</span></span>
<span id="cb16-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># </span><span class="al" style="color: #AD0000;
background-color: null;
font-style: inherit;">TODO</span><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">: Find which month had the highest sales for each product</span></span></code></pre></div>
</div>
<details>
<summary>
Click here for Solution!
</summary>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Solution 1: Find maximum sales</span></span>
<span id="cb17-2">sales_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Best_Sales <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;">apply</span>(sales_data[<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;">5</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max)</span>
<span id="cb17-3"></span>
<span id="cb17-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Solution 2: Find best month</span></span>
<span id="cb17-5">sales_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Best_Month <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;">apply</span>(sales_data[<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;">5</span>], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) {</span>
<span id="cb17-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(x)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">which.max</span>(x)]</span>
<span id="cb17-7">})</span>
<span id="cb17-8"></span>
<span id="cb17-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Alternative using multiple methods</span></span>
<span id="cb17-10">sales_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Max_pmax <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;">pmax</span>(sales_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Jan, sales_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Feb, </span>
<span id="cb17-11">                           sales_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Mar, sales_data<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Apr)</span>
<span id="cb17-12"></span>
<span id="cb17-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(sales_data)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  Product  Jan  Feb  Mar  Apr Best_Sales Best_Month Max_pmax
1  Laptop 1200 1100 1300 1250       1300        Mar     1300
2   Phone  800  850  900  820        900        Mar      900
3  Tablet  600  550  700  650        700        Mar      700
4   Watch  300  350  400  380        400        Mar      400</code></pre>
</div>
</div>
</details>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<p>• <strong><code>apply(df, 1, max)</code></strong> is the most common and versatile method for finding row maximums • <strong>Use <code>na.rm = TRUE</code></strong> when your data contains missing values • <strong><code>pmax()</code></strong> is faster for datasets with few columns • <strong><code>matrixStats::rowMaxs()</code></strong> provides the best performance for large datasets • <strong><code>dplyr::rowwise()</code></strong> integrates well with tidyverse workflows • <strong>Always specify <code>MARGIN = 1</code></strong> in <code>apply()</code> for row-wise operations</p>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Finding the <strong>max value in each row</strong> in R can be accomplished through multiple approaches, each with specific advantages. The <code>apply()</code> function remains the gold standard for most users due to its simplicity and reliability. For performance-critical applications, consider <code>matrixStats::rowMaxs()</code>, while tidyverse users will appreciate <code>dplyr</code>’s readable syntax.</p>
<p>Choose the method that best fits your workflow, data size, and coding style. With these techniques, you’ll efficiently handle row-wise maximum calculations in any R project.</p>
<p><strong>Ready to level up your R skills?</strong> Try implementing these methods with your own datasets and see which approach works best for your specific use case!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><a href="https://www.statology.org/r-max-value-in-each-row/">How to Find the Max Value in Each Row in R</a></li>
<li><a href="https://dplyr.tidyverse.org/articles/rowwise.html">Row-wise operations (dplyr documentation)</a></li>
<li><a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/apply">Apply Functions Over Array Margins (R Documentation)</a></li>
</ol>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-22/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Row Max R</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-22/</guid>
  <pubDate>Mon, 22 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Working with Google Sheets in Python: A Beginner’s Guide to EZSheets</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-17/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Author’s Note: I’m learning as I write this series! My goal is to break down each concept and explain the syntax in simple terms so we can both build confidence working with Google Sheets in Python. Every error I encounter becomes a learning opportunity for all of us.</strong></p>
</blockquote>
<section id="what-is-ezsheets" class="level1">
<h1>What is EZSheets?</h1>
<p>Google Sheets is a free, web-based spreadsheet application that’s perfect for collaboration. <strong>EZSheets</strong> is a Python library that makes working with Google Sheets incredibly simple . Think of it as a translator between Python and Google Sheets - it handles all the complex API details so you can focus on your data.</p>
<p>Unlike the official Google Sheets API (which can be overwhelming for beginners), EZSheets uses straightforward syntax that feels natural to Python programmers.</p>
</section>
<section id="installation-and-setup" class="level1">
<h1>Installation and Setup</h1>
<section id="step-1-install-ezsheets" class="level2">
<h2 class="anchored" data-anchor-id="step-1-install-ezsheets">Step 1: Install EZSheets</h2>
<p>Open your terminal or command prompt and run:</p>
<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="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install ezsheets</span></code></pre></div>
<p><strong>Simple Explanation:</strong> This command downloads and installs EZSheets along with all the helper libraries it needs to talk to Google’s servers .</p>
<p><strong>Troubleshooting Tips:</strong></p>
<ul>
<li>On Mac/Linux: Use <code>pip3</code> instead of <code>pip</code></li>
<li>Permission issues: Add <code>--user</code> flag: <code>pip install --user ezsheets</code></li>
</ul>
</section>
<section id="step-2-enable-google-apis" class="level2">
<h2 class="anchored" data-anchor-id="step-2-enable-google-apis">Step 2: Enable Google APIs</h2>
<p>Before Python can access your Google Sheets, you need to enable two APIs :</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>API Name</th>
<th>Purpose</th>
<th>Link</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Google Sheets API</td>
<td>Read/write spreadsheet data</td>
<td><a href="https://console.developers.google.com/apis/library/sheets.googleapis.com/">Enable Sheets API</a></td>
</tr>
<tr class="even">
<td>Google Drive API</td>
<td>Access/create files in Drive</td>
<td><a href="https://console.developers.google.com/apis/library/drive.googleapis.com/">Enable Drive API</a></td>
</tr>
</tbody>
</table>
<p><strong>Simple Explanation:</strong> APIs are like doorways that let different programs talk to each other. You’re giving Python permission to use these doorways.</p>
</section>
<section id="step-3-get-your-credentials" class="level2">
<h2 class="anchored" data-anchor-id="step-3-get-your-credentials">Step 3: Get Your Credentials</h2>
<p>You need three special files in the same folder as your Python script :</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>File Name</th>
<th>What It Does</th>
<th>How to Get It</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>credentials-sheets.json</code></td>
<td>Your “ID card” for Google APIs</td>
<td>Download from <a href="https://developers.google.com/sheets/api/quickstart/python/">Google Sheets Python Quickstart</a> and rename</td>
</tr>
<tr class="even">
<td><code>token-sheets.pickle</code></td>
<td>Remember you’re logged in to Sheets</td>
<td>Created automatically first time you run the code</td>
</tr>
<tr class="odd">
<td><code>token-drive.pickle</code></td>
<td>Remember you’re logged in to Drive</td>
<td>Created automatically during setup</td>
</tr>
</tbody>
</table>
<p><strong>Important:</strong> Treat these files like passwords - never share them or upload them to public repositories !</p>
</section>
</section>
<section id="your-first-google-sheets-script" class="level1">
<h1>Your First Google Sheets Script</h1>
<p>Let’s start with a simple example from “Automate the Boring Stuff”:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ezsheets</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This will open a browser window for login (first time only)</span></span>
<span id="cb2-4">ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.createSpreadsheet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'My First Python Spreadsheet'</span>)</span>
<span id="cb2-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Created spreadsheet: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ss<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><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb2-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Available sheets: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>sheetTitles<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<p><strong>What’s happening here:</strong></p>
<ul>
<li><code>import ezsheets</code>: Loads the EZSheets library</li>
<li><code>createSpreadsheet()</code>: Makes a new Google Sheet with the name you choose</li>
<li><code>ss.title</code>: Gets the name of your spreadsheet</li>
<li><code>ss.sheetTitles</code>: Shows all the worksheet tabs (like “Sheet1”)</li>
</ul>
</section>
<section id="understanding-spreadsheet-structure" class="level1">
<h1>Understanding Spreadsheet Structure</h1>
<p>Think of Google Sheets like a filing cabinet :</p>
<ul>
<li><strong>Spreadsheet</strong>: The entire file (like a binder)</li>
<li><strong>Sheet/Worksheet</strong>: Individual tabs within the file (like pages in the binder)</li>
<li><strong>Cells</strong>: Individual boxes where data goes (like A1, B2, C3)</li>
</ul>
</section>
<section id="basic-operations" class="level1">
<h1>Basic Operations</h1>
<section id="opening-an-existing-spreadsheet" class="level2">
<h2 class="anchored" data-anchor-id="opening-an-existing-spreadsheet">Opening an Existing Spreadsheet</h2>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 1: By spreadsheet ID (from the URL)</span></span>
<span id="cb3-2">ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.Spreadsheet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your-spreadsheet-id-here'</span>)</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 2: Upload a local file</span></span>
<span id="cb3-5">ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.upload(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'my_excel_file.xlsx'</span>)</span></code></pre></div>
<p><strong>Finding the Spreadsheet ID:</strong> Look at your Google Sheets URL : <code>https://docs.google.com/spreadsheets/d/YOUR_ID_IS_HERE/edit</code></p>
</section>
<section id="accessing-worksheets" class="level2">
<h2 class="anchored" data-anchor-id="accessing-worksheets">Accessing Worksheets</h2>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get the first sheet</span></span>
<span id="cb4-2">sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ss[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using index number</span></span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get sheet by name  </span></span>
<span id="cb4-5">sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ss[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sheet1'</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using sheet name</span></span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># See all available sheets</span></span>
<span id="cb4-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(ss.sheetTitles)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Returns: ('Sheet1', 'Sheet2', ...)</span></span></code></pre></div>
</section>
<section id="reading-data" class="level2">
<h2 class="anchored" data-anchor-id="reading-data">Reading Data</h2>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Get all data from a sheet</span></span>
<span id="cb5-2">all_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet.getRows()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Returns a list of lists</span></span>
<span id="cb5-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(all_data)</span>
<span id="cb5-4"></span>
<span id="cb5-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Access specific cells</span></span>
<span id="cb5-6">first_cell <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet[<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;">1</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row 1, Column 1 (A1)</span></span>
<span id="cb5-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Cell A1 contains: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>first_cell<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb5-8"></span>
<span id="cb5-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Using A1 notation (like in Excel)</span></span>
<span id="cb5-10">cell_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A1'</span>]</span>
<span id="cb5-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"A1 value: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>cell_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<p><strong>Simple Explanation:</strong></p>
<ul>
<li><code>getRows()</code>: Gets all the data as a list where each item is a row</li>
<li><code>[1, 1]</code>: Row and column numbers (starting from 1, not 0!)</li>
<li><code>['A1']</code>: Excel-style cell references</li>
</ul>
</section>
<section id="writing-data" class="level2">
<h2 class="anchored" data-anchor-id="writing-data">Writing Data</h2>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Update a single cell</span></span>
<span id="cb6-2">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'A1'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Hello, World!'</span></span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Update an entire row</span></span>
<span id="cb6-5">sheet.updateRow(<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;">'Name'</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;">'City'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Occupation'</span>])</span>
<span id="cb6-6">sheet.updateRow(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Alice'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">25</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'New York'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Engineer'</span>])</span>
<span id="cb6-7"></span>
<span id="cb6-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Update multiple cells</span></span>
<span id="cb6-9">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'B1'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Age'</span></span>
<span id="cb6-10">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C1'</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'City'</span></span></code></pre></div>
</section>
</section>
<section id="common-use-cases" class="level1">
<h1>Common Use Cases</h1>
<section id="data-entry-automation" class="level2">
<h2 class="anchored" data-anchor-id="data-entry-automation">1. Data Entry Automation</h2>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Example: Adding survey results</span></span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ezsheets</span>
<span id="cb7-3"></span>
<span id="cb7-4">ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.createSpreadsheet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Survey Results'</span>)</span>
<span id="cb7-5">sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ss[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb7-6"></span>
<span id="cb7-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set up headers</span></span>
<span id="cb7-8">headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Participant'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Rating'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Comments'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Date'</span>]</span>
<span id="cb7-9">sheet.updateRow(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, headers)</span>
<span id="cb7-10"></span>
<span id="cb7-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add data</span></span>
<span id="cb7-12">survey_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb7-13">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'John Doe'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Excellent service!'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-15'</span>],</span>
<span id="cb7-14">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Jane Smith'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Very good experience'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-16'</span>]</span>
<span id="cb7-15">]</span>
<span id="cb7-16"></span>
<span id="cb7-17"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, row_data <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(survey_data, start<span class="op" 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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Start from row 2</span></span>
<span id="cb7-18">    sheet.updateRow(i, row_data)</span></code></pre></div>
</section>
<section id="reading-and-processing-data" class="level2">
<h2 class="anchored" data-anchor-id="reading-and-processing-data">2. Reading and Processing Data</h2>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read existing data for analysis</span></span>
<span id="cb8-2">ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.Spreadsheet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'your-existing-spreadsheet-id'</span>)</span>
<span id="cb8-3">sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ss[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sales Data'</span>]</span>
<span id="cb8-4"></span>
<span id="cb8-5">all_rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet.getRows()</span>
<span id="cb8-6">header <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> all_rows[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># First row is usually headers</span></span>
<span id="cb8-7">data_rows <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> all_rows[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>:]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Everything except the header</span></span>
<span id="cb8-8"></span>
<span id="cb8-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Found </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(data_rows)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> records"</span>)</span>
<span id="cb8-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Columns: </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;">', '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>join(header)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
</section>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p>Now it’s time to put your Google Sheets automation skills into practice! Choose one of these beginner-friendly exercises to get started:</p>
<section id="exercise-1-sales-data-automation-beginner" class="level2">
<h2 class="anchored" data-anchor-id="exercise-1-sales-data-automation-beginner">🎯 Exercise 1: Sales Data Automation (Beginner)</h2>
<p><strong>Objective:</strong> Create a Python script that automatically updates a sales report in Google Sheets.</p>
<p><strong>Your Task:</strong></p>
<ol type="1">
<li>Create a new Google Sheet with columns: Date, Product, Quantity, Price, Total</li>
<li>Write a Python script using EZSheets to:
<ul>
<li>Add 5 sample sales records</li>
<li>Calculate the Total column (Quantity × Price)</li>
<li>Format the header row (bold, background color)</li>
<li>Add a summary row with total sales</li>
</ul></li>
</ol>
<p><strong>Solution Template:</strong></p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ezsheets</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Create or connect to your spreadsheet</span></span>
<span id="cb9-4">ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.createSpreadsheet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sales Report Practice'</span>)</span>
<span id="cb9-5">sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ss[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Set up headers</span></span>
<span id="cb9-8">headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Date'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Product'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantity'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Price'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Total'</span>]</span>
<span id="cb9-9">sheet.updateRow(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, headers)</span>
<span id="cb9-10"></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: Add sample data</span></span>
<span id="cb9-12">sample_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb9-13">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-15'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget A'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.99</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>],</span>
<span id="cb9-14">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-16'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget B'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45.50</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>],</span>
<span id="cb9-15">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-17'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget C'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.99</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>],</span>
<span id="cb9-16">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-18'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget A'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.99</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>],</span>
<span id="cb9-17">    [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-19'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget D'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">75.00</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>]</span>
<span id="cb9-18">]</span>
<span id="cb9-19"></span>
<span id="cb9-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add each row of data</span></span>
<span id="cb9-21"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(sample_data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>):  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Start at row 2</span></span>
<span id="cb9-22">    sheet.updateRow(i, row)</span>
<span id="cb9-23"></span>
<span id="cb9-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Exercise complete! View your sheet at: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>url<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<details>
<summary>
Click here for Complete Solution!
</summary>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ezsheets</span>
<span id="cb10-2"></span>
<span id="cb10-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_sales_report():</span>
<span id="cb10-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create new spreadsheet</span></span>
<span id="cb10-5">    ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.createSpreadsheet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Sales Report Practice'</span>)</span>
<span id="cb10-6">    sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ss[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb10-7">    </span>
<span id="cb10-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set up headers</span></span>
<span id="cb10-9">    headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Date'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Product'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantity'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Price'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Total'</span>]</span>
<span id="cb10-10">    sheet.updateRow(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, headers)</span>
<span id="cb10-11">    </span>
<span id="cb10-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample sales data</span></span>
<span id="cb10-13">    sales_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb10-14">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-15'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget A'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.99</span>],</span>
<span id="cb10-15">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-16'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget B'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">45.50</span>],</span>
<span id="cb10-16">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-17'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget C'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">19.99</span>],</span>
<span id="cb10-17">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-18'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget A'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">25.99</span>],</span>
<span id="cb10-18">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'2024-01-19'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Widget D'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">75.00</span>]</span>
<span id="cb10-19">    ]</span>
<span id="cb10-20">    </span>
<span id="cb10-21">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add data and calculate totals</span></span>
<span id="cb10-22">    total_sales <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb10-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, (date, product, qty, price) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(sales_data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>):</span>
<span id="cb10-24">        total <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> qty <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> price</span>
<span id="cb10-25">        total_sales <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> total</span>
<span id="cb10-26">        sheet.updateRow(i, [date, product, qty, price, total])</span>
<span id="cb10-27">    </span>
<span id="cb10-28">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add summary row</span></span>
<span id="cb10-29">    sheet.updateRow(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">7</span>, [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'TOTAL'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, total_sales])</span>
<span id="cb10-30">    </span>
<span id="cb10-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format currency columns (Price and Total)</span></span>
<span id="cb10-32">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Note: EZSheets has limited formatting - for advanced formatting, use gspread</span></span>
<span id="cb10-33">    </span>
<span id="cb10-34">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Sales report created successfully!"</span>)</span>
<span id="cb10-35">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Total Sales: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>total_sales<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:.2f}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-36">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"View your sheet: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>url<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb10-37">    </span>
<span id="cb10-38">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ss.url</span>
<span id="cb10-39"></span>
<span id="cb10-40"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run the function</span></span>
<span id="cb10-41"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb10-42">    create_sales_report()</span></code></pre></div>
</details>
<hr>
</section>
<section id="exercise-2-inventory-tracker-intermediate" class="level2">
<h2 class="anchored" data-anchor-id="exercise-2-inventory-tracker-intermediate">🎯 Exercise 2: Inventory Tracker (Intermediate)</h2>
<p><strong>Objective:</strong> Build an automated inventory management system that flags low stock items.</p>
<p><strong>Your Challenge:</strong></p>
<ol type="1">
<li>Create a sheet with columns: Item, Current Stock, Minimum Threshold, Status</li>
<li>Use Python to automatically update the Status column:
<ul>
<li>“Low Stock” if Current Stock &lt; Minimum Threshold</li>
<li>“In Stock” otherwise</li>
</ul></li>
<li>Generate a list of items that need restocking</li>
</ol>
<details>
<summary>
Click here for Complete Solution!
</summary>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> ezsheets</span>
<span id="cb11-2"></span>
<span id="cb11-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> create_inventory_tracker():</span>
<span id="cb11-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create new spreadsheet</span></span>
<span id="cb11-5">    ss <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ezsheets.createSpreadsheet(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Inventory Tracker'</span>)</span>
<span id="cb11-6">    sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ss[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb11-7">    </span>
<span id="cb11-8">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Set up headers</span></span>
<span id="cb11-9">    headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Item'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Current Stock'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Minimum Threshold'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Status'</span>]</span>
<span id="cb11-10">    sheet.updateRow(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, headers)</span>
<span id="cb11-11">    </span>
<span id="cb11-12">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample inventory data</span></span>
<span id="cb11-13">    inventory <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb11-14">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Laptop'</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;">10</span>],</span>
<span id="cb11-15">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mouse'</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;">15</span>],     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This will be low stock</span></span>
<span id="cb11-16">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Keyboard'</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;">20</span>],</span>
<span id="cb11-17">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Monitor'</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;">12</span>],   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This will be low stock</span></span>
<span id="cb11-18">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Webcam'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>],</span>
<span id="cb11-19">        [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Headset'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span>]</span>
<span id="cb11-20">    ]</span>
<span id="cb11-21">    </span>
<span id="cb11-22">    low_stock_items <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb11-23">    </span>
<span id="cb11-24">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Process each item</span></span>
<span id="cb11-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, (item, current, minimum) <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(inventory, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>):</span>
<span id="cb11-26">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Determine status</span></span>
<span id="cb11-27">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> current <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> minimum:</span>
<span id="cb11-28">            status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Low Stock"</span></span>
<span id="cb11-29">            low_stock_items.append(item)</span>
<span id="cb11-30">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb11-31">            status <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"In Stock"</span></span>
<span id="cb11-32">        </span>
<span id="cb11-33">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Update the row</span></span>
<span id="cb11-34">        sheet.updateRow(i, [item, current, minimum, status])</span>
<span id="cb11-35">    </span>
<span id="cb11-36">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add summary section</span></span>
<span id="cb11-37">    sheet.updateRow(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(inventory) <span class="op" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'SUMMARY:'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>])</span>
<span id="cb11-38">    sheet.updateRow(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(inventory) <span class="op" 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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Low Stock Items:'</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(low_stock_items), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>])</span>
<span id="cb11-39">    </span>
<span id="cb11-40">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># List low stock items</span></span>
<span id="cb11-41">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> j, item <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(low_stock_items):</span>
<span id="cb11-42">        sheet.updateRow(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(inventory) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> j, [item, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Needs Restocking'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">''</span>])</span>
<span id="cb11-43">    </span>
<span id="cb11-44">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Inventory tracker created!"</span>)</span>
<span id="cb11-45">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Items needing restock: </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;">', '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>join(low_stock_items) <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> low_stock_items <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'None'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-46">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"View your sheet: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>ss<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>url<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb11-47">    </span>
<span id="cb11-48">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> ss.url</span>
<span id="cb11-49"></span>
<span id="cb11-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run the function</span></span>
<span id="cb11-51"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb11-52">    create_inventory_tracker()</span></code></pre></div>
</details>
<hr>
</section>
<section id="exercise-3-budget-tracker-challenge-advanced" class="level2">
<h2 class="anchored" data-anchor-id="exercise-3-budget-tracker-challenge-advanced">🎯 Exercise 3: Budget Tracker Challenge (Advanced)</h2>
<p><strong>Objective:</strong> Create a comprehensive personal budget tracker with monthly summaries and category analysis.</p>
<p><strong>Your Challenge:</strong></p>
<ol type="1">
<li>Set up categories: Housing, Food, Transportation, Entertainment, Savings</li>
<li>Add expense entries with dates and amounts</li>
<li>Create monthly summary calculations</li>
<li>Implement validation to ensure no negative amounts</li>
</ol>
<p><strong>Success Criteria:</strong></p>
<ul>
<li>✅ Script runs without errors</li>
<li>✅ Categories are properly organized</li>
<li>✅ Monthly totals calculate correctly</li>
<li>✅ Data validation prevents invalid entries</li>
<li>✅ Summary section shows key insights</li>
</ul>
</section>
<section id="getting-started-tips" class="level2">
<h2 class="anchored" data-anchor-id="getting-started-tips">💡 Getting Started Tips:</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Step</th>
<th>Action</th>
<th>Command/Tip</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>1</strong></td>
<td>Install EZSheets</td>
<td><code>pip install ezsheets</code></td>
</tr>
<tr class="even">
<td><strong>2</strong></td>
<td>Set up authentication</td>
<td>Follow <a href="https://ezsheets.readthedocs.io/en/latest/quickstart.html">EZSheets quickstart guide</a></td>
</tr>
<tr class="odd">
<td><strong>3</strong></td>
<td>Start with Exercise 1</td>
<td>Build confidence with simpler tasks</td>
</tr>
<tr class="even">
<td><strong>4</strong></td>
<td>Test frequently</td>
<td>Run script after each major change</td>
</tr>
<tr class="odd">
<td><strong>5</strong></td>
<td>Use print statements</td>
<td>Debug by checking data at each step</td>
</tr>
</tbody>
</table>
</section>
<section id="completion-checklist" class="level2">
<h2 class="anchored" data-anchor-id="completion-checklist">📋 Completion Checklist:</h2>
<ul class="task-list">
<li><label><input type="checkbox">Code runs without errors</label></li>
<li><label><input type="checkbox">Google Sheet is properly formatted<br>
</label></li>
<li><label><input type="checkbox">All calculations work correctly</label></li>
<li><label><input type="checkbox">Code includes helpful comments</label></li>
<li><label><input type="checkbox">You can explain each line of code</label></li>
<li><label><input type="checkbox">You’ve tested with different data sets</label></li>
</ul>
<blockquote class="blockquote">
<p><strong>Pro Tip:</strong> Start small, test often, and don’t be afraid to break things! Every expert was once a beginner who kept experimenting.</p>
</blockquote>
</section>
</section>
<section id="go-to-commands-reference" class="level1">
<h1>Go To Commands Reference</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 28%">
<col style="width: 35%">
<col style="width: 35%">
</colgroup>
<thead>
<tr class="header">
<th>Operation</th>
<th>Code Example</th>
<th>What It Does</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Create new spreadsheet</td>
<td><code>ss = ezsheets.createSpreadsheet('Name')</code></td>
<td>Makes a new Google Sheet</td>
</tr>
<tr class="even">
<td>Open existing spreadsheet</td>
<td><code>ss = ezsheets.Spreadsheet('ID')</code></td>
<td>Opens a sheet you already have</td>
</tr>
<tr class="odd">
<td>List your spreadsheets</td>
<td><code>ezsheets.listSpreadsheets()</code></td>
<td>Shows all your Google Sheets</td>
</tr>
<tr class="even">
<td>Get sheet by index</td>
<td><code>sheet = ss[0]</code></td>
<td>Gets the first worksheet</td>
</tr>
<tr class="odd">
<td>Get sheet by name</td>
<td><code>sheet = ss['Sheet1']</code></td>
<td>Gets worksheet by name</td>
</tr>
<tr class="even">
<td>Read all data</td>
<td><code>data = sheet.getRows()</code></td>
<td>Gets everything as lists</td>
</tr>
<tr class="odd">
<td>Read one cell</td>
<td><code>value = sheet['A1']</code></td>
<td>Gets single cell value</td>
</tr>
<tr class="even">
<td>Write to cell</td>
<td><code>sheet['A1'] = 'Hello'</code></td>
<td>Puts text in a cell</td>
</tr>
<tr class="odd">
<td>Write entire row</td>
<td><code>sheet.updateRow(1, ['A', 'B', 'C'])</code></td>
<td>Fills a whole row</td>
</tr>
</tbody>
</table>
</section>
<section id="troubleshooting-common-issues" class="level1">
<h1>Troubleshooting Common Issues</h1>
<p><strong>Problem:</strong> Browser doesn’t open for authentication<br>
<strong>Solution:</strong> Try running a simple command like <code>ezsheets.listSpreadsheets()</code> to trigger the login process .</p>
<p><strong>Problem:</strong> “Credentials not found” error<br>
<strong>Solution:</strong> Make sure <code>credentials-sheets.json</code> is in the same folder as your Python script and spelled exactly right.</p>
<p><strong>Problem:</strong> Can’t find your spreadsheet<br>
<strong>Solution:</strong> Check that you’re using the correct spreadsheet ID from the URL, not the title.</p>
</section>
<section id="key-takeaways" class="level1">
<h1>Key Takeaways</h1>
<blockquote class="blockquote">
<p><strong>Key Finding:</strong> EZSheets makes Google Sheets automation accessible to beginners through simple, intuitive syntax.</p>
</blockquote>
<ul>
<li><strong>EZSheets makes Google Sheets simple</strong> no complex API calls needed</li>
<li><strong>Three files required</strong> credentials and two token files for authentication</li>
<li><strong>Rows and columns start at 1</strong> unlike typical Python indexing that starts at 0</li>
<li><strong>Two ways to access cells</strong> either <code>sheet[1, 1]</code> or <code>sheet['A1']</code></li>
<li><strong>Always keep credentials secure</strong> treat them like passwords</li>
<li><strong>Perfect for beginners</strong> syntax is intuitive and forgiving</li>
</ul>
</section>
<section id="next-steps" class="level1">
<h1>Next Steps</h1>
<p>Now that you understand the basics, try these practice exercises:</p>
<ol type="1">
<li><strong>Create a personal budget tracker</strong> - Use EZSheets to monitor expenses</li>
<li><strong>Build a simple inventory system</strong> - Track items and quantities</li>
<li><strong>Automate data collection from web forms</strong> - Process survey responses</li>
<li><strong>Generate weekly reports from existing data</strong> - Create automated reporting</li>
</ol>
<p>Remember, every expert was once a beginner. The key is to start with simple examples and gradually work up to more complex automation tasks. Google Sheets with Python opens up endless possibilities for organizing and analyzing your data!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><p><a href="https://developers.google.com/sheets/api/guides/concepts"><strong>Google Sheets API Overview - Official Documentation</strong></a> - Comprehensive guide to understanding the Google Sheets API architecture, authentication methods, and core concepts for developers looking to integrate spreadsheet functionality into their applications.</p></li>
<li><p><a href="https://ezsheets.readthedocs.io/en/latest/"><strong>EZSheets Official Documentation</strong></a> - Complete documentation for the EZSheets Python library, including installation instructions, authentication setup, detailed API reference, and practical examples for automating Google Sheets tasks.</p></li>
<li><p><a href="https://developers.google.com/sheets/api/quickstart/python"><strong>Google Sheets API Python Quickstart Guide</strong></a> - Step-by-step tutorial for setting up your first Python application with the Google Sheets API, including credential configuration, basic operations, and troubleshooting common setup issues.</p></li>
<li><p><a href="https://pypi.org/project/EZSheets/"><strong>EZSheets PyPI Package Page</strong></a> - Official package repository with installation commands, version history, dependency information, and community contributions for the EZSheets library, maintained by the Python Package Index.</p></li>
</ol>
<blockquote class="blockquote">
<p><strong>Ready to automate your spreadsheets?</strong> Pick an exercise above and start coding! Remember, the best way to learn is by doing. Each exercise builds on the previous one, so start with Exercise 1 and work your way up as you gain confidence.</p>
</blockquote>
<p><em>This article is part of a learning series where I explore Python automation tools. If you have questions or suggestions, remember that we’re all learning together!</em></p>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-17/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>EZSheets with Python</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-17/</guid>
  <pubDate>Wed, 17 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Complete Guide to Applying Functions to Each Row in R Matrices and Data Frames</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-15/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong> The <code>apply()</code> function in R is your go-to tool for performing operations on rows or columns of matrices and data frames. With <code>MARGIN=1</code> for rows and <code>MARGIN=2</code> for columns, you can efficiently process data without writing explicit loops.</p>
</blockquote>
<section id="what-is-the-apply-function-in-r" class="level1">
<h1>What is the apply() Function in R?</h1>
<p>The <strong><code>apply()</code> function</strong> is a powerful tool in R that allows you to apply a function to the margins (rows or columns) of an array, matrix, or data frame . Instead of writing loops, you can process entire rows or columns with a single function call, making your code cleaner and more efficient.</p>
<p>The <code>apply()</code> function returns a vector, array, or list of values obtained by applying a function to the specified margins . It’s particularly useful for R programmers who need to perform the same operation across multiple rows or columns of data.</p>
</section>
<section id="basic-syntax-and-arguments" class="level1">
<h1>Basic Syntax and Arguments</h1>
<section id="core-syntax-structure" class="level2">
<h2 class="anchored" data-anchor-id="core-syntax-structure">Core Syntax Structure</h2>
<div class="sourceCode" 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;">apply</span>(X, MARGIN, FUN, ...)</span></code></pre></div>
</section>
<section id="parameter-breakdown" class="level2">
<h2 class="anchored" data-anchor-id="parameter-breakdown">Parameter Breakdown</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 26%">
<col style="width: 31%">
<col style="width: 19%">
<col style="width: 21%">
</colgroup>
<thead>
<tr class="header">
<th>Parameter</th>
<th>Description</th>
<th>Values</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>X</strong></td>
<td>Input data</td>
<td>Matrix, array, or data frame</td>
<td><code>my_matrix</code></td>
</tr>
<tr class="even">
<td><strong>MARGIN</strong></td>
<td>Direction of operation</td>
<td><code>1</code> = rows, <code>2</code> = columns</td>
<td><code>1</code> for row-wise</td>
</tr>
<tr class="odd">
<td><strong>FUN</strong></td>
<td>Function to apply</td>
<td>Built-in or custom function</td>
<td><code>sum</code>, <code>mean</code></td>
</tr>
<tr class="even">
<td><strong>…</strong></td>
<td>Additional arguments</td>
<td>Extra parameters for FUN</td>
<td><code>na.rm = TRUE</code></td>
</tr>
</tbody>
</table>
</section>
<section id="key-points-to-remember" class="level2">
<h2 class="anchored" data-anchor-id="key-points-to-remember">Key Points to Remember</h2>
<ul>
<li><strong>MARGIN=1</strong>: Apply function to each <strong>row</strong></li>
<li><strong>MARGIN=2</strong>: Apply function to each <strong>column</strong></li>
<li><strong>X</strong> must be a matrix or data frame (data frames get coerced to matrices)</li>
<li><strong>FUN</strong> can be any R function - built-in or user-defined</li>
</ul>
</section>
</section>
<section id="row-wise-operations-with-apply" class="level1">
<h1>Row-wise Operations with apply()</h1>
<section id="basic-row-operations" class="level2">
<h2 class="anchored" data-anchor-id="basic-row-operations">Basic Row Operations</h2>
<p>Here are some row wise operations using <code>apply()</code> with <strong>MARGIN=1</strong>:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create sample matrix</span></span>
<span id="cb2-2">sample_matrix <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;">matrix</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;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</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;">ncol=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colnames</span>(sample_matrix) <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;">'B'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'C'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'D'</span>)</span>
<span id="cb2-4"></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row sums</span></span>
<span id="cb2-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sum)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 22 26 30</code></pre>
</div>
<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;"># Row means  </span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5.5 6.5 7.5</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row maximums</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10 11 12</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row minimums</span></span>
<span id="cb8-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, min)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2 3</code></pre>
</div>
</div>
</section>
<section id="custom-functions-for-rows" class="level2">
<h2 class="anchored" data-anchor-id="custom-functions-for-rows">Custom Functions for Rows</h2>
<p>You can create custom functions for more complex row operations:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Custom function: Calculate range (max - min) for each row</span></span>
<span id="cb10-2">row_range <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</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;">min</span>(x) }</span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, row_range)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 9 9 9</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Custom function: Count values greater than 5 in each row</span></span>
<span id="cb12-2">count_gt5 <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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <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;">5</span>) }</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, count_gt5)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 2 3</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Custom function: Standard deviation for each row</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sd)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.872983 3.872983 3.872983</code></pre>
</div>
</div>
</section>
<section id="working-with-data-frames" class="level2">
<h2 class="anchored" data-anchor-id="working-with-data-frames">Working with Data Frames</h2>
<p>When applying functions to data frame rows, ensure all columns are numeric:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create numeric data frame</span></span>
<span id="cb16-2">scores <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="cb16-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">math =</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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>),</span>
<span id="cb16-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">science =</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;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">87</span>),</span>
<span id="cb16-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">english =</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;">82</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>)</span>
<span id="cb16-6">)</span>
<span id="cb16-7"></span>
<span id="cb16-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate student averages (row-wise means)</span></span>
<span id="cb16-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(scores, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 85.00000 90.00000 85.66667 89.66667</code></pre>
</div>
</div>
</section>
</section>
<section id="column-wise-operations" class="level1">
<h1>Column-wise Operations</h1>
<p>While the focus is on rows, understanding column operations helps you use <code>apply()</code> more effectively:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Column operations with MARGIN=2</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, sum)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A  B  C  D 
 6 15 24 33 </code></pre>
</div>
<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;">apply</span>(sample_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A  B  C  D 
 2  5  8 11 </code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Subject averages from scores data frame  </span></span>
<span id="cb22-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(scores, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   math science english 
  86.25   87.75   88.75 </code></pre>
</div>
</div>
</section>
<section id="advanced-custom-functions" class="level1">
<h1>Advanced Custom Functions</h1>
<section id="functions-with-additional-arguments" class="level2">
<h2 class="anchored" data-anchor-id="functions-with-additional-arguments">Functions with Additional Arguments</h2>
<p>You can pass extra arguments to your functions:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Weighted average function</span></span>
<span id="cb24-2">weighted_mean <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, weights) {</span>
<span id="cb24-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> weights) <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>(weights)</span>
<span id="cb24-4">}</span>
<span id="cb24-5"></span>
<span id="cb24-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Apply with custom weights</span></span>
<span id="cb24-7">weights <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.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.3</span>)</span>
<span id="cb24-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(scores, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, weighted_mean, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">weights =</span> weights)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 85.0 90.0 84.9 89.9</code></pre>
</div>
</div>
</section>
<section id="complex-conditional-logic" class="level2">
<h2 class="anchored" data-anchor-id="complex-conditional-logic">Complex Conditional Logic</h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Grade analysis function</span></span>
<span id="cb26-2">grade_analysis <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>(scores) {</span>
<span id="cb26-3">  avg <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;">mean</span>(scores)</span>
<span id="cb26-4">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (avg <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;">90</span>) {</span>
<span id="cb26-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<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;">'A grade, average:'</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(avg, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb26-6">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (avg <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;">80</span>) {</span>
<span id="cb26-7">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<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;">'B grade, average:'</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(avg, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb26-8">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb26-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<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;">'C grade, average:'</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(avg, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)))</span>
<span id="cb26-10">  }</span>
<span id="cb26-11">}</span>
<span id="cb26-12"></span>
<span id="cb26-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(scores, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, grade_analysis)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "B grade, average: 85"   "A grade, average: 90"   "B grade, average: 85.7"
[4] "B grade, average: 89.7"</code></pre>
</div>
</div>
</section>
</section>
<section id="common-issues-and-troubleshooting" class="level1">
<h1>Common Issues and Troubleshooting</h1>
<section id="mixed-data-types-problem" class="level2">
<h2 class="anchored" data-anchor-id="mixed-data-types-problem">Mixed Data Types Problem</h2>
<p><strong>Issue</strong>: When using <code>apply()</code> on data frames with mixed types, R converts everything to character .</p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Problematic mixed data frame</span></span>
<span id="cb28-2">mixed_data <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="cb28-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</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;">'Alice'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Bob'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Charlie'</span>),</span>
<span id="cb28-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">score1 =</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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>),</span>
<span id="cb28-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">score2 =</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;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>)</span>
<span id="cb28-6">)</span>
<span id="cb28-7"></span>
<span id="cb28-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This converts numbers to text!</span></span>
<span id="cb28-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(mixed_data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">' | '</span>))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Alice | 85 | 88"   "Bob | 90 | 85"     "Charlie | 78 | 91"</code></pre>
</div>
</div>
<p><strong>Solution</strong>: Select only numeric columns:</p>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Select only numeric columns</span></span>
<span id="cb30-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Better approach</span></span>
<span id="cb30-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(mixed_data[, <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;">'score1'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'score2'</span>)], <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, mean)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 86.5 87.5 84.5</code></pre>
</div>
</div>
</section>
<section id="error-handling" class="level2">
<h2 class="anchored" data-anchor-id="error-handling">Error Handling</h2>
<p><strong>Issue</strong>: If one row causes an error, the entire <code>apply()</code> stops .</p>
<p><strong>Solution</strong>: Use <code>tryCatch()</code> for robust functions:</p>
<div class="cell">
<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">safe_log <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>
<span id="cb32-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tryCatch</span>({</span>
<span id="cb32-3">    <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;">any</span>(x <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;">0</span>)) {</span>
<span id="cb32-4">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb32-5">    }</span>
<span id="cb32-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">log</span>(x))</span>
<span id="cb32-7">  }, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">error =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(e) <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>)</span>
<span id="cb32-8">}</span>
<span id="cb32-9"></span>
<span id="cb32-10">test_data <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;">matrix</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="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="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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</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;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb32-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(test_data, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, safe_log)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[[1]]
[1] 0.000000 1.098612 1.609438

[[2]]
[1] NA</code></pre>
</div>
</div>
</section>
</section>
<section id="performance-alternatives" class="level1">
<h1>Performance Alternatives</h1>
<p>For simple operations, use specialized functions instead of <code>apply()</code>:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Operation</th>
<th>apply() Version</th>
<th>Faster Alternative</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Row sums</td>
<td><code>apply(X, 1, sum)</code></td>
<td><code>rowSums(X)</code></td>
</tr>
<tr class="even">
<td>Row means</td>
<td><code>apply(X, 1, mean)</code></td>
<td><code>rowMeans(X)</code></td>
</tr>
<tr class="odd">
<td>Column sums</td>
<td><code>apply(X, 2, sum)</code></td>
<td><code>colSums(X)</code></td>
</tr>
<tr class="even">
<td>Column means</td>
<td><code>apply(X, 2, mean)</code></td>
<td><code>colMeans(X)</code></td>
</tr>
</tbody>
</table>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Performance comparison</span></span>
<span id="cb34-2">test_matrix <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;">matrix</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;">12</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</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;">ncol=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb34-3"></span>
<span id="cb34-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># These are equivalent but rowSums() is faster:</span></span>
<span id="cb34-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(test_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sum)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 22 26 30</code></pre>
</div>
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(test_matrix)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 22 26 30</code></pre>
</div>
</div>
<p>Let’s do a simple benchmark test using <code>rbenchmark</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rbenchmark)</span>
<span id="cb38-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggplot2)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'ggplot2' was built under R version 4.5.1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb40-2"></span>
<span id="cb40-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 1000 by 1000 matrix</span></span>
<span id="cb40-4">test_matrix <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;">matrix</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;">1000</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;">1000</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nrow=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb40-5"></span>
<span id="cb40-6">benchmark_test_tbl <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;">benchmark</span>(</span>
<span id="cb40-7">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"apply"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">apply</span>(test_matrix, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, sum),</span>
<span id="cb40-8">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rowSums"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rowSums</span>(test_matrix),</span>
<span id="cb40-9">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replications =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>L,</span>
<span id="cb40-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">columns =</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;">"test"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"replications"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"elapsed"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"relative"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"user.self"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sys.self"</span>)</span>
<span id="cb40-11">)</span>
<span id="cb40-12"></span>
<span id="cb40-13">benchmark_test_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb40-14">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(relative)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>     test replications elapsed relative user.self sys.self
1 rowSums          100    0.42    1.000      0.36     0.01
2   apply          100    2.97    7.071      1.84     0.83</code></pre>
</div>
<div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualize the results in a boxplot</span></span>
<span id="cb42-2">benchmark_test_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb42-3">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> test, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> elapsed)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stat =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"identity"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.328</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(test))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-5">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb42-6">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb42-7">                <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;">"Benchmark of apply() vs rowSums"</span>,</span>
<span id="cb42-8">                <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;">"Function"</span>,</span>
<span id="cb42-9">                <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;">"Elapsed Time (s)"</span>,</span>
<span id="cb42-10">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Test"</span></span>
<span id="cb42-11">        )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-15/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="alternative-approaches" class="level1">
<h1>Alternative Approaches</h1>
<section id="other-apply-family-functions" class="level2">
<h2 class="anchored" data-anchor-id="other-apply-family-functions">Other Apply Family Functions</h2>
<ul>
<li><strong><code>lapply()</code></strong>: Works with lists, returns a list</li>
<li><strong><code>sapply()</code></strong>: Simplifies <code>lapply()</code> output to vectors<br>
</li>
<li><strong><code>mapply()</code></strong>: Multivariate version for multiple inputs</li>
</ul>
</section>
<section id="tidyverse-alternatives" class="level2">
<h2 class="anchored" data-anchor-id="tidyverse-alternatives">Tidyverse Alternatives</h2>
<p>For complex row operations, consider:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span>
<span id="cb43-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row-wise operations in dplyr</span></span>
<span id="cb43-3">scores <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;">rowwise</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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">avg =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(math, science, english)))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 4
# Rowwise: 
   math science english   avg
  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt; &lt;dbl&gt;
1    85      88      82  85  
2    90      85      95  90  
3    78      91      88  85.7
4    92      87      90  89.7</code></pre>
</div>
</div>
</section>
</section>
<section id="best-practices-checklist" class="level1">
<h1>Best Practices Checklist</h1>
<p>✅ <strong>Use <code>MARGIN=1</code> for rows, <code>MARGIN=2</code> for columns</strong><br>
✅ <strong>Ensure data is numeric before using apply()</strong><br>
✅ <strong>Use <code>rowSums()</code>, <code>colSums()</code>, <code>rowMeans()</code>, <code>colMeans()</code> for simple operations</strong><br>
✅ <strong>Test custom functions on individual rows/columns first</strong><br>
✅ <strong>Add error handling with <code>tryCatch()</code> for robust functions</strong><br>
✅ <strong>Consider alternatives for mixed-type data frames</strong><br>
✅ <strong>Remember that data frames are coerced to matrices</strong></p>
</section>
<section id="quick-reference-table" class="level1">
<h1>Quick Reference Table</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 16%">
<col style="width: 36%">
<col style="width: 22%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Task</th>
<th>Code Example</th>
<th>MARGIN</th>
<th>Output</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Row sum</td>
<td><code>apply(X, 1, sum)</code></td>
<td>1</td>
<td>Vector of row sums</td>
</tr>
<tr class="even">
<td>Row mean</td>
<td><code>apply(X, 1, mean)</code></td>
<td>1</td>
<td>Vector of row means</td>
</tr>
<tr class="odd">
<td>Custom function</td>
<td><code>apply(X, 1, my_func)</code></td>
<td>1</td>
<td>Vector of results</td>
</tr>
<tr class="even">
<td>With arguments</td>
<td><code>apply(X, 1, func, arg=value)</code></td>
<td>1</td>
<td>Vector with custom args</td>
</tr>
<tr class="odd">
<td>Anonymous function</td>
<td><code>apply(X, 1, function(x) ...)</code></td>
<td>1</td>
<td>Vector from custom logic</td>
</tr>
</tbody>
</table>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p>Now it’s time to put your knowledge into practice! Below is a real-world scenario that will test your understanding of the <code>apply()</code> function for row-wise operations.</p>
<section id="practice-scenario-student-performance-analysis" class="level2">
<h2 class="anchored" data-anchor-id="practice-scenario-student-performance-analysis"><strong>Practice Scenario: Student Performance Analysis</strong></h2>
<p>You’re analyzing test scores for students in a programming course. Each student took four exams: <strong>Midterm 1</strong>, <strong>Midterm 2</strong>, <strong>Final Project</strong>, and <strong>Final Exam</strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Student score data</span></span>
<span id="cb45-2">student_scores <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="cb45-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">student_id =</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;">"STU001"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU002"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU003"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU004"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU005"</span>),</span>
<span id="cb45-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midterm1 =</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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>),</span>
<span id="cb45-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midterm2 =</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;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">87</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">82</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>),</span>
<span id="cb45-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">project =</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;">93</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>),</span>
<span id="cb45-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">final_exam =</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;">91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">79</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">93</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>)</span>
<span id="cb45-8">)</span></code></pre></div>
</div>
</section>
<section id="tasks-to-complete" class="level2">
<h2 class="anchored" data-anchor-id="tasks-to-complete"><strong>Tasks to Complete:</strong></h2>
<p><strong>Task 1:</strong> Calculate the average score for each student across all four exams.</p>
<p><strong>Task 2:</strong> Find the highest score achieved by each student.</p>
<p><strong>Task 3:</strong> Create a custom function that determines if a student’s average is above 85. Apply this function to each student.</p>
<p><strong>Task 4:</strong> Calculate the range (difference between highest and lowest score) for each student.</p>
<p><strong>Task 5:</strong> Determine how many scores above 90 each student achieved.</p>
</section>
<section id="your-challenge" class="level2">
<h2 class="anchored" data-anchor-id="your-challenge"><strong>Your Challenge:</strong></h2>
<p>Write R code using the <code>apply()</code> function to solve each task. Remember:</p>
<ul>
<li>Use <code>MARGIN = 1</code> for row-wise operations</li>
<li>Select only the numeric columns (exclude <code>student_id</code>)</li>
<li>Test your code step by step</li>
</ul>
</section>
<section id="hints" class="level2">
<h2 class="anchored" data-anchor-id="hints"><strong>Hints:</strong></h2>
<ul>
<li>For numeric columns only: <code>student_scores[, 2:5]</code> or <code>student_scores[, -1]</code></li>
<li>Custom functions can be defined inline: <code>function(x) { your_logic_here }</code></li>
<li>Use <code>sum(x &gt; 90)</code> to count values above a threshold</li>
</ul>
<details>
<summary>
Click here for Solution!
</summary>
<p>Here’s the complete solution with explanations:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the data</span></span>
<span id="cb46-2">student_scores <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="cb46-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">student_id =</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;">"STU001"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU002"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU003"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU004"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"STU005"</span>),</span>
<span id="cb46-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midterm1 =</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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>),</span>
<span id="cb46-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">midterm2 =</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;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">87</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">82</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>),</span>
<span id="cb46-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">project =</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;">93</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">95</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>),</span>
<span id="cb46-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">final_exam =</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;">91</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">89</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">79</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">93</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>)</span>
<span id="cb46-8">)</span>
<span id="cb46-9"></span>
<span id="cb46-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract only numeric columns (exclude student_id)</span></span>
<span id="cb46-11">scores_only <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> student_scores[, <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;">5</span>]</span>
<span id="cb46-12"></span>
<span id="cb46-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Task 1: Calculate average score for each student</span></span>
<span id="cb46-14">student_averages <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;">apply</span>(scores_only, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, mean)</span>
<span id="cb46-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student Averages:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Student Averages:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(student_averages)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 89.50 90.75 81.00 90.25 92.75</code></pre>
</div>
<div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Task 2: Find highest score for each student  </span></span>
<span id="cb50-2">student_max <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;">apply</span>(scores_only, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max)</span>
<span id="cb50-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student Maximum Scores:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Student Maximum Scores:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(student_max)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 93 95 85 93 96</code></pre>
</div>
<div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Task 3: Custom function - average above 85?</span></span>
<span id="cb54-2">above_85 <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>
<span id="cb54-3">  avg <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;">mean</span>(x)</span>
<span id="cb54-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(avg <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;">85</span>)</span>
<span id="cb54-5">}</span>
<span id="cb54-6">student_above_85 <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;">apply</span>(scores_only, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, above_85)</span>
<span id="cb54-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Students with average above 85:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Students with average above 85:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb56" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(student_above_85)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1]  TRUE  TRUE FALSE  TRUE  TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Task 4: Calculate range for each student</span></span>
<span id="cb58-2">student_range <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;">apply</span>(scores_only, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</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;">min</span>(x))</span>
<span id="cb58-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student Score Ranges:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Student Score Ranges:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(student_range)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 8 8 7 5 8</code></pre>
</div>
<div class="sourceCode cell-code" id="cb62" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb62-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Task 5: Count scores above 90 for each student</span></span>
<span id="cb62-2">scores_above_90 <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;">apply</span>(scores_only, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(x <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;">90</span>))</span>
<span id="cb62-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of scores above 90 per student:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Number of scores above 90 per student:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb64" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(scores_above_90)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 2 0 2 3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb66" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb66-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bonus: Create a comprehensive summary</span></span>
<span id="cb66-2">student_summary <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="cb66-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">student_id =</span> student_scores<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>student_id,</span>
<span id="cb66-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">average =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(student_averages, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb66-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_score =</span> student_max,</span>
<span id="cb66-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">above_85_avg =</span> student_above_85,</span>
<span id="cb66-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">score_range =</span> student_range,</span>
<span id="cb66-8">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scores_above_90 =</span> scores_above_90</span>
<span id="cb66-9">)</span>
<span id="cb66-10"></span>
<span id="cb66-11"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Complete Student Summary:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Complete Student Summary:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb68" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb68-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(student_summary)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>  student_id average max_score above_85_avg score_range scores_above_90
1     STU001   89.50        93         TRUE           8               2
2     STU002   90.75        95         TRUE           8               2
3     STU003   81.00        85        FALSE           7               0
4     STU004   90.25        93         TRUE           5               2
5     STU005   92.75        96         TRUE           8               3</code></pre>
</div>
</div>
<p><strong>Key Learning Points:</strong></p>
<ul>
<li><strong>Data Selection</strong>: We used student_scores[, 2:5]` to select only numeric columns, avoiding issues with mixed data types</li>
<li><strong>Custom Functions</strong>: Task 3 and 5 showed how to write custom functions and apply them row-wise</li>
<li><strong>Anonymous Functions</strong>: Tasks 4 and 5 used <code>function(x)</code> inline for concise operations</li>
<li><strong>Practical Application</strong>: This exercise mirrors real-world data analysis scenarios</li>
</ul>
<p><strong>Alternative Solutions:</strong></p>
<div class="sourceCode" id="cb70" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb70-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># You could also use:</span></span>
<span id="cb70-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For averages: rowMeans(scores_only) - faster for simple means</span></span>
<span id="cb70-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># For sums: rowSums(scores_only) - faster for simple sums</span></span>
<span id="cb70-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># But apply() gives you more flexibility for custom operations!</span></span></code></pre></div>
</details>
</section>
<section id="test-your-understanding" class="level2">
<h2 class="anchored" data-anchor-id="test-your-understanding"><strong>Test Your Understanding</strong></h2>
<p>After completing the exercise, ask yourself:</p>
<ol type="1">
<li><strong>Why did we exclude the <code>student_id</code> column?</strong> <em>(Hint: mixed data types)</em></li>
<li><strong>Could we use <code>rowMeans()</code> instead of <code>apply()</code> for Task 1?</strong> <em>(Yes, but apply() is more flexible)</em></li>
<li><strong>How would you modify the code to handle missing values (NA)?</strong> <em>(Add <code>na.rm = TRUE</code>)</em></li>
</ol>
</section>
<section id="next-steps" class="level2">
<h2 class="anchored" data-anchor-id="next-steps"><strong>Next Steps</strong></h2>
<p>Try modifying the exercise:</p>
<ul>
<li>Add a sixth student with some missing scores (<code>NA</code>)</li>
<li>Create a function that assigns letter grades based on averages</li>
<li>Calculate weighted averages (e.g., final exam worth 40%, others 20% each)</li>
</ul>
<p><strong>Great job working through this exercise!</strong> You’ve now practiced the core concepts of using <code>apply()</code> for row-wise operations in real-world scenarios. This foundation will serve you well in data analysis projects.</p>
</section>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>The <code>apply()</code> function is an essential tool for R programmers working with matrices and data frames. By using <strong>MARGIN=1</strong> for row-wise operations, you can efficiently process data without explicit loops. Remember to handle mixed data types carefully, consider performance alternatives for simple operations, and add error handling for robust code.</p>
<p><strong>Key Takeaways:</strong></p>
<ul>
<li>Use <code>apply(X, 1, FUN)</code> for row-wise operations</li>
<li>Handle mixed data types by selecting numeric columns only<br>
</li>
<li>Consider <code>rowSums()</code>, <code>rowMeans()</code> for better performance on simple operations</li>
<li>Add error handling with <code>tryCatch()</code> for production code</li>
<li>Test custom functions thoroughly before applying to large datasets</li>
</ul>
<p>Start experimenting with <code>apply()</code> in your next R project - it will make your data processing code cleaner and more efficient!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><p><strong>R Documentation</strong> - <a href="https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/apply">apply: Apply Functions Over Array Margins</a><br>
<em>Official R documentation for the apply() function, including detailed syntax, arguments, usage examples, and technical specifications.</em></p></li>
<li><p><strong>DataQuest</strong> - <a href="https://www.dataquest.io/blog/apply-functions-in-r-sapply-lapply-tapply/">Apply Functions in R with Examples</a><br>
<em>In-depth tutorial explaining the apply() function family in R with practical data analysis examples, comparing efficiency with loops and vectorized operations.</em></p></li>
<li><p><strong>R-bloggers</strong> - <a href="https://www.r-bloggers.com/2022/03/complete-tutorial-on-using-apply-functions-in-r/">Complete Tutorial on Using ‘apply’ Functions in R</a><br>
<em>Step-by-step guide featuring real-world data analysis examples, custom function applications, and best practices for using apply() functions efficiently.</em></p></li>
</ol>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-15/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Using apply() in R</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-15/</guid>
  <pubDate>Mon, 15 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Working with Excel Spreadsheets in Python: A Complete Beginner’s Guide to openpyxl</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-10/</link>
  <description><![CDATA[ 






<p><strong>Authors Note: I am learning as I write this series, so I might make mistakes or do things that are not optimal. If you find any of these situations, please comment so readers have the correct or better knowledge and I too can learn.</strong></p>
<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong> openpyxl is a powerful Python library that lets you create, read, and modify Excel files without needing Microsoft Excel installed. Perfect for automating repetitive spreadsheet tasks!</p>
</blockquote>
<p>Excel spreadsheets are everywhere in the business world, and as a Python programmer, you’ll often need to work with them. Whether you’re analyzing sales data, creating reports, or automating data entry, the <strong>openpyxl</strong> library is your gateway to Excel automation. This guide will teach you everything you need to know to start working with Excel files using Python.</p>
<hr>
<section id="what-is-openpyxl" class="level1">
<h1>What is openpyxl?</h1>
<p><strong>openpyxl</strong> is a Python library that allows you to read, write, and modify Excel 2010 xlsx/xlsm/xltx/xltm files . Think of it as your Python toolkit for Excel automation. Unlike other libraries, openpyxl doesn’t require Microsoft Excel to be installed on your computer, making it perfect for servers and automated systems.</p>
<section id="why-use-openpyxl" class="level2">
<h2 class="anchored" data-anchor-id="why-use-openpyxl">Why Use openpyxl?</h2>
<p>• <strong>No Excel Required</strong>: Works without Microsoft Excel installed • <strong>Full Feature Support</strong>: Handles formulas, charts, styling, and more • <strong>Active Development</strong>: Regularly updated and well-maintained • <strong>Beginner Friendly</strong>: Simple syntax that’s easy to learn</p>
</section>
</section>
<section id="excel-terminology-made-simple" class="level1">
<h1>Excel Terminology Made Simple</h1>
<p>Before diving into code, let’s understand the basic Excel terms you’ll encounter:</p>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Term</th>
<th>Explanation</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Workbook</strong></td>
<td>The main Excel file you work with</td>
<td><code>mydata.xlsx</code></td>
</tr>
<tr class="even">
<td><strong>Worksheet</strong></td>
<td>A single sheet within a workbook</td>
<td>Sheet1, Sheet2</td>
</tr>
<tr class="odd">
<td><strong>Column</strong></td>
<td>Vertical line, labeled with letters</td>
<td>A, B, C, D…</td>
</tr>
<tr class="even">
<td><strong>Row</strong></td>
<td>Horizontal line, labeled with numbers</td>
<td>1, 2, 3, 4…</td>
</tr>
<tr class="odd">
<td><strong>Cell</strong></td>
<td>Intersection of column and row</td>
<td>A1, B2, C3…</td>
</tr>
</tbody>
</table>
</section>
<section id="getting-started-installation" class="level1">
<h1>Getting Started: Installation</h1>
<p>Installing openpyxl is straightforward. Open your terminal or command prompt and run:</p>
<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="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install openpyxl</span></code></pre></div>
<p>That’s it! You’re ready to start working with Excel files in Python.</p>
</section>
<section id="creating-your-first-excel-file" class="level1">
<h1>Creating Your First Excel File</h1>
<p>Let’s start with the basics - creating a new Excel workbook and adding some data:</p>
<div id="508e9408" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> openpyxl <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Workbook</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a new workbook</span></span>
<span id="cb2-4">workbook <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Workbook()</span>
<span id="cb2-5">sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> workbook.active</span>
<span id="cb2-6"></span>
<span id="cb2-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add some data</span></span>
<span id="cb2-8">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A1"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Product"</span></span>
<span id="cb2-9">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B1"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Price"</span> </span>
<span id="cb2-10">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C1"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stock"</span></span>
<span id="cb2-11"></span>
<span id="cb2-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add product information</span></span>
<span id="cb2-13">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Laptop"</span></span>
<span id="cb2-14">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B2"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">999.99</span></span>
<span id="cb2-15">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C2"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">15</span></span>
<span id="cb2-16"></span>
<span id="cb2-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the file</span></span>
<span id="cb2-18">workbook.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"products.xlsx"</span>)</span></code></pre></div>
</div>
<p>This creates a new Excel file called <code>products.xlsx</code> with a simple product table .</p>
</section>
<section id="reading-existing-excel-files" class="level1">
<h1>Reading Existing Excel Files</h1>
<p>Reading data from existing Excel files is just as simple:</p>
<div id="e8da8bcf" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> openpyxl <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> load_workbook</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load an existing workbook</span></span>
<span id="cb3-4">workbook <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> load_workbook(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"products.xlsx"</span>)</span>
<span id="cb3-5">sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> workbook.active</span>
<span id="cb3-6"></span>
<span id="cb3-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read cell values</span></span>
<span id="cb3-8">product_name <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A2"</span>].value</span>
<span id="cb3-9">price <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B2"</span>].value</span>
<span id="cb3-10"></span>
<span id="cb3-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Product: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>product_name<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Price: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>price<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Output: Product: Laptop, Price: $999.99</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Product: Laptop, Price: $999.99</code></pre>
</div>
</div>
<section id="reading-multiple-cells-at-once" class="level2">
<h2 class="anchored" data-anchor-id="reading-multiple-cells-at-once">Reading Multiple Cells at Once</h2>
<p>For larger datasets, you can iterate through rows efficiently:</p>
<div id="f2dfd77f" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read all data from the spreadsheet</span></span>
<span id="cb5-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sheet.iter_rows(values_only<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>):</span>
<span id="cb5-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(row)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>('Product', 'Price', 'Stock')
('Laptop', 999.99, 15)</code></pre>
</div>
</div>
<p>This prints each row as a tuple, making it easy to process data in bulk .</p>
</section>
</section>
<section id="working-with-cells-and-data" class="level1">
<h1>Working with Cells and Data</h1>
<section id="accessing-cells-in-different-ways" class="level2">
<h2 class="anchored" data-anchor-id="accessing-cells-in-different-ways">Accessing Cells in Different Ways</h2>
<p>openpyxl gives you multiple ways to access cells:</p>
<div id="1cd3f074" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 1: Cell reference (like in Excel)</span></span>
<span id="cb7-2">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A1"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello"</span></span>
<span id="cb7-3">value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A1"</span>].value</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 2: Row and column numbers (1-based indexing)</span></span>
<span id="cb7-6">sheet.cell(row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello"</span>)</span>
<span id="cb7-7">value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sheet.cell(row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>).value</span>
<span id="cb7-8"></span>
<span id="cb7-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 3: Appending rows (great for adding data)</span></span>
<span id="cb7-10">sheet.append([<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Mouse"</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.99</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">150</span>])</span>
<span id="cb7-11"></span>
<span id="cb7-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read all data from the spreadsheet</span></span>
<span id="cb7-13"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sheet.iter_rows(values_only<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>):</span>
<span id="cb7-14">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(row)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>('Hello', 'Price', 'Stock')
('Laptop', 999.99, 15)
('Mouse', 29.99, 150)</code></pre>
</div>
</div>
</section>
<section id="data-types-and-conversion" class="level2">
<h2 class="anchored" data-anchor-id="data-types-and-conversion">Data Types and Conversion</h2>
<p>openpyxl automatically handles different data types:</p>
<p>• <strong>Text</strong>: <code>sheet["A1"] = "Product Name"</code> • <strong>Numbers</strong>: <code>sheet["B1"] = 299.99</code> • <strong>Dates</strong>: <code>sheet["C1"] = datetime.date(2024, 1, 15)</code> • <strong>Formulas</strong>: <code>sheet["D1"] = "=B1*C1"</code></p>
</section>
</section>
<section id="adding-style-and-formatting" class="level1">
<h1>Adding Style and Formatting</h1>
<p>Making your spreadsheets look professional is important. Here’s how to add formatting:</p>
<div id="d8a9b4cb" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> openpyxl.styles <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Font, PatternFill, Alignment</span>
<span id="cb9-2"></span>
<span id="cb9-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make headers bold and centered</span></span>
<span id="cb9-4">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A1"</span>].font <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Font(bold<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb9-5">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A1"</span>].alignment <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Alignment(horizontal<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'center'</span>)</span>
<span id="cb9-6"></span>
<span id="cb9-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add background color</span></span>
<span id="cb9-8">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A1"</span>].fill <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> PatternFill(start_color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"366092"</span>, </span>
<span id="cb9-9">                               end_color<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"366092"</span>, </span>
<span id="cb9-10">                               fill_type<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>)</span>
<span id="cb9-11"></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Format numbers as currency</span></span>
<span id="cb9-13">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B2"</span>].number_format <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$#,##0.00'</span></span></code></pre></div>
</div>
<section id="common-formatting-options" class="level2">
<h2 class="anchored" data-anchor-id="common-formatting-options">Common Formatting Options</h2>
<table class="caption-top table">
<thead>
<tr class="header">
<th>Format Type</th>
<th>Code Example</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Bold Text</strong></td>
<td><code>Font(bold=True)</code></td>
<td><strong>Bold</strong></td>
</tr>
<tr class="even">
<td><strong>Currency</strong></td>
<td><code>'$#,##0.00'</code></td>
<td>$1,234.56</td>
</tr>
<tr class="odd">
<td><strong>Percentage</strong></td>
<td><code>'0.00%'</code></td>
<td>25.50%</td>
</tr>
<tr class="even">
<td><strong>Date</strong></td>
<td><code>'YYYY-MM-DD'</code></td>
<td>2024-01-15</td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="working-with-multiple-worksheets" class="level1">
<h1>Working with Multiple Worksheets</h1>
<p>Most real-world Excel files have multiple sheets. Here’s how to manage them:</p>
<div id="144dd944" class="cell" data-execution_count="6">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create additional sheets</span></span>
<span id="cb10-2">data_sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> workbook.create_sheet(title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sales Data"</span>)</span>
<span id="cb10-3">summary_sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> workbook.create_sheet(title<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Summary"</span>)</span>
<span id="cb10-4"></span>
<span id="cb10-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Switch between sheets</span></span>
<span id="cb10-6">current_sheet <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> workbook[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sales Data"</span>]</span>
<span id="cb10-7"></span>
<span id="cb10-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># List all sheet names</span></span>
<span id="cb10-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(workbook.sheetnames)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ['Sheet', 'Sales Data', 'Summary']</span></span>
<span id="cb10-10"></span>
<span id="cb10-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Rename a sheet</span></span>
<span id="cb10-12">summary_sheet.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Monthly Summary"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>['Sheet', 'Sales Data', 'Summary']</code></pre>
</div>
</div>
</section>
<section id="adding-formulas-and-calculations" class="level1">
<h1>Adding Formulas and Calculations</h1>
<p>One of Excel’s most powerful features is automatic calculations. openpyxl makes this easy:</p>
<div id="5aa15ff3" class="cell" data-execution_count="7">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add formulas (Excel will calculate them)</span></span>
<span id="cb12-2">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D1"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Total Value"</span></span>
<span id="cb12-3">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D2"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=B2*C2"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Price × Stock</span></span>
<span id="cb12-4"></span>
<span id="cb12-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create summary calculations</span></span>
<span id="cb12-6">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B5"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=SUM(B2:B4)"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of prices</span></span>
<span id="cb12-7">sheet[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C5"</span>] <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"=AVERAGE(C2:C4)"</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Average stock</span></span></code></pre></div>
</div>
<p><strong>Important Note</strong>: openpyxl stores formulas as text. Excel calculates the results when you open the file .</p>
</section>
<section id="creating-simple-charts" class="level1">
<h1>Creating Simple Charts</h1>
<p>Visual data representation makes reports more compelling:</p>
<div id="b2c38a11" class="cell" data-execution_count="8">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> openpyxl.chart <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> BarChart, Reference</span>
<span id="cb13-2"></span>
<span id="cb13-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a bar chart</span></span>
<span id="cb13-4">chart <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BarChart()</span>
<span id="cb13-5">chart.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Product Stock Levels"</span></span>
<span id="cb13-6">chart.x_axis.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Products"</span></span>
<span id="cb13-7">chart.y_axis.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Stock Quantity"</span></span>
<span id="cb13-8"></span>
<span id="cb13-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Define data range</span></span>
<span id="cb13-10">data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Reference(sheet, min_col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, min_row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, max_row<span class="op" 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="cb13-11">categories <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Reference(sheet, min_col<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, min_row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, max_row<span class="op" 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="cb13-12"></span>
<span id="cb13-13">chart.add_data(data, titles_from_data<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb13-14">chart.set_categories(categories)</span>
<span id="cb13-15"></span>
<span id="cb13-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add chart to worksheet</span></span>
<span id="cb13-17">sheet.add_chart(chart, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E2"</span>)</span>
<span id="cb13-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the workbook</span></span>
<span id="cb13-19">workbook.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"products.xlsx"</span>)</span></code></pre></div>
</div>
</section>
<section id="best-practices-for-beginners" class="level1">
<h1>Best Practices for Beginners</h1>
<section id="essential-tips" class="level2">
<h2 class="anchored" data-anchor-id="essential-tips">Essential Tips</h2>
<p>• <strong>Always save your workbook</strong>: Use <code>workbook.save("filename.xlsx")</code> after making changes • <strong>Use descriptive variable names</strong>: <code>student_worksheet</code> instead of <code>ws1</code> • <strong>Test with small datasets first</strong>: Verify your code works before processing large files • <strong>Handle errors gracefully</strong>: Use try-except blocks for file operations • <strong>Keep backups</strong>: Always backup important files before modifying them</p>
</section>
<section id="common-mistakes-to-avoid" class="level2">
<h2 class="anchored" data-anchor-id="common-mistakes-to-avoid">Common Mistakes to Avoid</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 45%">
<col style="width: 28%">
</colgroup>
<thead>
<tr class="header">
<th>Mistake</th>
<th>Why It Happens</th>
<th>Solution</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Forgetting to save</strong></td>
<td>Code runs but file doesn’t update</td>
<td>Always call <code>workbook.save()</code></td>
</tr>
<tr class="even">
<td><strong>Wrong file extension</strong></td>
<td>Using <code>.xls</code> instead of <code>.xlsx</code></td>
<td>openpyxl only works with <code>.xlsx</code></td>
</tr>
<tr class="odd">
<td><strong>1-based vs 0-based indexing</strong></td>
<td>Excel uses 1-based indexing</td>
<td>Remember: A1 is row=1, column=1</td>
</tr>
</tbody>
</table>
</section>
</section>
<section id="practical-example-sales-report" class="level1">
<h1>Practical Example: Sales Report</h1>
<p>Let’s put everything together with a real-world example:</p>
<div id="5889f61f" class="cell" data-execution_count="9">
<div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb14-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> openpyxl <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Workbook</span>
<span id="cb14-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> openpyxl.styles <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Font</span>
<span id="cb14-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> datetime</span>
<span id="cb14-4"></span>
<span id="cb14-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create sales report</span></span>
<span id="cb14-6">wb <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Workbook()</span>
<span id="cb14-7">ws <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> wb.active</span>
<span id="cb14-8">ws.title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sales Report"</span></span>
<span id="cb14-9"></span>
<span id="cb14-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add headers with formatting</span></span>
<span id="cb14-11">headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Date'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Product'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Quantity'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Unit Price'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Total'</span>]</span>
<span id="cb14-12">ws.append(headers)</span>
<span id="cb14-13"></span>
<span id="cb14-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make headers bold</span></span>
<span id="cb14-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> cell <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> ws[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>]:</span>
<span id="cb14-16">    cell.font <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Font(bold<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)</span>
<span id="cb14-17"></span>
<span id="cb14-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add sales data</span></span>
<span id="cb14-19">sales_data <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb14-20">    [datetime.date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</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;">15</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Laptop'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">999.99</span>],</span>
<span id="cb14-21">    [datetime.date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</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;">16</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mouse'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">29.99</span>],</span>
<span id="cb14-22">    [datetime.date(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2024</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;">17</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Keyboard'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">79.99</span>]</span>
<span id="cb14-23">]</span>
<span id="cb14-24"></span>
<span id="cb14-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sales_data:</span>
<span id="cb14-26">    ws.append(row)</span>
<span id="cb14-27"></span>
<span id="cb14-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add total formulas</span></span>
<span id="cb14-29"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</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;">5</span>):</span>
<span id="cb14-30">    ws.cell(row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>).value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f'=C</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">*D</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>row<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span></span>
<span id="cb14-31">    ws.cell(row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>).number_format <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$#,##0.00'</span></span>
<span id="cb14-32">    ws.cell(row<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>row, column<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>).number_format <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'$#,##0.00'</span></span>
<span id="cb14-33"></span>
<span id="cb14-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save the report</span></span>
<span id="cb14-35">wb.save(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'sales_report.xlsx'</span>)</span></code></pre></div>
</div>
</section>
<section id="quick-reference-table" class="level1">
<h1>Quick Reference Table</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 18%">
<col style="width: 42%">
<col style="width: 39%">
</colgroup>
<thead>
<tr class="header">
<th>Task</th>
<th>Code Example</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Create workbook</strong></td>
<td><code>wb = Workbook()</code></td>
<td>New Excel file</td>
</tr>
<tr class="even">
<td><strong>Load workbook</strong></td>
<td><code>wb = load_workbook('file.xlsx')</code></td>
<td>Open existing file</td>
</tr>
<tr class="odd">
<td><strong>Access sheet</strong></td>
<td><code>ws = wb.active</code></td>
<td>Get current sheet</td>
</tr>
<tr class="even">
<td><strong>Read cell</strong></td>
<td><code>value = ws['A1'].value</code></td>
<td>Get cell value</td>
</tr>
<tr class="odd">
<td><strong>Write cell</strong></td>
<td><code>ws['A1'] = 'Hello'</code></td>
<td>Set cell value</td>
</tr>
<tr class="even">
<td><strong>Add row</strong></td>
<td><code>ws.append(['A', 'B', 'C'])</code></td>
<td>Add data row</td>
</tr>
<tr class="odd">
<td><strong>Save file</strong></td>
<td><code>wb.save('file.xlsx')</code></td>
<td>Save changes</td>
</tr>
</tbody>
</table>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Working with Excel spreadsheets in Python using openpyxl opens up endless possibilities for data automation and analysis. You’ve learned how to create, read, and modify Excel files, add formatting and charts, and follow best practices that will serve you well in real-world projects.</p>
<p>The key to mastering openpyxl is practice. Start with simple tasks like reading data from existing files, then gradually work up to creating complex reports with multiple sheets, formulas, and charts. Remember to always test your code with small datasets first, handle errors properly, and keep backups of important files.</p>
<p><strong>Ready to automate your Excel workflows?</strong> Start with the examples in this guide and gradually build more complex solutions. Your future self will thank you for learning this valuable skill!</p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<p>Based on the research findings and best practices for digital content citations, here’s a properly formatted references section with four working, relevant clickable links for the Excel/openpyxl tutorial:</p>
</section>
<section id="references-1" class="level1">
<h1>References</h1>
<ol type="1">
<li><p><strong>openpyxl Official Documentation</strong> - The comprehensive official documentation for the openpyxl library, including installation guides, API reference, and advanced features.<br>
<a href="https://openpyxl.readthedocs.io/">https://openpyxl.readthedocs.io/</a></p></li>
<li><p><strong>A Guide to Excel Spreadsheets in Python With openpyxl – Real Python</strong> - An in-depth tutorial covering practical use cases, detailed code examples, and best practices for manipulating Excel spreadsheets using Python.<br>
<a href="https://realpython.com/openpyxl-excel-spreadsheets-python/">https://realpython.com/openpyxl-excel-spreadsheets-python/</a></p></li>
<li><p><strong>Reading an Excel File Using Python openpyxl Module – GeeksforGeeks</strong> - A step-by-step beginner’s guide with code snippets for reading and extracting data from Excel files using the openpyxl library.<br>
<a href="https://www.geeksforgeeks.org/python/python-reading-excel-file-using-openpyxl-module/">https://www.geeksforgeeks.org/python/python-reading-excel-file-using-openpyxl-module/</a></p></li>
<li><p><strong>How to Read Excel File in Python using Openpyxl – Medium</strong> - An accessible tutorial explaining the fundamentals of reading Excel files, accessing worksheets, and retrieving data with openpyxl for Python beginners.<br>
<a href="https://medium.com/@vidvatek/how-to-read-excel-file-in-python-using-openpyxl-354f3729b1cf">https://medium.com/<span class="citation" data-cites="vidvatek/how-to-read-excel-file-in-python-using-openpyxl-354f3729b1cf">@vidvatek/how-to-read-excel-file-in-python-using-openpyxl-354f3729b1cf</span></a></p></li>
</ol>
<hr>
<p><em>Have you tried automating Excel tasks with Python? Share your experiences and questions in the comments below, and don’t forget to share this guide with fellow Python beginners on social media!</em></p>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-10/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Excel Spreadsheets with Python</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-10/</guid>
  <pubDate>Wed, 10 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>TidyDensity Update</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-08/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Update:</strong> TidyDensity 1.5.2 delivers significant performance improvements and enhanced mixture modeling capabilities, but introduces breaking changes.</p>
</blockquote>
<p><strong>TidyDensity 1.5.2</strong> has arrived with substantial improvements that will transform how R programmers work with statistical distributions. Released on September 6, 2025, this update brings a fundamentally redesigned <code>quantile_normalize()</code> function and powerful new mixture modeling capabilities through enhanced <code>tidy_mixture_density()</code> functionality . While these changes offer compelling performance benefits, they also introduce breaking changes that require careful consideration for existing workflows.</p>
<hr>
<section id="what-is-tidydensity" class="level1">
<h1>What is TidyDensity?</h1>
<p>TidyDensity is an R package designed to simplify the generation, analysis, and visualization of random numbers from statistical distributions within the tidyverse ecosystem. The package provides a consistent, tidy interface for working with distributional data, making it invaluable for simulation studies, statistical modeling, and exploratory data analysis.</p>
<p><strong>Core capabilities include:</strong></p>
<ul>
<li>Generating tidy random samples from numerous distributions</li>
<li>Creating and analyzing mixture models</li>
<li>Performing quantile normalization for cross-sample comparability</li>
<li>Seamless integration with tidyverse workflows</li>
</ul>
</section>
<section id="breaking-changes-the-new-quantile_normalize" class="level1">
<h1>Breaking Changes: The New <code>quantile_normalize()</code></h1>
<section id="performance-revolution" class="level2">
<h2 class="anchored" data-anchor-id="performance-revolution"><strong>Performance Revolution</strong></h2>
<p>The most significant change in TidyDensity 1.5.2 is the complete redesign of the <code>quantile_normalize()</code> function. This algorithmic overhaul delivers substantial performance improvements.</p>
</section>
<section id="technical-implementation" class="level2">
<h2 class="anchored" data-anchor-id="technical-implementation"><strong>Technical Implementation</strong></h2>
<p>The new algorithm leverages vectorized operations and indexing techniques, moving away from the classical approach that relied on memory-intensive intermediate storage. The redesign focuses on:</p>
<ul>
<li><strong>Reduced redundant sorting operations</strong></li>
<li><strong>In-place memory operations</strong> where possible</li>
<li><strong>Optimized index mapping</strong> for restoring original order</li>
<li><strong>Enhanced algorithmic efficiency</strong> for large datasets</li>
</ul>
</section>
<section id="why-breaking-changes-occurred" class="level2">
<h2 class="anchored" data-anchor-id="why-breaking-changes-occurred"><strong>Why Breaking Changes Occurred</strong></h2>
<p>The algorithmic improvements come with a trade-off: <strong>slightly different numerical outputs</strong>. While the statistical properties remain identical (same quantiles, same normalization effect), the exact element-wise values may differ between versions. The biggest difference is that the function now only returns the normalized data. The old one returned the input data, output data and other intermediate information like duplicate ranks rows.</p>
<div class="cell">
<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>(TidyDensity)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'TidyDensity' was built under R version 4.5.1</code></pre>
</div>
<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;"># Example: Both versions produce identical quantiles</span></span>
<span id="cb3-2">data <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;">matrix</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">ncol =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>)</span>
<span id="cb3-3">normalized_data <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;">quantile_normalize</span>(data)</span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(normalized_data)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>           [,1]       [,2]       [,3]       [,4]
[1,] -0.9980322  0.8648786  0.8648786  1.2291761
[2,] -0.5656369 -0.5656369  1.2291761  0.8648786
[3,]  1.2291761  0.2846513  0.2846513  0.2846513
[4,]  0.2846513  1.2291761 -0.9980322 -0.9980322
[5,]  0.8648786 -0.9980322 -0.5656369 -0.5656369</code></pre>
</div>
<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;"># All columns now share identical quantile distributions</span></span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># But individual elements may differ slightly from v1.5.1</span></span>
<span id="cb5-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.data.frame</span>(normalized_data) <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;">sapply</span>(<span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(x) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile</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;">seq</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</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;">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></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>             V1         V2         V3         V4
0%   -0.9980322 -0.9980322 -0.9980322 -0.9980322
25%  -0.5656369 -0.5656369 -0.5656369 -0.5656369
50%   0.2846513  0.2846513  0.2846513  0.2846513
75%   0.8648786  0.8648786  0.8648786  0.8648786
100%  1.2291761  1.2291761  1.2291761  1.2291761</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Return in tibble format</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">quantile_normalize</span>(</span>
<span id="cb7-3"><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;">rand_norm_1 =</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;">30</span>),</span>
<span id="cb7-4">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">rand_norm_b =</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;">30</span>)),</span>
<span id="cb7-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.return_tibble =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 30 × 2
   rand_norm_1 rand_norm_b
         &lt;dbl&gt;       &lt;dbl&gt;
 1     -0.589       1.28  
 2     -1.17        1.04  
 3     -1.85       -0.0239
 4      0.899      -0.232 
 5      0.204       1.21  
 6      1.21       -0.796 
 7     -1.60        1.11  
 8      0.0248     -1.17  
 9     -1.22       -0.710 
10     -1.00        0.370 
# ℹ 20 more rows</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p><strong>Important:</strong> The quantile normalization properties are perfectly preserved - all columns have identical quantiles after processing. Only the specific element arrangements differ.</p>
</blockquote>
</section>
</section>
<section id="new-features-enhanced-tidy_mixture_density" class="level1">
<h1>New Features: Enhanced <code>tidy_mixture_density()</code></h1>
<section id="flexible-combination-types" class="level2">
<h2 class="anchored" data-anchor-id="flexible-combination-types"><strong>Flexible Combination Types</strong></h2>
<p>TidyDensity 1.5.2 introduces a powerful <code>.combination_type</code> parameter to <code>tidy_mixture_density()</code>, enabling five different ways to combine distributions :</p>
<table class="caption-top table">
<colgroup>
<col style="width: 42%">
<col style="width: 32%">
<col style="width: 25%">
</colgroup>
<thead>
<tr class="header">
<th>Combination Type</th>
<th>Description</th>
<th>Use Case</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>stack</strong></td>
<td>Concatenate all data points (default)</td>
<td>Traditional mixture models</td>
</tr>
<tr class="even">
<td><strong>add</strong></td>
<td>Element-wise addition</td>
<td>Additive effects modeling</td>
</tr>
<tr class="odd">
<td><strong>subtract</strong></td>
<td>Element-wise subtraction</td>
<td>Difference analysis</td>
</tr>
<tr class="even">
<td><strong>multiply</strong></td>
<td>Element-wise multiplication</td>
<td>Interaction effects</td>
</tr>
<tr class="odd">
<td><strong>divide</strong></td>
<td>Element-wise division</td>
<td>Ratio analysis</td>
</tr>
</tbody>
</table>
</section>
<section id="practical-examples" class="level2">
<h2 class="anchored" data-anchor-id="practical-examples"><strong>Practical Examples</strong></h2>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Traditional mixture model (default behavior)</span></span>
<span id="cb9-2">mix_stack <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;">tidy_mixture_density</span>(</span>
<span id="cb9-3">  <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;">100</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>), </span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tidy_normal</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.mean =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.sd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>),</span>
<span id="cb9-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combination_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stack"</span></span>
<span id="cb9-6">)</span>
<span id="cb9-7">mix_stack</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$data
$data$dist_tbl
# A tibble: 150 × 2
       x      y
   &lt;int&gt;  &lt;dbl&gt;
 1     1 -0.609
 2     2 -0.370
 3     3 -0.308
 4     4 -0.786
 5     5  0.437
 6     6 -0.552
 7     7  0.303
 8     8 -0.652
 9     9 -0.144
10    10 -0.260
# ℹ 140 more rows

$data$dens_tbl
# A tibble: 150 × 2
       x        y
   &lt;dbl&gt;    &lt;dbl&gt;
 1 -4.28 0.000118
 2 -4.19 0.000171
 3 -4.10 0.000245
 4 -4.01 0.000349
 5 -3.91 0.000489
 6 -3.82 0.000677
 7 -3.73 0.000931
 8 -3.63 0.00126 
 9 -3.54 0.00170 
10 -3.45 0.00226 
# ℹ 140 more rows

$data$input_data
$data$input_data$`rnorm(100, 0, 1)`
  [1] -0.60865878 -0.37038002 -0.30760339 -0.78599279  0.43657861 -0.55247080
  [7]  0.30320995 -0.65226084 -0.14429342 -0.25965742 -1.57960296  0.07128014
 [13]  0.65531402  1.35367040 -0.30645307 -0.82656469  1.32659588  0.36869342
 [19]  0.31268606  1.84046365 -1.35549208 -0.15825175  0.68863337 -1.39859775
 [25] -1.07112427  1.45502151  0.06602545 -0.39876615  0.05499137  0.09214760
 [31]  0.38800665 -1.04310666 -0.93508809  0.78018540 -0.14736187  0.48487063
 [37] -0.71797977 -0.09083663  0.24619862  0.42560605 -0.91303163 -0.40070704
 [43] -0.09056107  2.12683480  0.97909343  0.25586273  0.06160965 -0.24959411
 [49] -0.63688175  0.61513865 -1.80508425 -0.10904217 -1.49586272  0.65779129
 [55] -0.21556674  1.45041449  1.64820547 -0.00864845  1.14990888 -0.14165598
 [61]  1.08637758 -0.47666081  0.31451903  1.59206247 -0.31551530 -1.60855895
 [67]  0.91927450 -0.56171737 -0.17915531  0.25223463  0.99074046  1.09265035
 [73] -0.42699577 -1.42269492  0.28942361 -0.93808071 -0.38747430 -1.04629553
 [79] -0.93624539 -0.89624495 -0.94646613  1.43409772  0.40376921  2.20170782
 [85]  0.14770417 -1.10348135 -0.84095040  0.95636639  1.13483275 -0.43345698
 [91]  0.77418611  0.24623017 -0.49152719  0.97051886 -0.40725688  0.02543623
 [97] -0.16623957 -1.15500277  1.37865589  1.67954844

$data$input_data$`tidy_normal(.mean = 5, .sd = 1)`
# A tibble: 50 × 7
   sim_number     x     y    dx       dy      p     q
   &lt;fct&gt;      &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;
 1 1              1  6.52  2.33 0.000974 0.936   6.52
 2 1              2  3.39  2.45 0.00261  0.0538  3.39
 3 1              3  5.54  2.57 0.00629  0.706   5.54
 4 1              4  5.40  2.69 0.0136   0.655   5.40
 5 1              5  5.64  2.81 0.0262   0.739   5.64
 6 1              6  5.40  2.93 0.0457   0.656   5.40
 7 1              7  5.02  3.05 0.0718   0.508   5.02
 8 1              8  4.34  3.17 0.102    0.254   4.34
 9 1              9  4.81  3.29 0.133    0.423   4.81
10 1             10  4.08  3.41 0.160    0.179   4.08
# ℹ 40 more rows



$plots
$plots$line_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$plots$dens_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>

$input_fns
[1] "rnorm(100, 0, 1), tidy_normal(.mean = 5, .sd = 1)"</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Additive mixture for modeling combined effects</span></span>
<span id="cb13-2">mix_additive <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;">tidy_mixture_density</span>(</span>
<span id="cb13-3">  <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;">50</span>), </span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbeta</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">0.5</span>), </span>
<span id="cb13-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combination_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"add"</span></span>
<span id="cb13-6">)</span>
<span id="cb13-7">mix_additive</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$data
$data$dist_tbl
# A tibble: 50 × 2
       x       y
   &lt;int&gt;   &lt;dbl&gt;
 1     1  0.172 
 2     2 -0.385 
 3     3  0.388 
 4     4  0.168 
 5     5 -1.48  
 6     6  0.743 
 7     7  0.859 
 8     8  0.783 
 9     9 -0.0703
10    10 -0.992 
# ℹ 40 more rows

$data$dens_tbl
# A tibble: 50 × 2
       x        y
   &lt;dbl&gt;    &lt;dbl&gt;
 1 -2.41 0.000289
 2 -2.28 0.000893
 3 -2.16 0.00236 
 4 -2.04 0.00531 
 5 -1.91 0.0103  
 6 -1.79 0.0174  
 7 -1.66 0.0259  
 8 -1.54 0.0353  
 9 -1.41 0.0461  
10 -1.29 0.0597  
# ℹ 40 more rows

$data$input_data
$data$input_data$`rnorm(50)`
 [1] -0.28375667 -1.35693228 -0.44459842 -0.44189941 -1.75245474  0.56591611
 [7]  0.64969767  0.43861900 -0.29576390 -1.05083449  0.46476746 -0.09441544
[13]  0.01988715 -0.11553959 -1.06165613 -0.23215249 -0.73166009  0.85851074
[19] -0.23347946 -0.98707523  0.48980891  0.45443754 -0.06019617 -0.28090697
[25]  0.02640269  0.34780762  0.08271394  0.38223602  0.37200374  0.15833057
[31]  0.67451345  0.19271746 -0.76646273 -0.61174894 -0.66437076  0.41119339
[37]  0.94342842  1.79174540 -0.78712893  0.84426079  1.21105485 -1.08434366
[43]  0.34320348  1.51119066 -1.54429610 -0.53518346 -0.12958712  0.40503043
[49]  1.10792452 -0.35614745

$data$input_data$`rbeta(50, 0.5, 0.5)`
 [1] 0.4558286432 0.9722089635 0.8326968294 0.6098241262 0.2693505903
 [6] 0.1769712879 0.2097711862 0.3447365735 0.2255041322 0.0593279079
[11] 0.3605619162 0.7653921143 0.9300206371 0.0649047973 0.8248588302
[16] 0.4746486057 0.0007107969 0.0303139028 0.1092924293 0.9994465190
[21] 0.5447640613 0.6838621697 0.4264545201 0.0350483756 0.1439936791
[26] 0.9999991418 0.9971223513 0.9366023326 0.2380888988 0.3954270399
[31] 0.6355559970 0.0082225336 0.1935222058 0.8301693526 0.0006154042
[36] 0.9468539743 0.5805084634 0.9630710788 0.8536182424 0.0636560268
[41] 0.3383464734 0.8648131947 0.2472292868 0.7353653812 0.6462800353
[46] 0.3418460528 0.8706317638 0.0537689028 0.4028589675 0.5659417332



$plots
$plots$line_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-3.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$plots$dens_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-4.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>

$input_fns
[1] "rnorm(50), rbeta(50, 0.5, 0.5)"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multiplicative interactions</span></span>
<span id="cb17-2">mix_multiplicative <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;">tidy_mixture_density</span>(</span>
<span id="cb17-3">  <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;">50</span>), </span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbeta</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">0.5</span>), </span>
<span id="cb17-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combination_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"multiply"</span></span>
<span id="cb17-6">)</span>
<span id="cb17-7">mix_multiplicative</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$data
$data$dist_tbl
# A tibble: 50 × 2
       x       y
   &lt;int&gt;   &lt;dbl&gt;
 1     1  0.0128
 2     2 -0.352 
 3     3 -0.228 
 4     4  0.0318
 5     5  0.0846
 6     6  0.0148
 7     7  0.301 
 8     8  0.125 
 9     9 -0.196 
10    10  1.58  
# ℹ 40 more rows

$data$dens_tbl
# A tibble: 50 × 2
        x       y
    &lt;dbl&gt;   &lt;dbl&gt;
 1 -1.49  0.00113
 2 -1.42  0.00577
 3 -1.34  0.0217 
 4 -1.27  0.0605 
 5 -1.19  0.127  
 6 -1.12  0.205  
 7 -1.04  0.261  
 8 -0.968 0.272  
 9 -0.893 0.243  
10 -0.818 0.197  
# ℹ 40 more rows

$data$input_data
$data$input_data$`rnorm(50)`
 [1]  0.32370511 -1.21400213 -1.20899806  0.03519770  0.53932734  0.02665457
 [7]  0.41845197  0.14043657 -0.55713865  1.68772225  0.33995465 -1.38935947
[13]  0.74516592 -0.04187743 -1.86673573  0.14417007 -0.44909386 -0.46806361
[19] -1.36766494  0.19793102  0.85812595  0.38882190 -1.00188631  0.43322473
[25]  0.43850846  0.84118862  1.63111722 -0.80401369 -0.96695329  0.44273011
[31]  0.18966768  0.18008685  0.47594963  2.67993093  0.56240726 -0.68272322
[37]  2.07827084  2.87539786  0.07352364 -0.59474395  0.54737811  0.70341946
[43] -0.46216676 -1.04391929 -0.53857891  0.60391106 -1.06072413  0.36132956
[49] -1.24601342 -0.67495126

$data$input_data$`rbeta(50, 0.5, 0.5)`
 [1] 0.03961826 0.28965374 0.18885811 0.90263774 0.15690042 0.55506535
 [7] 0.71961247 0.88995670 0.35266007 0.93680182 0.79821917 0.79099196
[13] 0.47833454 0.88224189 0.06153885 0.96803949 0.16677721 0.08155236
[19] 0.78180736 0.89987410 0.06377253 0.99049015 0.92083908 0.81639438
[25] 0.11599055 0.89253034 0.99961689 0.06746365 0.94993344 0.93847486
[31] 0.04346734 0.97125746 0.01042851 0.07114067 0.04258062 0.31699967
[37] 0.57796230 0.62059110 0.28030112 0.95068023 0.21437376 0.46588256
[43] 0.60081481 0.98127380 0.03041642 0.67000409 0.72883674 0.26381406
[49] 0.43238751 0.07012001



$plots
$plots$line_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-5.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$plots$dens_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-6.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>

$input_fns
[1] "rnorm(50), rbeta(50, 0.5, 0.5)"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Subtration for differencing</span></span>
<span id="cb21-2">mix_subtract <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;">tidy_mixture_density</span>(</span>
<span id="cb21-3">        <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;">50</span>),</span>
<span id="cb21-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbeta</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">0.5</span>),</span>
<span id="cb21-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combination_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"subtract"</span></span>
<span id="cb21-6">)</span>
<span id="cb21-7">mix_subtract</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$data
$data$dist_tbl
# A tibble: 50 × 2
       x       y
   &lt;int&gt;   &lt;dbl&gt;
 1     1 -0.934 
 2     2  0.0255
 3     3 -0.807 
 4     4  0.639 
 5     5 -0.151 
 6     6  0.261 
 7     7  0.383 
 8     8 -1.06  
 9     9 -1.17  
10    10  0.868 
# ℹ 40 more rows

$data$dens_tbl
# A tibble: 50 × 2
       x        y
   &lt;dbl&gt;    &lt;dbl&gt;
 1 -3.33 0.000267
 2 -3.22 0.000687
 3 -3.10 0.00159 
 4 -2.99 0.00333 
 5 -2.88 0.00633 
 6 -2.76 0.0109  
 7 -2.65 0.0173  
 8 -2.54 0.0252  
 9 -2.43 0.0342  
10 -2.31 0.0442  
# ℹ 40 more rows

$data$input_data
$data$input_data$`rnorm(50)`
 [1]  0.04741171  0.78106971 -0.51649345  0.79333600  0.84698156  0.44755311
 [7]  0.42599173 -0.05861854 -0.17167764  1.86283568 -0.34825311  1.15277765
[13] -0.43388248 -0.44234758  0.18575088 -1.14766103 -1.00124274 -1.29743634
[19]  0.04227262  1.88358997 -1.03996542  0.01229659  0.54674453  0.78417878
[25] -0.68734596  1.46836234  1.17552232  0.21217058  0.58419970  1.79239641
[31] -0.15530648  0.77885429  1.54672370  1.11665693  0.35566983  0.52467994
[37]  0.30117165 -0.38017897 -0.35182655 -0.50842405  1.54094057  0.01395280
[43] -0.56581282 -0.36566571  0.98543508 -0.48095752 -0.08275619 -0.53918661
[49] -0.51094105  0.65497036

$data$input_data$`rbeta(50, 0.5, 0.5)`
 [1] 0.981019342 0.755544020 0.290997064 0.153894509 0.997501146 0.187018584
 [7] 0.042874119 0.997810266 0.998535547 0.994524920 0.049740075 0.053181972
[13] 0.009592371 0.127241021 0.385442338 0.141428843 0.966020263 0.998885522
[19] 0.194088533 0.709788033 0.479590987 0.346260596 0.958567049 0.796353667
[25] 0.928775994 0.981901862 0.241413100 0.061032775 0.789884132 0.895207272
[31] 0.011005119 0.931750374 0.761540209 0.632937614 0.959700133 0.065693893
[37] 0.654884437 0.980215158 0.887362439 0.347511106 0.982633970 0.108274936
[43] 0.898851715 0.602077489 0.104709882 0.286286952 0.181128702 0.670655445
[49] 0.287199855 0.910472770



$plots
$plots$line_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-7.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$plots$dens_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-8.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>

$input_fns
[1] "rnorm(50), rbeta(50, 0.5, 0.5)"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Division for ratios</span></span>
<span id="cb25-2">mix_divide <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;">tidy_mixture_density</span>(</span>
<span id="cb25-3">        <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;">50</span>),</span>
<span id="cb25-4">        <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rbeta</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</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;">0.5</span>),</span>
<span id="cb25-5">        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.combination_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"divide"</span></span>
<span id="cb25-6">)</span>
<span id="cb25-7">mix_divide</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$data
$data$dist_tbl
# A tibble: 50 × 2
       x         y
   &lt;int&gt;     &lt;dbl&gt;
 1     1     3.00 
 2     2    -1.79 
 3     3     0.603
 4     4     1.36 
 5     5 25721.   
 6     6  9101.   
 7     7     1.80 
 8     8    -0.620
 9     9    -0.698
10    10     1.73 
# ℹ 40 more rows

$data$dens_tbl
# A tibble: 50 × 2
        x        y
    &lt;dbl&gt;    &lt;dbl&gt;
 1 -445.  6.46e- 3
 2   89.1 5.56e- 4
 3  623.  2.49e-20
 4 1157.  3.46e-20
 5 1691.  5.86e-19
 6 2225.  1.04e-19
 7 2759.  1.06e-18
 8 3293.  0       
 9 3827.  0       
10 4362.  0       
# ℹ 40 more rows

$data$input_data
$data$input_data$`rnorm(50)`
 [1]  1.14765189 -0.65037474  0.56076429  0.14529634  2.07643060  1.35312739
 [7]  1.36098704 -0.38382420 -0.60398679  1.72618260  1.02326275  0.53904945
[13]  0.58427997 -1.24784343  0.54468193  0.23675504 -0.06278437 -0.19938179
[19]  0.29774594 -1.73700726 -0.95278639  1.50377661  0.76641470 -1.64577160
[25] -1.87538645  0.20415661 -0.02288698  0.03017884 -1.11147919 -0.53853737
[31]  0.58745346  1.53857208 -0.71316156  0.30820280  1.12513966 -1.22796997
[37] -0.43473722 -1.17160252 -1.49085069 -0.97810140  0.77343726 -0.27780074
[43]  0.10606935  1.18324993 -0.66469916  0.77692003 -1.72619510  0.12750687
[49] -0.63319646 -0.44339251

$data$input_data$`rbeta(50, 0.5, 0.5)`
 [1] 3.820203e-01 3.628845e-01 9.300254e-01 1.067735e-01 8.072951e-05
 [6] 1.486745e-04 7.576313e-01 6.194406e-01 8.654029e-01 9.957012e-01
[11] 4.249757e-01 1.018361e-01 9.576092e-01 9.578569e-03 1.354495e-03
[16] 9.547469e-01 9.999004e-01 6.847354e-03 2.532576e-01 8.101000e-01
[21] 9.979821e-01 7.362087e-01 1.718800e-01 1.436152e-01 7.798669e-01
[26] 9.892079e-01 5.585185e-01 8.395558e-01 2.514851e-03 6.805656e-01
[31] 9.669103e-01 1.653675e-04 7.284081e-01 9.800184e-01 9.538534e-02
[36] 8.859544e-01 9.990685e-01 1.933901e-01 9.998451e-01 5.631759e-01
[41] 4.015837e-02 3.758858e-01 9.970344e-01 6.033153e-01 5.793981e-01
[46] 2.345981e-03 9.560933e-01 7.753198e-01 2.004754e-01 7.424075e-01



$plots
$plots$line_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-9.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$plots$dens_plot</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/index_files/figure-html/unnamed-chunk-2-10.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>

$input_fns
[1] "rnorm(50), rbeta(50, 0.5, 0.5)"</code></pre>
</div>
</div>
<p>Each combination returns comprehensive output including:</p>
<ul>
<li><strong>Tidy data tables</strong> with combined distributions</li>
<li><strong>Density estimates</strong> for visualization</li>
<li><strong>Ready-to-use plots</strong> for immediate analysis</li>
<li><strong>Input function metadata</strong> for reproducibility</li>
</ul>
</section>
</section>
<section id="best-practices-and-recommendations" class="level1">
<h1>Best Practices and Recommendations</h1>
<section id="for-existing-users" class="level2">
<h2 class="anchored" data-anchor-id="for-existing-users"><strong>For Existing Users</strong></h2>
<ul>
<li><strong>Gradually migrate</strong> critical workflows after thorough testing</li>
<li><strong>Document any code</strong> that depends on exact quantile_normalize() outputs</li>
<li><strong>Leverage new mixture modeling</strong> for more sophisticated statistical modeling</li>
<li><strong>Test downstream analyses</strong> to ensure compatibility</li>
</ul>
</section>
<section id="for-new-users" class="level2">
<h2 class="anchored" data-anchor-id="for-new-users"><strong>For New Users</strong></h2>
<ul>
<li><strong>Start with v1.5.2</strong> to benefit from performance improvements immediately</li>
<li><strong>Explore mixture modeling capabilities</strong> for creative statistical applications</li>
<li><strong>Use in tidyverse pipelines</strong> for seamless data science workflows</li>
</ul>
</section>
</section>
<section id="looking-forward" class="level1">
<h1>Looking Forward</h1>
<p>TidyDensity 1.5.2 represents a significant evolution in the package’s capabilities. The performance improvements in <code>quantile_normalize()</code> make it more suitable for large-scale data science applications, while the enhanced <code>tidy_mixture_density()</code> opens new possibilities for sophisticated statistical modeling.</p>
<p>The breaking changes, though initially challenging, position the package for better scalability and more efficient memory usage, crucial factors for modern data science workflows.</p>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p><strong>TidyDensity 1.5.2</strong> delivers substantial improvements that will benefit R programmers working with statistical distributions. The 48.6% performance improvement in <code>quantile_normalize()</code> and flexible mixture modeling capabilities make this update highly valuable, despite the breaking changes.</p>
<p><strong>Key takeaways:</strong></p>
<ul>
<li>✅ Significant performance gains across all dataset sizes</li>
<li>✅ Enhanced mixture modeling with five combination types</li>
<li>✅ Preserved statistical properties in quantile normalization</li>
<li>⚠️ Breaking changes require testing of existing workflows</li>
<li>🚀 Improved scalability for large-scale data science applications</li>
</ul>
<p><strong>Ready to upgrade?</strong> Update to TidyDensity 1.5.2 and test your critical workflows to ensure compatibility. The performance benefits and new capabilities make this update well worth the migration effort.</p>
<blockquote class="blockquote">
<p>💡 <strong>What’s your experience with TidyDensity 1.5.2?</strong></p>
</blockquote>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-08/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>TidyDensity Update</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/<span class="citation" data-cites="stevensanderson">@stevensanderson</span></a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/<span class="citation" data-cites="spsanderson">@spsanderson</span></a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: <em>Extending Excel with Python and R</em> here</em>: <a href="https://packt.link/oTyZJ">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-08/</guid>
  <pubDate>Mon, 08 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>The Beginner’s Guide to Web Scraping in Python: From Zero to Web Data Hero</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-03/</link>
  <description><![CDATA[ 






<section id="authors-note" class="level2">
<h2 class="anchored" data-anchor-id="authors-note">Author’s Note</h2>
<blockquote class="blockquote">
<p><strong>Learning Together:</strong> Hey there! I want to be completely honest with you from the start. I’m learning web scraping as I write this series, which means we’re on this journey together. My goal isn’t to pretend I’m an expert, but rather to share what I discover in the clearest, most beginner friendly way possible. Every example in this guide has been tested to ensure it works, and I’ll explain every piece of code like I’m talking to a friend who’s never seen Python before. Let’s dive in!</p>
</blockquote>
<hr>
</section>
<section id="introduction-what-is-web-scraping-and-why-should-you-care" class="level1">
<h1>Introduction: What Is Web Scraping and Why Should You Care?</h1>
<p><strong>Web scraping</strong> is like having a super-powered copy-and-paste tool for the internet. Instead of manually visiting websites and copying information by hand, you can write Python programs that automatically visit web pages, extract the data you need, and organize it for you .</p>
<p>Think of it this way: if you wanted to collect product prices from 100 different online stores, you could spend days clicking through websites, or you could write a 20-line Python script that does it in minutes.</p>
<blockquote class="blockquote">
<p><strong>Key Insight:</strong> Web scraping transforms the entire internet into your personal database, accessible through Python code.</p>
</blockquote>
</section>
<section id="understanding-the-python-web-scraping-ecosystem" class="level1">
<h1>Understanding the Python Web Scraping Ecosystem</h1>
<p>Before we start coding, let’s understand the tools in our toolkit. Python offers several libraries for web scraping, each with its own strengths and use cases.</p>
<section id="the-big-three-requests-beautifulsoup-and-selenium" class="level2">
<h2 class="anchored" data-anchor-id="the-big-three-requests-beautifulsoup-and-selenium">The Big Three: Requests, BeautifulSoup, and Selenium</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 22%">
<col style="width: 36%">
</colgroup>
<thead>
<tr class="header">
<th>Library</th>
<th>Purpose</th>
<th>Best For</th>
<th>Learning Curve</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>requests</strong></td>
<td>Fetches web pages</td>
<td>Static content, APIs</td>
<td>Easy</td>
</tr>
<tr class="even">
<td><strong>BeautifulSoup</strong></td>
<td>Parses HTML</td>
<td>Simple HTML extraction</td>
<td>Easy</td>
</tr>
<tr class="odd">
<td><strong>Selenium</strong></td>
<td>Controls browsers</td>
<td>Dynamic content, JavaScript</td>
<td>Moderate</td>
</tr>
</tbody>
</table>
<section id="what-about-the-webbrowser-module" class="level3">
<h3 class="anchored" data-anchor-id="what-about-the-webbrowser-module">What About the <code>webbrowser</code> Module?</h3>
<p>You might have heard about Python’s <code>webbrowser</code> module, but here’s the thing: <strong>it’s not actually for scraping</strong> . The <code>webbrowser</code> module simply opens URLs in your default browser - it can’t extract or process data. Think of it as Python’s way of saying “Hey browser, open this page for the human to look at.”</p>
</section>
</section>
</section>
<section id="setting-up-your-web-scraping-environment" class="level1">
<h1>Setting Up Your Web Scraping Environment</h1>
<p>Before we can start scraping, we need to install our tools. Open your terminal or command prompt and run:</p>
<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="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install requests beautifulsoup4</span></code></pre></div>
<p>For Selenium (we’ll cover this later):</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install selenium</span></code></pre></div>
</section>
<section id="your-first-web-scraping-script-static-content" class="level1">
<h1>Your First Web Scraping Script: Static Content</h1>
<p>Let’s start with the simplest possible example. We’ll scrape a basic webpage and extract some information.</p>
<section id="step-by-step-breakdown" class="level2">
<h2 class="anchored" data-anchor-id="step-by-step-breakdown">Step-by-Step Breakdown</h2>
<div id="0b4e1643" class="cell" data-execution_count="1">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> requests</span>
<span id="cb3-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> bs4 <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> BeautifulSoup</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Send HTTP request to get web page</span></span>
<span id="cb3-5">url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://example.com"</span></span>
<span id="cb3-6">response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> requests.get(url)</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Check if request was successful</span></span>
<span id="cb3-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> response.status_code <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>:</span>
<span id="cb3-10">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✓ Successfully fetched the page!"</span>)</span>
<span id="cb3-11">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Content length: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(response.text)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> characters"</span>)</span>
<span id="cb3-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb3-13">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"✗ Failed to fetch page. Status code: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>response<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>status_code<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-14"></span>
<span id="cb3-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: Parse HTML with BeautifulSoup</span></span>
<span id="cb3-16">soup <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BeautifulSoup(response.text, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'html.parser'</span>)</span>
<span id="cb3-17"></span>
<span id="cb3-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: Extract data</span></span>
<span id="cb3-19">title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soup.find(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>).get_text()</span>
<span id="cb3-20"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Page title: </span><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><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb3-21"></span>
<span id="cb3-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find all paragraphs</span></span>
<span id="cb3-23">paragraphs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soup.find_all(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>)</span>
<span id="cb3-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Found </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(paragraphs)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> paragraph(s):"</span>)</span>
<span id="cb3-25"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(paragraphs, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb3-26">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"  </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">. </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>get_text()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>strip()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>✓ Successfully fetched the page!
Content length: 1256 characters
Page title: Example Domain
Found 2 paragraph(s):
  1. This domain is for use in illustrative examples in documents. You may use this
    domain in literature without prior coordination or asking for permission.
  2. More information...</code></pre>
</div>
</div>
</section>
<section id="function-explanations-in-simple-terms" class="level2">
<h2 class="anchored" data-anchor-id="function-explanations-in-simple-terms">Function Explanations (In Simple Terms)</h2>
<ul>
<li><strong><code>requests.get(url)</code></strong>: Think of this as knocking on a website’s door and asking for its content</li>
<li><strong><code>response.status_code</code></strong>: The website’s response - 200 means “sure, here’s the page!”</li>
<li><strong><code>BeautifulSoup(html, 'html.parser')</code></strong>: Takes messy HTML and organizes it so we can easily find things</li>
<li><strong><code>soup.find('title')</code></strong>: Looks for the first <code>&lt;title&gt;</code> tag on the page</li>
<li><strong><code>soup.find_all('p')</code></strong>: Finds ALL <code>&lt;p&gt;</code> (paragraph) tags on the page</li>
<li><strong><code>.get_text()</code></strong>: Extracts just the text content, ignoring HTML tags</li>
</ul>
</section>
</section>
<section id="mastering-beautifulsoup-different-ways-to-find-elements" class="level1">
<h1>Mastering BeautifulSoup: Different Ways to Find Elements</h1>
<p>BeautifulSoup gives you multiple ways to find HTML elements. Here’s a comparison of the most common methods:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 20%">
<col style="width: 20%">
<col style="width: 37%">
<col style="width: 22%">
</colgroup>
<thead>
<tr class="header">
<th>Method</th>
<th>Syntax</th>
<th>What It Finds</th>
<th>Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>By Tag</strong></td>
<td><code>soup.find("tag")</code></td>
<td>First element with that tag</td>
<td><code>soup.find("title")</code></td>
</tr>
<tr class="even">
<td><strong>By ID</strong></td>
<td><code>soup.find("tag", id="id-name")</code></td>
<td>Element with specific ID</td>
<td><code>soup.find("h1", id="main-title")</code></td>
</tr>
<tr class="odd">
<td><strong>By Class</strong></td>
<td><code>soup.find("tag", class_="class-name")</code></td>
<td>Element with specific CSS class</td>
<td><code>soup.find("p", class_="intro")</code></td>
</tr>
<tr class="even">
<td><strong>CSS Selectors</strong></td>
<td><code>soup.select_one("css-selector")</code></td>
<td>First element matching CSS selector</td>
<td><code>soup.select_one(".footer a")</code></td>
</tr>
<tr class="odd">
<td><strong>Find All</strong></td>
<td><code>soup.find_all("tag")</code></td>
<td>ALL elements with that tag</td>
<td><code>soup.find_all("li", class_="item")</code></td>
</tr>
</tbody>
</table>
<section id="practical-example-multiple-selection-methods" class="level2">
<h2 class="anchored" data-anchor-id="practical-example-multiple-selection-methods">Practical Example: Multiple Selection Methods</h2>
<div id="d31508f3" class="cell" data-execution_count="2">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample HTML structure</span></span>
<span id="cb5-2">html_content <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb5-3"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;html&gt;</span></span>
<span id="cb5-4"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;body&gt;</span></span>
<span id="cb5-5"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;h1 id="main-title"&gt;Welcome to Web Scraping&lt;/h1&gt;</span></span>
<span id="cb5-6"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;p class="intro"&gt;This is an introduction.&lt;/p&gt;</span></span>
<span id="cb5-7"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;ul&gt;</span></span>
<span id="cb5-8"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        &lt;li class="item"&gt;Item 1&lt;/li&gt;</span></span>
<span id="cb5-9"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        &lt;li class="item featured"&gt;Item 2 (Featured)&lt;/li&gt;</span></span>
<span id="cb5-10"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">        &lt;li class="item"&gt;Item 3&lt;/li&gt;</span></span>
<span id="cb5-11"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">    &lt;/ul&gt;</span></span>
<span id="cb5-12"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/body&gt;</span></span>
<span id="cb5-13"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">&lt;/html&gt;</span></span>
<span id="cb5-14"><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb5-15"></span>
<span id="cb5-16">soup <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BeautifulSoup(html_content, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'html.parser'</span>)</span>
<span id="cb5-17"></span>
<span id="cb5-18"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Different ways to extract data</span></span>
<span id="cb5-19">title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soup.find(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'h1'</span>).get_text()                    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Welcome to Web Scraping"</span></span>
<span id="cb5-20">intro <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soup.find(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'p'</span>, class_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'intro'</span>).get_text()     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "This is an introduction."</span></span>
<span id="cb5-21">featured <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soup.find(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'li'</span>, class_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'item featured'</span>).get_text()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># "Item 2 (Featured)"</span></span>
<span id="cb5-22">all_items <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [li.get_text() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> li <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> soup.find_all(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'li'</span>)]      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># List of all items</span></span>
<span id="cb5-23"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(featured)</span>
<span id="cb5-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(all_items)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Item 2 (Featured)
['Item 1', 'Item 2 (Featured)', 'Item 3']</code></pre>
</div>
</div>
</section>
</section>
<section id="when-static-scraping-isnt-enough-enter-selenium" class="level1">
<h1>When Static Scraping Isn’t Enough: Enter Selenium</h1>
<p>Some websites load their content using JavaScript after the initial page loads. This is called <strong>dynamic content</strong>. When <code>requests</code> and <code>BeautifulSoup</code> visit these pages, they only see the empty shell - not the data that gets filled in later.</p>
<p>This is where <strong>Selenium</strong> comes in. Selenium actually opens a real web browser and can wait for JavaScript to run.</p>
<section id="when-to-use-each-tool" class="level2">
<h2 class="anchored" data-anchor-id="when-to-use-each-tool">When to Use Each Tool</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 29%">
<col style="width: 38%">
<col style="width: 32%">
</colgroup>
<thead>
<tr class="header">
<th>Scenario</th>
<th>Tool Choice</th>
<th>Reasoning</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Static HTML pages</strong></td>
<td>requests + BeautifulSoup</td>
<td>Faster and more efficient</td>
</tr>
<tr class="even">
<td><strong>JavaScript-heavy sites</strong></td>
<td>Selenium</td>
<td>Can execute JavaScript</td>
</tr>
<tr class="odd">
<td><strong>Need to interact</strong> (click, scroll, forms)</td>
<td>Selenium</td>
<td>Full browser control</td>
</tr>
<tr class="even">
<td><strong>Large-scale scraping</strong></td>
<td>requests + BeautifulSoup</td>
<td>Better performance</td>
</tr>
<tr class="odd">
<td><strong>Sites behind login</strong></td>
<td>Either (with sessions)</td>
<td>Depends on complexity</td>
</tr>
</tbody>
</table>
</section>
<section id="basic-selenium-example" class="level2">
<h2 class="anchored" data-anchor-id="basic-selenium-example">Basic Selenium Example</h2>
<div id="59bef735" class="cell" data-execution_count="3">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> selenium <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> webdriver</span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> selenium.webdriver.common.by <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> By</span>
<span id="cb7-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> selenium.webdriver.chrome.options <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> Options</span>
<span id="cb7-4"></span>
<span id="cb7-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Setup Chrome to run in the background (headless)</span></span>
<span id="cb7-6">chrome_options <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> Options()</span>
<span id="cb7-7">chrome_options.add_argument(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"--headless"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Run without opening browser window</span></span>
<span id="cb7-8"></span>
<span id="cb7-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create WebDriver instance</span></span>
<span id="cb7-10">driver <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> webdriver.Chrome(options<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>chrome_options)</span>
<span id="cb7-11"></span>
<span id="cb7-12"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb7-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Navigate to webpage</span></span>
<span id="cb7-14">    driver.get(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://example.com"</span>)</span>
<span id="cb7-15">    </span>
<span id="cb7-16">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Wait for page to load (implicit wait)</span></span>
<span id="cb7-17">    driver.implicitly_wait(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb7-18">    </span>
<span id="cb7-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find elements</span></span>
<span id="cb7-20">    title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> driver.find_element(By.TAG_NAME, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"h1"</span>).text</span>
<span id="cb7-21">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Page title: </span><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><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-22">    </span>
<span id="cb7-23">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find multiple elements</span></span>
<span id="cb7-24">    paragraphs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> driver.find_elements(By.TAG_NAME, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"p"</span>)</span>
<span id="cb7-25">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> p <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> paragraphs:</span>
<span id="cb7-26">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Paragraph: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>p<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>text<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb7-27">        </span>
<span id="cb7-28"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">finally</span>:</span>
<span id="cb7-29">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Always close the browser</span></span>
<span id="cb7-30">    driver.quit()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Page title: Example Domain
Paragraph: This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.
Paragraph: More information...</code></pre>
</div>
</div>
<p><strong>Selenium Function Explanations:</strong></p>
<ul>
<li><strong><code>webdriver.Chrome()</code></strong>: Starts a Chrome browser that Python can control</li>
<li><strong><code>driver.get(url)</code></strong>: Tells the browser to navigate to a specific webpage</li>
<li><strong><code>driver.find_element(By.TAG_NAME, "h1")</code></strong>: Finds the first <code>&lt;h1&gt;</code> element on the page</li>
<li><strong><code>driver.quit()</code></strong>: Closes the browser (very important - don’t leave browsers running!)</li>
</ul>
</section>
</section>
<section id="handling-common-challenges-the-reality-of-web-scraping" class="level1">
<h1>Handling Common Challenges: The Reality of Web Scraping</h1>
<p>Web scraping isn’t always smooth sailing. Here are the most common challenges you’ll face and how to handle them:</p>
<section id="challenge-solutions-table" class="level2">
<h2 class="anchored" data-anchor-id="challenge-solutions-table">Challenge Solutions Table</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 20%">
<col style="width: 22%">
<col style="width: 31%">
</colgroup>
<thead>
<tr class="header">
<th>Challenge</th>
<th>Problem</th>
<th>Solution</th>
<th>Code Example</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Rate Limiting</strong></td>
<td>Server blocks rapid requests</td>
<td>Add delays</td>
<td><code>time.sleep(1)</code></td>
</tr>
<tr class="even">
<td><strong>Bot Detection</strong></td>
<td>Server detects automated requests</td>
<td>Use realistic headers</td>
<td><code>headers = {'User-Agent': 'Mozilla/5.0...'}</code></td>
</tr>
<tr class="odd">
<td><strong>Dynamic Content</strong></td>
<td>Data loads via JavaScript</td>
<td>Use Selenium</td>
<td><code>driver.get(url)</code></td>
</tr>
<tr class="even">
<td><strong>Session Management</strong></td>
<td>Need to stay logged in</td>
<td>Use requests.Session()</td>
<td><code>session = requests.Session()</code></td>
</tr>
<tr class="odd">
<td><strong>Changing Structure</strong></td>
<td>Website layout changes</td>
<td>Use multiple selectors</td>
<td><code>soup.find('h1') or soup.find('h2')</code></td>
</tr>
</tbody>
</table>
</section>
<section id="robust-scraping-with-error-handling" class="level2">
<h2 class="anchored" data-anchor-id="robust-scraping-with-error-handling">Robust Scraping with Error Handling</h2>
<p>Here’s a more professional scraping function that handles errors gracefully:</p>
<div id="3d74338e" class="cell" data-execution_count="4">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> requests</span>
<span id="cb9-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> bs4 <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> BeautifulSoup</span>
<span id="cb9-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb9-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> random</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> robust_scrape(url, max_retries<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, delay_range<span class="op" 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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)):</span>
<span id="cb9-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""</span></span>
<span id="cb9-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    A robust scraping function with error handling</span></span>
<span id="cb9-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb9-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Args:</span></span>
<span id="cb9-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        url (str): Website URL to scrape</span></span>
<span id="cb9-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        max_retries (int): How many times to retry if something fails</span></span>
<span id="cb9-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        delay_range (tuple): Random delay between requests (min, max seconds)</span></span>
<span id="cb9-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    </span></span>
<span id="cb9-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    Returns:</span></span>
<span id="cb9-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">        BeautifulSoup object or None if failed</span></span>
<span id="cb9-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">    """</span></span>
<span id="cb9-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Headers to look like a real browser</span></span>
<span id="cb9-19">    headers <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> {</span>
<span id="cb9-20">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'User-Agent'</span>: <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'</span></span>
<span id="cb9-21">    }</span>
<span id="cb9-22">    </span>
<span id="cb9-23">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> attempt <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(max_retries):</span>
<span id="cb9-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-25">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Random delay to seem human-like</span></span>
<span id="cb9-26">            delay <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> random.uniform(<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span>delay_range)</span>
<span id="cb9-27">            time.sleep(delay)</span>
<span id="cb9-28">            </span>
<span id="cb9-29">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Make the request</span></span>
<span id="cb9-30">            response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> requests.get(url, headers<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>headers, timeout<span class="op" 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="cb9-31">            response.raise_for_status()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Raises error for bad status codes</span></span>
<span id="cb9-32">            </span>
<span id="cb9-33">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Parse and return</span></span>
<span id="cb9-34">            soup <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BeautifulSoup(response.text, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'html.parser'</span>)</span>
<span id="cb9-35">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> soup</span>
<span id="cb9-36">            </span>
<span id="cb9-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> requests.exceptions.Timeout:</span>
<span id="cb9-38">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Attempt </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>attempt <span class="op" 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><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: Request timed out"</span>)</span>
<span id="cb9-39">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> requests.exceptions.RequestException <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb9-40">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Attempt </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>attempt <span class="op" 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><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: Request error: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb9-41">        </span>
<span id="cb9-42">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> attempt <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> max_retries <span class="op" 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="cb9-43">            wait_time <span class="op" 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="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">**</span> attempt  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Wait longer each time (1s, 2s, 4s)</span></span>
<span id="cb9-44">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Waiting </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>wait_time<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> seconds before retry..."</span>)</span>
<span id="cb9-45">            time.sleep(wait_time)</span>
<span id="cb9-46">    </span>
<span id="cb9-47">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All retry attempts failed"</span>)</span>
<span id="cb9-48">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb9-49"></span>
<span id="cb9-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Usage example</span></span>
<span id="cb9-51">soup <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> robust_scrape(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://example.com"</span>)</span>
<span id="cb9-52"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> soup:</span>
<span id="cb9-53">    title <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soup.find(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'title'</span>).get_text()</span>
<span id="cb9-54">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Successfully scraped: </span><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><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb9-55"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb9-56">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Scraping failed after all retries"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Successfully scraped: Example Domain</code></pre>
</div>
</div>
</section>
</section>
<section id="best-practices-and-ethical-considerations" class="level1">
<h1>Best Practices and Ethical Considerations</h1>
<p>Web scraping comes with great power and great responsibility. Here are the essential guidelines every scraper should follow:</p>
<section id="technical-best-practices" class="level2">
<h2 class="anchored" data-anchor-id="technical-best-practices">Technical Best Practices</h2>
<ul>
<li><strong>Always check robots.txt</strong> before scraping (visit website.com/robots.txt)</li>
<li><strong>Add delays between requests</strong> to avoid overwhelming servers</li>
<li><strong>Use proper User-Agent headers</strong> to identify your scraper honestly</li>
<li><strong>Handle errors gracefully</strong> with try/except blocks</li>
<li><strong>Validate and clean your data</strong> after extraction</li>
<li><strong>Close browser instances</strong> when using Selenium (use <code>driver.quit()</code>)</li>
</ul>
</section>
<section id="ethical-guidelines" class="level2">
<h2 class="anchored" data-anchor-id="ethical-guidelines">Ethical Guidelines</h2>
<ul>
<li><strong>Respect website Terms of Service</strong> - read them before scraping</li>
<li><strong>Don’t scrape personal or private data</strong> without permission</li>
<li><strong>Use official APIs when available</strong> - they’re usually better than scraping</li>
<li><strong>Give attribution</strong> when using scraped data in your projects</li>
<li><strong>Be transparent</strong> about your scraping activities if asked</li>
<li><strong>Don’t overload servers</strong> - be respectful of website resources</li>
</ul>
</section>
<section id="legal-considerations" class="level2">
<h2 class="anchored" data-anchor-id="legal-considerations">Legal Considerations</h2>
<p><strong>Important:</strong> This is not legal advice, but here are some general principles:</p>
<ul>
<li>Scraping publicly available data is generally okay</li>
<li>Always respect copyright and intellectual property rights</li>
<li>Be extra careful with personal data due to privacy laws (GDPR, CCPA)</li>
<li>When in doubt, contact the website owner for permission</li>
</ul>
</section>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p>Now it’s your turn to practice! Here’s a hands-on exercise to reinforce what you’ve learned.</p>
<p><strong>Challenge:</strong> Create a script that scrapes quotes from a test website and saves them to a text file.</p>
<p><strong>Your Task:</strong></p>
<ol type="1">
<li>Visit <code>https://quotes.toscrape.com/</code> (a site designed for scraping practice)</li>
<li>Extract the first 5 quotes on the page</li>
<li>For each quote, get the text, author, and tags</li>
<li>Save the results to a text file</li>
</ol>
<p><strong>Starter Code:</strong></p>
<div class="sourceCode" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> requests</span>
<span id="cb11-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> bs4 <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> BeautifulSoup</span>
<span id="cb11-3"></span>
<span id="cb11-4">url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://quotes.toscrape.com/"</span></span>
<span id="cb11-5"></span>
<span id="cb11-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here!</span></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Hint: Look for &lt;div class="quote"&gt; elements</span></span>
<span id="cb11-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Each quote has text in &lt;span class="text"&gt;</span></span>
<span id="cb11-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Authors are in &lt;small class="author"&gt;</span></span>
<span id="cb11-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Tags are in &lt;div class="tags"&gt; with &lt;a&gt; elements</span></span></code></pre></div>
<details>
<summary>
Click here for Solution!
</summary>
<div id="570a204e" class="cell" data-execution_count="5">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> requests</span>
<span id="cb12-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> bs4 <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> BeautifulSoup</span>
<span id="cb12-3"></span>
<span id="cb12-4"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> scrape_quotes():</span>
<span id="cb12-5">    url <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://quotes.toscrape.com/"</span></span>
<span id="cb12-6">    </span>
<span id="cb12-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Fetch the page</span></span>
<span id="cb12-8">    response <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> requests.get(url)</span>
<span id="cb12-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> response.status_code <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>:</span>
<span id="cb12-10">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Failed to fetch the page"</span>)</span>
<span id="cb12-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span></span>
<span id="cb12-12">    </span>
<span id="cb12-13">    soup <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BeautifulSoup(response.text, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'html.parser'</span>)</span>
<span id="cb12-14">    </span>
<span id="cb12-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Find all quote containers</span></span>
<span id="cb12-16">    quotes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> soup.find_all(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'div'</span>, class_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'quote'</span>)</span>
<span id="cb12-17">    </span>
<span id="cb12-18">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract data from first 5 quotes</span></span>
<span id="cb12-19">    scraped_quotes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb12-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> quote <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> quotes[:<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>]:</span>
<span id="cb12-21">        text <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote.find(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'span'</span>, class_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>).get_text()</span>
<span id="cb12-22">        author <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> quote.find(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'small'</span>, class_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'author'</span>).get_text()</span>
<span id="cb12-23">        tags <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [tag.get_text() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> tag <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> quote.find_all(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'a'</span>, class_<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tag'</span>)]</span>
<span id="cb12-24">        </span>
<span id="cb12-25">        scraped_quotes.append({</span>
<span id="cb12-26">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>: text,</span>
<span id="cb12-27">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'author'</span>: author,</span>
<span id="cb12-28">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tags'</span>: tags</span>
<span id="cb12-29">        })</span>
<span id="cb12-30">    </span>
<span id="cb12-31">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Save to file</span></span>
<span id="cb12-32">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'scraped_quotes.txt'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'w'</span>, encoding<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'utf-8'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> f:</span>
<span id="cb12-33">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, quote <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(scraped_quotes, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>):</span>
<span id="cb12-34">            f.write(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Quote </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">:</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-35">            f.write(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Text: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>quote[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'text'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-36">            f.write(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Author: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>quote[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'author'</span>]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-37">            f.write(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Tags: </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;">', '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>join(quote[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'tags'</span>])<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-38">            f.write(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="ch" style="color: #20794D;
background-color: null;
font-style: inherit;">\n</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb12-39">    </span>
<span id="cb12-40">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Successfully scraped </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(scraped_quotes)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> quotes!"</span>)</span>
<span id="cb12-41">    </span>
<span id="cb12-42"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"__main__"</span>:</span>
<span id="cb12-43">    scrape_quotes()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Successfully scraped 5 quotes!</code></pre>
</div>
</div>
</details>
</section>
<section id="quick-takeaways-your-web-scraping-cheat-sheet" class="level1">
<h1>Quick Takeaways: Your Web Scraping Cheat Sheet</h1>
<p>Here are the key points to remember from this guide:</p>
<ul>
<li><strong>Start Simple</strong>: Begin with <code>requests</code> + <code>BeautifulSoup</code> for static websites</li>
<li><strong>Use Selenium for JavaScript</strong>: Only when content loads dynamically</li>
<li><strong>Always Be Respectful</strong>: Add delays, check robots.txt, follow terms of service</li>
<li><strong>Handle Errors Gracefully</strong>: Use try/except blocks and retry logic</li>
<li><strong>Clean Your Data</strong>: Validate and normalize scraped data</li>
<li><strong>Choose the Right Tool</strong>: Static content = requests; Dynamic content = Selenium</li>
<li><strong>Practice Makes Perfect</strong>: Start with simple sites before tackling complex ones</li>
<li><strong>Stay Ethical</strong>: Respect privacy, copyright, and website policies</li>
</ul>
</section>
<section id="conclusion-your-web-scraping-journey-starts-now" class="level1">
<h1>Conclusion: Your Web Scraping Journey Starts Now</h1>
<p>Congratulations! You’ve just taken your first steps into the powerful world of web scraping with Python. We’ve covered the essential tools (requests, BeautifulSoup, and Selenium), learned how to handle common challenges, and explored the ethical considerations that make you a responsible scraper.</p>
<p>Remember, web scraping is like learning to drive - you start in empty parking lots (simple websites) before tackling busy highways (complex sites). The examples in this guide give you a solid foundation, but the real learning happens when you start building your own projects.</p>
<p><strong>Your Next Steps:</strong></p>
<ol type="1">
<li>Practice with the exercise above</li>
<li>Try scraping your favorite website (responsibly!)<br>
</li>
<li>Explore advanced topics like handling forms and sessions</li>
<li>Build a project that solves a real problem for you</li>
</ol>
<p><strong>Ready to Level Up Your Python Skills?</strong> Start your next web scraping project today, and remember - every expert was once a beginner. You’ve got this! 🚀</p>
<hr>
<p><em>Have questions about web scraping or want to share your first scraping success story? Drop a comment below - I’d love to hear about your journey and help with any challenges you encounter along the way!</em></p>
<hr>
<p>Happy Coding! 🚀</p>
<section id="webscraping-in-python" class="level2">
<h2 class="anchored" data-anchor-id="webscraping-in-python"><img src="https://www.spsanderson.com/steveondata/posts/2025-09-03/todays_post.png" class="img-fluid" alt="Webscraping in Python"></h2>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/<span class="citation" data-cites="stevensanderson">@stevensanderson</span></a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/<span class="citation" data-cites="spsanderson">@spsanderson</span></a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: <em>Extending Excel with Python and R</em> here</em>: <a href="https://packt.link/oTyZJ">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>
</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-03/</guid>
  <pubDate>Wed, 03 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>How to Reset Row Numbers of Data Frame in R: Complete Guide</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-09-01/</link>
  <description><![CDATA[ 






<section id="primary-methods-for-resetting-row-numbers" class="level1">
<h1>Primary Methods for Resetting Row Numbers</h1>
<section id="setting-row-names-to-null-recommended" class="level2">
<h2 class="anchored" data-anchor-id="setting-row-names-to-null-recommended">1. Setting Row Names to NULL (Recommended)</h2>
<p>The <strong>most straightforward and widely recommended</strong> method is setting row names to <code>NULL</code>:</p>
<div class="sourceCode" 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;"># Basic syntax</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(df) <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;">NULL</span></span></code></pre></div>
<p>This approach removes any custom row names and resets them to the default sequence (1, 2, 3, …) . After execution, your data frame will have continuous sequential row numbers starting from 1.</p>
<p><strong>Example:</strong></p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create sample data with non-sequential row names</span></span>
<span id="cb2-2">iris_subset <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> iris[<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;">77</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;">55</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;">6</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>), ]</span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(iris_subset))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "77" "1" "55" "20" "6" "10"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "77" "1"  "55" "20" "6"  "10"</code></pre>
</div>
<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;"># Reset row numbers</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(iris_subset) <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;">NULL</span></span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(iris_subset))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "1" "2" "3" "4" "5" "6"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "3" "4" "5" "6"</code></pre>
</div>
</div>
</section>
<section id="assigning-new-sequential-numbers" class="level2">
<h2 class="anchored" data-anchor-id="assigning-new-sequential-numbers">2. Assigning New Sequential Numbers</h2>
<p>You can explicitly assign a new sequence of numbers to row names:</p>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 2A: Using seq_len()</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(df) <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;">seq_len</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">nrow</span>(df))</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Method 2B: Using range notation</span></span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(df) <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;">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;">nrow</span>(df)</span></code></pre></div>
<p>This method ensures row names are numeric and sequential, particularly useful after subsetting or reordering operations .</p>
</section>
<section id="using-tidyverse-approaches" class="level2">
<h2 class="anchored" data-anchor-id="using-tidyverse-approaches">3. Using Tidyverse Approaches</h2>
<p>While base R methods are most common, tidyverse users have alternative options:</p>
<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;">library</span>(dplyr)</span>
<span id="cb7-2"></span>
<span id="cb7-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Add a sequential ID column</span></span>
<span id="cb7-4">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> df <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">row_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>())</span>
<span id="cb7-5"></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert to tibble (removes row names by default)</span></span>
<span id="cb7-7">df_tibble <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_tibble</span>(df)</span></code></pre></div>
<hr>
</section>
</section>
<section id="common-use-cases-and-scenarios" class="level1">
<h1>Common Use Cases and Scenarios</h1>
<section id="after-filtering-or-subsetting-data" class="level2">
<h2 class="anchored" data-anchor-id="after-filtering-or-subsetting-data">After Filtering or Subsetting Data</h2>
<p><strong>Most frequent scenario:</strong> When rows are filtered, original row numbers are retained, creating non-sequential indices .</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Original data</span></span>
<span id="cb8-2">original_df <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="cb8-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Name =</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;">"Alice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bob"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Charlie"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"David"</span>),</span>
<span id="cb8-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Score =</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;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>),</span>
<span id="cb8-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">stringsAsFactors =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb8-6">)</span>
<span id="cb8-7"></span>
<span id="cb8-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filter data (creates gaps in row numbers)</span></span>
<span id="cb8-9">filtered_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> original_df[original_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Score <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;">80</span>, ]</span>
<span id="cb8-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(filtered_df))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "1" "2" "4"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "4"</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reset row numbers for clean indexing</span></span>
<span id="cb10-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(filtered_df) <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;">NULL</span></span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(filtered_df))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "1" "2" "3"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "3"</code></pre>
</div>
</div>
</section>
<section id="after-removing-duplicates" class="level2">
<h2 class="anchored" data-anchor-id="after-removing-duplicates">After Removing Duplicates</h2>
<p>Duplicate removal often leaves non-sequential row numbers:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample data with duplicates</span></span>
<span id="cb12-2">data_with_dups <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="cb12-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ID =</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;">2</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>),</span>
<span id="cb12-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Value =</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>)</span>
<span id="cb12-5">)</span>
<span id="cb12-6"></span>
<span id="cb12-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove duplicates</span></span>
<span id="cb12-8">unique_data <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;">unique</span>(data_with_dups)</span>
<span id="cb12-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(unique_data))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Non-sequential: "1" "2" "4" "7"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "4" "5" "7"</code></pre>
</div>
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reset for clean presentation</span></span>
<span id="cb14-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(unique_data) <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;">NULL</span></span>
<span id="cb14-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(unique_data))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sequential: "1" "2" "3" "4"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "3" "4" "5"</code></pre>
</div>
</div>
</section>
<section id="after-sorting-or-reordering" class="level2">
<h2 class="anchored" data-anchor-id="after-sorting-or-reordering">After Sorting or Reordering</h2>
<p>Sorting doesn’t automatically update row numbers :</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Student data</span></span>
<span id="cb16-2">students <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="cb16-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Name =</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;">"John"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alice"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Bob"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Carol"</span>),</span>
<span id="cb16-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">GPA =</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;">3.2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.9</span>)</span>
<span id="cb16-5">)</span>
<span id="cb16-6"></span>
<span id="cb16-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sort by GPA (descending)</span></span>
<span id="cb16-8">students_sorted <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> students[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">order</span>(students<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>GPA, <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>
<span id="cb16-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(students_sorted))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows original row numbers: "4" "2" "3" "1"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "4" "2" "3" "1"</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Reset to reflect new order</span></span>
<span id="cb18-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(students_sorted) <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;">NULL</span></span>
<span id="cb18-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(students_sorted))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Clean: "1" "2" "3" "4"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "3" "4"</code></pre>
</div>
</div>
<hr>
</section>
</section>
<section id="advanced-techniques-and-considerations" class="level1">
<h1>Advanced Techniques and Considerations</h1>
<section id="handling-large-data-frames" class="level2">
<h2 class="anchored" data-anchor-id="handling-large-data-frames">Handling Large Data Frames</h2>
<p>For <strong>large datasets</strong>, the performance differences between methods are minimal:</p>
<table class="caption-top table">
<colgroup>
<col style="width: 21%">
<col style="width: 37%">
<col style="width: 40%">
</colgroup>
<thead>
<tr class="header">
<th>Method</th>
<th>Average Time</th>
<th>Best Use Case</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>rownames(df) &lt;- NULL</code></td>
<td>Fastest</td>
<td>General purpose</td>
</tr>
<tr class="even">
<td><code>rownames(df) &lt;- 1:nrow(df)</code></td>
<td>Slightly slower</td>
<td>When explicit numbering needed</td>
</tr>
<tr class="odd">
<td><code>df %&gt;% mutate(row_id = row_number())</code></td>
<td>Moderate</td>
<td>When keeping original structure</td>
</tr>
</tbody>
</table>
</section>
<section id="data-integrity-considerations" class="level2">
<h2 class="anchored" data-anchor-id="data-integrity-considerations">Data Integrity Considerations</h2>
<blockquote class="blockquote">
<p><strong>Important:</strong> Resetting row names can obscure original data structure. Consider keeping original identifiers as separate columns when traceability is important .</p>
</blockquote>
<div class="sourceCode" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Preserve original row information</span></span>
<span id="cb20-2">df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>original_row <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;">rownames</span>(df)</span>
<span id="cb20-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(df) <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;">NULL</span></span></code></pre></div>
<hr>
</section>
</section>
<section id="potential-issues-and-edge-cases" class="level1">
<h1>Potential Issues and Edge Cases</h1>
<section id="confusion-between-row-names-vs.-row-numbers" class="level2">
<h2 class="anchored" data-anchor-id="confusion-between-row-names-vs.-row-numbers">1. Confusion Between Row Names vs.&nbsp;Row Numbers</h2>
<p><strong>Critical distinction:</strong> Row names are labels, while row numbers indicate position .</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># After subsetting</span></span>
<span id="cb21-2">subset_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> original_df[<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;">4</span>), ]</span>
<span id="cb21-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(subset_df))     <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Row names: "1" "4"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "4"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(subset_df[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, ])          <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Accesses second row (originally row 4)</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   Name Score
4 David    88</code></pre>
</div>
</div>
</section>
<section id="non-unique-row-names-error" class="level2">
<h2 class="anchored" data-anchor-id="non-unique-row-names-error">2. Non-Unique Row Names Error</h2>
<p>Attempting to assign duplicate values as row names fails:</p>
<div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This will cause an error</span></span>
<span id="cb25-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(df) <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;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Error: duplicate 'row.names' are not allowed</span></span></code></pre></div>
</section>
<section id="na-values-in-row-names" class="level2">
<h2 class="anchored" data-anchor-id="na-values-in-row-names">3. NA Values in Row Names</h2>
<p>Row names cannot be NA or missing:</p>
<div class="sourceCode" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This will cause an error</span></span>
<span id="cb26-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">try</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(df) <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Error: missing values not allowed</span></span></code></pre></div>
<hr>
</section>
</section>
<section id="your-turn" class="level1">
<h1>Your Turn!</h1>
<p><strong>Practice Exercise:</strong> Create a data frame, filter it to create non-sequential row numbers, then reset them using different methods.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Create sample data</span></span>
<span id="cb27-2">practice_df <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="cb27-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Product =</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>),</span>
<span id="cb27-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Price =</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;">10</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;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</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;">35</span>),</span>
<span id="cb27-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Category =</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;">"X"</span>, <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;">"X"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z"</span>, <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;">"Z"</span>)</span>
<span id="cb27-6">)</span>
<span id="cb27-7"></span>
<span id="cb27-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Filter for specific categories (creates gaps)</span></span>
<span id="cb27-9">filtered_practice <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> practice_df[practice_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Category <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z"</span>), ]</span>
<span id="cb27-10"></span>
<span id="cb27-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: Try different reset methods and compare results</span></span>
<span id="cb27-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here...</span></span></code></pre></div>
</div>
<details>
<summary>
Click here for Solution!
</summary>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: Create sample data</span></span>
<span id="cb28-2">practice_df <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="cb28-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Product =</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="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"C"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"D"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"E"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F"</span>),</span>
<span id="cb28-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Price =</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;">10</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;">15</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</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;">35</span>),</span>
<span id="cb28-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Category =</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;">"X"</span>, <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;">"X"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z"</span>, <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;">"Z"</span>)</span>
<span id="cb28-6">)</span>
<span id="cb28-7"></span>
<span id="cb28-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: Filter for specific categories</span></span>
<span id="cb28-9">filtered_practice <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> practice_df[practice_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>Category <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;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Z"</span>), ]</span>
<span id="cb28-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Original row names after filtering:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Original row names after filtering:"</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(filtered_practice))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "1" "3" "4" "6"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "3" "4" "6"</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: Method 1 - Set to NULL</span></span>
<span id="cb32-2">method1_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> filtered_practice</span>
<span id="cb32-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(method1_df) <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;">NULL</span></span>
<span id="cb32-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Method 1 result:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Method 1 result:"</code></pre>
</div>
<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"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(method1_df))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "1" "2" "3" "4"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "3" "4"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: Method 2 - Explicit sequence</span></span>
<span id="cb36-2">method2_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> filtered_practice  </span>
<span id="cb36-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(method2_df) <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;">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;">nrow</span>(method2_df)</span>
<span id="cb36-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Method 2 result:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Method 2 result:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(method2_df))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "1" "2" "3" "4"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "3" "4"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 5: Method 3 - Using dplyr</span></span>
<span id="cb40-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'dplyr'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:stats':

    filter, lag</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union</code></pre>
</div>
<div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1">method3_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> filtered_practice <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb44-2">  <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;">new_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">row_number</span>()) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb44-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>new_id)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Remove the helper column</span></span>
<span id="cb44-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(method3_df) <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;">NULL</span></span>
<span id="cb44-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Method 3 result:"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Method 3 result:"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rownames</span>(method3_df))  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Shows: "1" "2" "3" "4"</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1" "2" "3" "4"</code></pre>
</div>
</div>
</details>
<hr>
</section>
<section id="quick-takeaways" class="level1">
<h1>Quick Takeaways</h1>
<p>• <strong>Primary Method:</strong> Use <code>rownames(df) &lt;- NULL</code> for most scenarios - it’s simple, fast, and reliable</p>
<p>• <strong>Common Use Cases:</strong> Essential after filtering, removing duplicates, sorting, or sampling data</p>
<p>• <strong>Performance:</strong> All methods perform similarly; choose based on functional requirements rather than speed</p>
<p>• <strong>Data Integrity:</strong> Consider preserving original row identifiers as separate columns when traceability matters</p>
<p>• <strong>Error Prevention:</strong> Ensure row names are unique and non-missing to avoid common pitfalls</p>
<p>• <strong>Best Practice:</strong> Reset row numbers as part of data cleaning workflows for cleaner presentation and export</p>
<hr>
</section>
<section id="method-comparison-table" class="level1">
<h1>Method Comparison Table</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 20%">
<col style="width: 38%">
<col style="width: 28%">
<col style="width: 14%">
</colgroup>
<thead>
<tr class="header">
<th>Scenario</th>
<th>Recommended Method</th>
<th>Code Example</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>General reset</td>
<td><code>rownames(df) &lt;- NULL</code></td>
<td><code>rownames(filtered_df) &lt;- NULL</code></td>
<td>Fastest, most common</td>
</tr>
<tr class="even">
<td>Explicit numbering</td>
<td><code>rownames(df) &lt;- 1:nrow(df)</code></td>
<td><code>rownames(sorted_df) &lt;- 1:nrow(sorted_df)</code></td>
<td>When specific sequence needed</td>
</tr>
<tr class="odd">
<td>Tidyverse workflow</td>
<td><code>as_tibble()</code> or <code>mutate()</code></td>
<td><code>df %&gt;% as_tibble()</code></td>
<td>Integrates with dplyr pipelines</td>
</tr>
<tr class="even">
<td>Preserve original</td>
<td>Keep as column</td>
<td><code>df$orig_row &lt;- rownames(df)</code></td>
<td>When traceability required</td>
</tr>
</tbody>
</table>
<hr>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Resetting row numbers in R data frames is a fundamental skill for effective data manipulation and presentation. The <code>rownames(df) &lt;- NULL</code> method provides the most straightforward solution for most use cases, ensuring clean sequential indexing essential for professional data analysis workflows.</p>
<p>Whether you’re filtering datasets, removing duplicates, or preparing data for export, understanding these techniques ensures your data frames maintain proper structure and readability. The choice between methods should be driven by your specific requirements rather than performance considerations, as the differences are minimal in practical applications.</p>
<p><strong>Ready to implement these techniques in your next R project? Start with the basic <code>rownames(df) &lt;- NULL</code> method and expand to more specialized approaches as your needs develop.</strong></p>
<hr>
</section>
<section id="frequently-asked-questions-faqs" class="level1">
<h1>Frequently Asked Questions (FAQs)</h1>
<p><strong>Q1: When should I reset row numbers in my data frame?</strong> Reset row numbers after filtering, subsetting, removing duplicates, sorting, or any operation that creates gaps in the row sequence. This ensures clean, sequential indexing.</p>
<p><strong>Q2: What’s the difference between <code>rownames(df) &lt;- NULL</code> and <code>rownames(df) &lt;- 1:nrow(df)</code>?</strong> Both create sequential row numbers, but <code>NULL</code> is faster and more commonly used. The explicit sequence method gives you more control over the exact values assigned.</p>
<p><strong>Q3: Will resetting row numbers affect my data frame’s content?</strong> No, resetting row numbers only changes the row labels/names, not the actual data content. Your data remains unchanged.</p>
<p><strong>Q4: Can I reset row numbers in tibbles?</strong> Tibbles don’t use row names by default. If you need sequential IDs, add them as a regular column using <code>mutate(id = row_number())</code>.</p>
<p><strong>Q5: What happens if I try to set duplicate row names?</strong> R will throw an error: “duplicate ‘row.names’ are not allowed.” Row names must be unique across the entire data frame.</p>
<hr>
<p><em>Found this guide helpful? Share your experience with row number resetting in the comments below, and don’t forget to share this article with fellow R users who might benefit from these techniques!</em></p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><p><strong>Stack Overflow Community</strong>. (2023). <a href="https://stackoverflow.com/questions/5208679/how-to-reset-row-names"><em>How to reset row names?</em></a>. Stack Overflow. Retrieved August 28, 2025.</p></li>
<li><p><strong>Wickham, H., François, R., Henry, L., &amp; Müller, K.</strong> (2023). <a href="https://dplyr.tidyverse.org/articles/rowwise.html"><em>Row-wise operations</em></a>. dplyr: A Grammar of Data Manipulation Documentation. Posit PBC.</p></li>
<li><p><strong>Müller, K. &amp; Wickham, H.</strong> (2023). <a href="https://tibble.tidyverse.org/reference/rownames.html"><em>Tools for working with row names — rownames</em></a>. tibble: Simple Data Frames Documentation. Posit PBC.</p></li>
<li><p><strong>R-bloggers Community</strong>. (2020). <a href="https://www.r-bloggers.com/2020/08/data-manipulation-in-r-using-data-frames-an-extensive-article-of-basics/"><em>Data manipulation in R using data frames – an extensive article of basics</em></a>. R-bloggers.</p></li>
</ol>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-09-01/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Rownumbers in R</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-09-01/</guid>
  <pubDate>Mon, 01 Sep 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>Revolutionary RandomWalker Update: 23 New Functions Transform Stochastic Modeling in R</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-08-19/</link>
  <description><![CDATA[ 






<blockquote class="blockquote">
<p><strong>Key Update:</strong> RandomWalker version 0.3.0 introduces 21 new distribution-based random walk generators plus 2 enhanced utility functions, expanding from basic normal distributions to comprehensive stochastic modeling across discrete, continuous, and statistical test distributions.</p>
</blockquote>
<p>The RandomWalker package has undergone a revolutionary transformation, evolving from a basic random walk generator to a comprehensive stochastic modeling toolkit. This update represents the most significant expansion in the package’s history, introducing <strong>21 new random walk generator functions</strong> and <strong>2 enhanced utility functions</strong> that will fundamentally change how R programmers approach random walk simulations.</p>
<section id="complete-function-arsenal-from-basic-to-advanced" class="level1">
<h1><strong>Complete Function Arsenal: From Basic to Advanced</strong></h1>
<p>The new RandomWalker update delivers an unprecedented collection of functions covering every major category of statistical distributions.</p>
<section id="continuous-distribution-random-walks" class="level2">
<h2 class="anchored" data-anchor-id="continuous-distribution-random-walks"><strong>Continuous Distribution Random Walks</strong></h2>
<p>The package now supports nine sophisticated continuous distribution functions, each optimized for specific modeling scenarios:</p>
<ul>
<li><strong><code>random_uniform_walk()</code></strong>: Perfect for Monte Carlo simulations requiring flat probability distributions</li>
<li><strong><code>random_weibull_walk()</code></strong>: Essential for reliability engineering and survival analysis applications</li>
<li><strong><code>random_t_walk()</code></strong>: Ideal for heavy-tailed financial processes and robust statistical modeling</li>
<li><strong><code>random_logistic_walk()</code></strong>: Designed for growth modeling and S-curve phenomena</li>
<li><strong><code>random_lognormal_walk()</code></strong>: Critical for asset pricing and multiplicative processes</li>
<li><strong><code>random_gamma_walk()</code></strong>: Optimized for waiting times and shape-scale modeling scenarios</li>
<li><strong><code>random_exponential_walk()</code></strong>: Built for Poisson process intervals and decay modeling</li>
<li><strong><code>random_beta_walk()</code></strong>: Perfect for bounded probability processes and proportion modeling</li>
<li><strong><code>random_cauchy_walk()</code></strong>: Specialized for extreme value theory and heavy-tailed phenomena</li>
</ul>
<div class="cell" data-warnng="false">
<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>(RandomWalker)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Continuous Walks</span></span>
<span id="cb1-5">ns <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;">1</span>L</span>
<span id="cb1-6"></span>
<span id="cb1-7"><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;">123</span>)</span>
<span id="cb1-8">rw_tbl <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;">bind_rows</span>(</span>
<span id="cb1-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">brownian_motion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-10">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Brownian Motion"</span>),</span>
<span id="cb1-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geometric_brownian_motion</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-12">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geometric Brownian Motion"</span>),</span>
<span id="cb1-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_beta_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-14">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Beta"</span>),</span>
<span id="cb1-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_cauchy_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-16">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cauchy"</span>),</span>
<span id="cb1-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_chisquared_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-18">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Chisquared"</span>),</span>
<span id="cb1-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_exponential_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-20">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Exponential"</span>),</span>
<span id="cb1-21">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_f_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-22">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"F Distribution"</span>),</span>
<span id="cb1-23">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_gamma_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-24">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gamma"</span>),</span>
<span id="cb1-25">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_logistic_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-26">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Logisitic"</span>),</span>
<span id="cb1-27">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_lognormal_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-28">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Log-Normal"</span>),</span>
<span id="cb1-29">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_normal_drift_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-30">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Random Normal Drift"</span>),</span>
<span id="cb1-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_normal_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-32">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Random Normal"</span>),</span>
<span id="cb1-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_t_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-34">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"T Distribution"</span>),</span>
<span id="cb1-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_uniform_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-36">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Uniform"</span>),</span>
<span id="cb1-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_weibull_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb1-38">    <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Weibull"</span>)</span>
<span id="cb1-39">) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(step_number, x, y, walk_type)</span>
<span id="cb1-41"></span>
<span id="cb1-42">rw_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-43">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> walk_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> step_number)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb1-47">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> rw_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-48">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(walk_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb1-49">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb1-50">        step_number <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>(step_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb1-51">          step_number <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>(step_number)</span>
<span id="cb1-52">      ),</span>
<span id="cb1-53">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> step_number),</span>
<span id="cb1-54">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb1-55">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plasma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-57">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-58">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb1-59">    <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;">"Random Walks for Continuous Distributions"</span>,</span>
<span id="cb1-60">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb1-61">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of Simulations: "</span>, ns, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" - "</span>,</span>
<span id="cb1-62">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of Steps: "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb1-63">    ),</span>
<span id="cb1-64">    <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;">"Y Value"</span>,</span>
<span id="cb1-65">    <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;">"X Value"</span></span>
<span id="cb1-66">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-67">  <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;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb1-68">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb1-69">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-70">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-71">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb1-72">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb1-73">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-08-19/index_files/figure-html/continuous_random_walks-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
<section id="discrete-distribution-random-walks" class="level2">
<h2 class="anchored" data-anchor-id="discrete-distribution-random-walks"><strong>Discrete Distribution Random Walks</strong></h2>
<p>Six new discrete distribution functions enable precise modeling of count processes and success-based scenarios:</p>
<ul>
<li><strong><code>random_poisson_walk()</code></strong>: Event counting and arrival processes</li>
<li><strong><code>random_binomial_walk()</code></strong>: Fixed trials with success/failure modeling</li>
<li><strong><code>random_negbinomial_walk()</code></strong>: Over-dispersed counts and waiting for multiple successes</li>
<li><strong><code>random_geometric_walk()</code></strong>: First success timing with memoryless properties</li>
<li><strong><code>random_hypergeometric_walk()</code></strong>: Sampling without replacement from finite populations</li>
<li><strong><code>random_multinomial_walk()</code></strong>: Multi-category outcomes for complex probability spaces</li>
</ul>
</section>
<section id="statistical-test-based-random-walks" class="level2">
<h2 class="anchored" data-anchor-id="statistical-test-based-random-walks"><strong>Statistical Test-Based Random Walks</strong></h2>
<p>Four specialized functions bring nonparametric testing capabilities to random walk modeling:</p>
<ul>
<li><strong><code>random_wilcox_walk()</code></strong>: Wilcoxon signed-rank applications for nonparametric analysis</li>
<li><strong><code>random_wilcoxon_sr_walk()</code></strong>: Enhanced Wilcoxon with step specification functionality</li>
<li><strong><code>random_smirnov_walk()</code></strong>: Distribution comparison and goodness-of-fit testing</li>
<li><strong><code>random_f_walk()</code></strong>: Variance ratio testing and ANOVA applications</li>
<li><strong><code>random_chisquared_walk()</code></strong>: Goodness-of-fit and variance testing scenarios</li>
</ul>
</section>
<section id="specialized-functions" class="level2">
<h2 class="anchored" data-anchor-id="specialized-functions"><strong>Specialized Functions</strong></h2>
<p>The update includes a powerful custom modeling function:</p>
<ul>
<li><strong><code>random_displacement_walk()</code></strong>: User-defined step distributions enabling unlimited flexibility for custom modeling scenarios.</li>
</ul>
<div class="cell" data-warnng="false">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Discrete Walks</span></span>
<span id="cb2-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;">123</span>)</span>
<span id="cb2-3">dw_tbl <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;">bind_rows</span>(</span>
<span id="cb2-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">discrete_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-5">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Discrete"</span>),</span>
<span id="cb2-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_binomial_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Binomial"</span>),</span>
<span id="cb2-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_displacement_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-9">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Displacement"</span>),</span>
<span id="cb2-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_geometric_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-11">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Geometric"</span>),</span>
<span id="cb2-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_hypergeometric_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-13">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hypergeometric"</span>),</span>
<span id="cb2-14">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_multinomial_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">.size =</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;">|&gt;</span></span>
<span id="cb2-15">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Multinomial"</span>),</span>
<span id="cb2-16">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_negbinomial_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>,</span>
<span id="cb2-17">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.size =</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;">1</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;">1</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;">|&gt;</span></span>
<span id="cb2-18">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Negative Binomial"</span>),</span>
<span id="cb2-19">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_poisson_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-20">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Poisson"</span>),</span>
<span id="cb2-21">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_smirnov_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">.z =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">seq</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="at" style="color: #657422;
background-color: null;
font-style: inherit;">by =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>),</span>
<span id="cb2-22">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.sizes =</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-23">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Smirnov"</span>),</span>
<span id="cb2-24">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_wilcoxon_sr_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-25">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wilcoxon Signed Rank"</span>),</span>
<span id="cb2-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">random_wilcox_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> ns, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</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;">|&gt;</span></span>
<span id="cb2-27">      <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;">walk_type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Wilcox"</span>)</span>
<span id="cb2-28">  )</span>
<span id="cb2-29"></span>
<span id="cb2-30">dw_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-31">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> x, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-32">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">facet_wrap</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> walk_type, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">scales =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"free"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-33">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_path</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> step_number)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_point</span>(</span>
<span id="cb2-35">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> dw_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-36">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(walk_type) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb2-37">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(</span>
<span id="cb2-38">        step_number <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>(step_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|</span></span>
<span id="cb2-39">          step_number <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>(step_number)</span>
<span id="cb2-40">      ),</span>
<span id="cb2-41">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> step_number),</span>
<span id="cb2-42">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb2-43">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_color_viridis_c</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">option =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"plasma"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_minimal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-46">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(</span>
<span id="cb2-47">    <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;">"Random Walks for Discrete Distributions"</span>,</span>
<span id="cb2-48">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">subtitle =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(</span>
<span id="cb2-49">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of Simulations: "</span>, ns, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" - "</span>,</span>
<span id="cb2-50">      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Number of Steps: "</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span></span>
<span id="cb2-51">    ),</span>
<span id="cb2-52">    <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;">"Y Value"</span>,</span>
<span id="cb2-53">    <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;">"X Value"</span></span>
<span id="cb2-54">  ) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-55">  <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;">"none"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb2-56">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(</span>
<span id="cb2-57">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-58">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-59">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.text.y =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>(),</span>
<span id="cb2-60">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">axis.ticks.x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">element_blank</span>()</span>
<span id="cb2-61">  )</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-08-19/index_files/figure-html/discrete_random_walks-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="enhanced-utility-functions-powerful-new-capabilities" class="level1">
<h1><strong>Enhanced Utility Functions: Powerful New Capabilities</strong></h1>
<p>Two critical utility functions received major enhancements that dramatically expand their functionality:</p>
<section id="advanced-subsetting-with-subset_walks" class="level2">
<h2 class="anchored" data-anchor-id="advanced-subsetting-with-subset_walks"><strong>Advanced Subsetting with <code>subset_walks()</code></strong></h2>
<p>The updated <code>subset_walks()</code> function introduces the <code>.value</code> parameter, allowing users to subset random walks based on any column, not just the default “y” position :</p>
<div class="cell">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb3-2">walks <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;">rw30</span>()</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset_walks</span>(walks, <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;">"max"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"y"</span>) <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;">visualize_walks</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.interactive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="girafe html-widget html-fill-item" id="htmlwidget-75c802b17b45502efefe" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-75c802b17b45502efefe">{"x":{"html":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='ggiraph-svg' role='graphics-document' id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f' viewBox='0 0 504 360'>\n <defs id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_defs'>\n  <clipPath id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_c1'>\n   <rect x='0' y='0' width='504' height='360'/>\n  <\/clipPath>\n  <clipPath id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_c2'>\n   <rect x='30.17' y='66.51' width='458.39' height='248.36'/>\n  <\/clipPath>\n  <clipPath id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_c3'>\n   <rect x='5.48' y='38.85' width='493.04' height='302.04'/>\n  <\/clipPath>\n <\/defs>\n <g id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_rootg' class='ggiraph-svg-rootg'>\n  <g clip-path='url(#svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_c1)'>\n   <rect x='0' y='0' width='504' height='360' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='0.75' stroke-linejoin='round' stroke-linecap='round' class='ggiraph-svg-bg'/>\n   <rect x='0' y='0' width='504' height='360' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='round'/>\n  <\/g>\n  <g clip-path='url(#svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_c2)'>\n   <polyline points='30.17,273.74 488.56,273.74' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='30.17,214.06 488.56,214.06' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='30.17,154.37 488.56,154.37' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='30.17,94.69 488.56,94.69' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='99.41,314.87 99.41,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='204.64,314.87 204.64,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='309.87,314.87 309.87,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='415.11,314.87 415.11,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='30.17,303.58 488.56,303.58' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='30.17,243.90 488.56,243.90' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='30.17,184.22 488.56,184.22' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='30.17,124.53 488.56,124.53' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='46.79,314.87 46.79,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='152.02,314.87 152.02,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='257.26,314.87 257.26,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='362.49,314.87 362.49,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='467.72,314.87 467.72,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e1' points='51.00,303.58 55.21,294.87 59.42,301.88 63.63,301.87 67.84,275.44 72.05,263.87 76.26,254.70 80.47,267.93 84.68,277.31 88.89,250.05 93.09,263.10 97.30,260.54 101.51,249.89 105.72,237.72 109.93,224.72 114.14,226.67 118.35,236.47 122.56,240.14 126.77,250.91 130.98,243.42 135.19,230.05 139.40,204.66 143.61,200.29 147.82,210.73 152.02,198.50 156.23,187.70 160.44,190.54 164.65,209.14 168.86,200.05 173.07,186.57 177.28,190.10 181.49,183.69 185.70,186.99 189.91,178.84 194.12,180.24 198.33,184.36 202.54,183.02 206.75,186.41 210.95,193.46 215.16,197.23 219.37,197.33 223.58,194.85 227.79,176.56 232.00,192.77 236.21,195.16 240.42,187.62 244.63,166.58 248.84,161.50 253.05,161.66 257.26,165.33 261.47,160.39 265.68,148.58 269.88,150.78 274.09,148.82 278.30,146.23 282.51,137.53 286.72,124.26 290.93,120.93 295.14,121.84 299.35,105.19 303.56,103.23 307.77,84.39 311.98,85.13 316.19,77.80 320.40,96.26 324.61,97.60 328.82,97.86 333.02,106.91 337.23,119.28 341.44,107.96 345.65,97.05 349.86,112.55 354.07,107.48 358.28,120.76 362.49,133.31 366.70,127.04 370.91,135.23 375.12,123.37 379.33,122.91 383.54,116.51 387.75,122.76 391.95,136.50 396.16,125.58 400.37,122.74 404.58,125.59 408.79,124.77 413.00,108.94 417.21,117.27 421.42,126.22 425.63,133.62 429.84,152.54 434.05,142.75 438.26,140.46 442.47,137.98 446.68,138.50 450.88,144.59 455.09,154.42 459.30,144.25 463.51,161.27 467.72,156.02' fill='none' stroke='#F8766D' stroke-opacity='0.7' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' data-id='16' title='Walk Number: 16 | Step: 1 | y: 0'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e2' cx='51' cy='303.58' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 1 | y: 0'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e3' cx='55.21' cy='294.87' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 2 | y: 0.73'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e4' cx='59.42' cy='301.88' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 3 | y: 0.143'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e5' cx='63.63' cy='301.87' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 4 | y: 0.143'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e6' cx='67.84' cy='275.44' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 5 | y: 2.358'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e7' cx='72.05' cy='263.87' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 6 | y: 3.327'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e8' cx='76.26' cy='254.7' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 7 | y: 4.095'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e9' cx='80.47' cy='267.93' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 8 | y: 2.987'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e10' cx='84.68' cy='277.31' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 9 | y: 2.201'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e11' cx='88.89' cy='250.05' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 10 | y: 4.485'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e12' cx='93.09' cy='263.1' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 11 | y: 3.392'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e13' cx='97.3' cy='260.54' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 12 | y: 3.606'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e14' cx='101.51' cy='249.89' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 13 | y: 4.499'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e15' cx='105.72' cy='237.72' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 14 | y: 5.517'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e16' cx='109.93' cy='224.72' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 15 | y: 6.606'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e17' cx='114.14' cy='226.67' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 16 | y: 6.443'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e18' cx='118.35' cy='236.47' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 17 | y: 5.622'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e19' cx='122.56' cy='240.14' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 18 | y: 5.315'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e20' cx='126.77' cy='250.91' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 19 | y: 4.413'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e21' cx='130.98' cy='243.42' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 20 | y: 5.04'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e22' cx='135.19' cy='230.05' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 21 | y: 6.16'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e23' cx='139.4' cy='204.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 22 | y: 8.288'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e24' cx='143.61' cy='200.29' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 23 | y: 8.654'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e25' cx='147.82' cy='210.73' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 24 | y: 7.779'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e26' cx='152.02' cy='198.5' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 25 | y: 8.803'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e27' cx='156.23' cy='187.7' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 26 | y: 9.708'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e28' cx='160.44' cy='190.54' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 27 | y: 9.47'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e29' cx='164.65' cy='209.14' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 28 | y: 7.912'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e30' cx='168.86' cy='200.05' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 29 | y: 8.673'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e31' cx='173.07' cy='186.57' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 30 | y: 9.803'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e32' cx='177.28' cy='190.1' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 31 | y: 9.507'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e33' cx='181.49' cy='183.69' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 32 | y: 10.044'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e34' cx='185.7' cy='186.99' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 33 | y: 9.768'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e35' cx='189.91' cy='178.84' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 34 | y: 10.45'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e36' cx='194.12' cy='180.24' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 35 | y: 10.333'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e37' cx='198.33' cy='184.36' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 36 | y: 9.988'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e38' cx='202.54' cy='183.02' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 37 | y: 10.1'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e39' cx='206.75' cy='186.41' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 38 | y: 9.816'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e40' cx='210.95' cy='193.46' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 39 | y: 9.225'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e41' cx='215.16' cy='197.23' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 40 | y: 8.909'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e42' cx='219.37' cy='197.33' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 41 | y: 8.901'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e43' cx='223.58' cy='194.85' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 42 | y: 9.109'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e44' cx='227.79' cy='176.56' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 43 | y: 10.641'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e45' cx='232' cy='192.77' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 44 | y: 9.283'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e46' cx='236.21' cy='195.16' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 45 | y: 9.084'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e47' cx='240.42' cy='187.62' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 46 | y: 9.715'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e48' cx='244.63' cy='166.58' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 47 | y: 11.477'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e49' cx='248.84' cy='161.5' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 48 | y: 11.903'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e50' cx='253.05' cy='161.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 49 | y: 11.889'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e51' cx='257.26' cy='165.33' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 50 | y: 11.582'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e52' cx='261.47' cy='160.39' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 51 | y: 11.996'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e53' cx='265.68' cy='148.58' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 52 | y: 12.985'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e54' cx='269.88' cy='150.78' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 53 | y: 12.801'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e55' cx='274.09' cy='148.82' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 54 | y: 12.965'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e56' cx='278.3' cy='146.23' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 55 | y: 13.182'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e57' cx='282.51' cy='137.53' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 56 | y: 13.911'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e58' cx='286.72' cy='124.26' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 57 | y: 15.023'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e59' cx='290.93' cy='120.93' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 58 | y: 15.302'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e60' cx='295.14' cy='121.84' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 59 | y: 15.226'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e61' cx='299.35' cy='105.19' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 60 | y: 16.62'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e62' cx='303.56' cy='103.23' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 61 | y: 16.785'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e63' cx='307.77' cy='84.39' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 62 | y: 18.363'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e64' cx='311.98' cy='85.13' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 63 | y: 18.301'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e65' cx='316.19' cy='77.8' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 64 | y: 18.915'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e66' cx='320.4' cy='96.26' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 65 | y: 17.369'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e67' cx='324.61' cy='97.6' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 66 | y: 17.256'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e68' cx='328.82' cy='97.86' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 67 | y: 17.234'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e69' cx='333.02' cy='106.91' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 68 | y: 16.476'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e70' cx='337.23' cy='119.28' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 69 | y: 15.44'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e71' cx='341.44' cy='107.96' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 70 | y: 16.388'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e72' cx='345.65' cy='97.05' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 71 | y: 17.302'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e73' cx='349.86' cy='112.55' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 72 | y: 16.004'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e74' cx='354.07' cy='107.48' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 73 | y: 16.428'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e75' cx='358.28' cy='120.76' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 74 | y: 15.316'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e76' cx='362.49' cy='133.31' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 75 | y: 14.265'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e77' cx='366.7' cy='127.04' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 76 | y: 14.79'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e78' cx='370.91' cy='135.23' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 77 | y: 14.104'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e79' cx='375.12' cy='123.37' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 78 | y: 15.097'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e80' cx='379.33' cy='122.91' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 79 | y: 15.136'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e81' cx='383.54' cy='116.51' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 80 | y: 15.672'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e82' cx='387.75' cy='122.76' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 81 | y: 15.148'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e83' cx='391.95' cy='136.5' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 82 | y: 13.997'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e84' cx='396.16' cy='125.58' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 83 | y: 14.912'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e85' cx='400.37' cy='122.74' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 84 | y: 15.15'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e86' cx='404.58' cy='125.59' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 85 | y: 14.911'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e87' cx='408.79' cy='124.77' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 86 | y: 14.98'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e88' cx='413' cy='108.94' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 87 | y: 16.306'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e89' cx='417.21' cy='117.27' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 88 | y: 15.608'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e90' cx='421.42' cy='126.22' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 89 | y: 14.859'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e91' cx='425.63' cy='133.62' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 90 | y: 14.239'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e92' cx='429.84' cy='152.54' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 91 | y: 12.654'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e93' cx='434.05' cy='142.75' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 92 | y: 13.474'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e94' cx='438.26' cy='140.46' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 93 | y: 13.666'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e95' cx='442.47' cy='137.98' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 94 | y: 13.873'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e96' cx='446.68' cy='138.5' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 95 | y: 13.83'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e97' cx='450.88' cy='144.59' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 96 | y: 13.32'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e98' cx='455.09' cy='154.42' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 97 | y: 12.496'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e99' cx='459.3' cy='144.25' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 98 | y: 13.348'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e100' cx='463.51' cy='161.27' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 99 | y: 11.922'/>\n   <circle id='svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_e101' cx='467.72' cy='156.02' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 100 | y: 12.362'/>\n  <\/g>\n  <g clip-path='url(#svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f_c1)'>\n   <text x='20.34' y='306.73' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>0<\/text>\n   <text x='20.34' y='247.05' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>5<\/text>\n   <text x='15.44' y='187.37' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>10<\/text>\n   <text x='15.44' y='127.68' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>15<\/text>\n   <text x='44.34' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>0<\/text>\n   <text x='147.13' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>25<\/text>\n   <text x='252.36' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>50<\/text>\n   <text x='357.59' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>75<\/text>\n   <text x='460.38' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>100<\/text>\n   <text x='248.04' y='338.57' font-size='8.25pt' font-family='Arial'>Step<\/text>\n   <text x='30.17' y='58.26' font-size='9.9pt' font-family='Arial'>y<\/text>\n   <text x='330.01' y='352.67' font-size='6.6pt' font-family='Arial'>1 dimensions, 100 steps, mu = 0, sd = 1.<\/text>\n   <text x='15.44' y='31.05' font-size='8.25pt' font-family='Arial'>Function: rw30<\/text>\n   <text x='15.44' y='14.93' font-size='9.9pt' font-family='Arial'>30 Random Walks<\/text>\n  <\/g>\n <\/g>\n<\/svg>","js":null,"uid":"svg_d8bea0e9_a032_42c2_a8e1_7bbe705cc63f","ratio":1.4,"settings":{"tooltip":{"css":".tooltip_SVGID_ { padding:5px;background:black;color:white;border-radius:2px 2px 2px 2px;text-align:left; ; position:absolute;pointer-events:none;z-index:999;}","placement":"doc","opacity":0.7,"offx":200,"offy":5,"use_cursor_pos":false,"use_fill":false,"use_stroke":false,"delay_over":200,"delay_out":500},"hover":{"css":".hover_data_SVGID_ { stroke:black;stroke-width:2pt; }","reactive":false,"nearest_distance":null},"hover_inv":{"css":".hover_inv_SVGID_ { opacity:0.4; }"},"hover_key":{"css":".hover_key_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_key_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_key_SVGID_ { fill:orange;stroke:black; }\nline.hover_key_SVGID_, polyline.hover_key_SVGID_ { fill:none;stroke:orange; }\nrect.hover_key_SVGID_, polygon.hover_key_SVGID_, path.hover_key_SVGID_ { fill:orange;stroke:none; }\nimage.hover_key_SVGID_ { stroke:orange; }","reactive":true},"hover_theme":{"css":".hover_theme_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_theme_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_theme_SVGID_ { fill:orange;stroke:black; }\nline.hover_theme_SVGID_, polyline.hover_theme_SVGID_ { fill:none;stroke:orange; }\nrect.hover_theme_SVGID_, polygon.hover_theme_SVGID_, path.hover_theme_SVGID_ { fill:orange;stroke:none; }\nimage.hover_theme_SVGID_ { stroke:orange; }","reactive":true},"select":{"css":".select_data_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_data_SVGID_ { stroke:none;fill:red; }\ncircle.select_data_SVGID_ { fill:red;stroke:black; }\nline.select_data_SVGID_, polyline.select_data_SVGID_ { fill:none;stroke:red; }\nrect.select_data_SVGID_, polygon.select_data_SVGID_, path.select_data_SVGID_ { fill:red;stroke:none; }\nimage.select_data_SVGID_ { stroke:red; }","type":"multiple","only_shiny":true,"selected":[]},"select_inv":{"css":""},"select_key":{"css":".select_key_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_key_SVGID_ { stroke:none;fill:red; }\ncircle.select_key_SVGID_ { fill:red;stroke:black; }\nline.select_key_SVGID_, polyline.select_key_SVGID_ { fill:none;stroke:red; }\nrect.select_key_SVGID_, polygon.select_key_SVGID_, path.select_key_SVGID_ { fill:red;stroke:none; }\nimage.select_key_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"select_theme":{"css":".select_theme_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_theme_SVGID_ { stroke:none;fill:red; }\ncircle.select_theme_SVGID_ { fill:red;stroke:black; }\nline.select_theme_SVGID_, polyline.select_theme_SVGID_ { fill:none;stroke:red; }\nrect.select_theme_SVGID_, polygon.select_theme_SVGID_, path.select_theme_SVGID_ { fill:red;stroke:none; }\nimage.select_theme_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"zoom":{"min":1,"max":5,"duration":300},"toolbar":{"position":"topright","pngname":"diagram","tooltips":null,"fixed":false,"hidden":[],"delay_over":200,"delay_out":500},"sizing":{"rescale":true,"width":1}}},"evals":[],"jsHooks":[]}</script>
</div>
<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_walks</span>(walks, <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;">"min"</span>) <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;">visualize_walks</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.interactive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="girafe html-widget html-fill-item" id="htmlwidget-6fc14a306ffce2a6ae61" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-6fc14a306ffce2a6ae61">{"x":{"html":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='ggiraph-svg' role='graphics-document' id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2' viewBox='0 0 504 360'>\n <defs id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_defs'>\n  <clipPath id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_c1'>\n   <rect x='0' y='0' width='504' height='360'/>\n  <\/clipPath>\n  <clipPath id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_c2'>\n   <rect x='33.1' y='66.51' width='455.46' height='248.36'/>\n  <\/clipPath>\n  <clipPath id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_c3'>\n   <rect x='5.48' y='38.85' width='493.04' height='302.04'/>\n  <\/clipPath>\n <\/defs>\n <g id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_rootg' class='ggiraph-svg-rootg'>\n  <g clip-path='url(#svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_c1)'>\n   <rect x='0' y='0' width='504' height='360' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='0.75' stroke-linejoin='round' stroke-linecap='round' class='ggiraph-svg-bg'/>\n   <rect x='0' y='0' width='504' height='360' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='round'/>\n  <\/g>\n  <g clip-path='url(#svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_c2)'>\n   <polyline points='33.10,278.11 488.56,278.11' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,233.60 488.56,233.60' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,189.08 488.56,189.08' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,144.57 488.56,144.57' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,100.06 488.56,100.06' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='101.90,314.87 101.90,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='206.46,314.87 206.46,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='311.02,314.87 311.02,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='415.58,314.87 415.58,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,300.37 488.56,300.37' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,255.85 488.56,255.85' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,211.34 488.56,211.34' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,166.83 488.56,166.83' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,122.31 488.56,122.31' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,77.80 488.56,77.80' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='49.62,314.87 49.62,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='154.18,314.87 154.18,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='258.74,314.87 258.74,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='363.30,314.87 363.30,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='467.86,314.87 467.86,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e1' points='53.80,77.80 57.98,84.81 62.16,95.93 66.35,105.52 70.53,103.30 74.71,104.36 78.89,107.04 83.08,127.70 87.26,139.49 91.44,140.66 95.62,148.48 99.81,155.57 103.99,146.23 108.17,144.66 112.35,153.96 116.53,158.13 120.72,160.66 124.90,166.72 129.08,175.30 133.26,175.76 137.45,169.17 141.63,167.15 145.81,168.93 149.99,165.48 154.18,158.41 158.36,159.66 162.54,155.60 166.72,165.80 170.91,168.02 175.09,171.76 179.27,170.02 183.45,166.84 187.64,167.94 191.82,174.76 196.00,183.04 200.18,180.56 204.36,168.48 208.55,175.49 212.73,178.91 216.91,175.97 221.09,180.91 225.28,179.83 229.46,180.25 233.64,187.16 237.82,179.76 242.01,172.22 246.19,163.11 250.37,151.82 254.55,156.33 258.74,160.46 262.92,158.13 267.10,152.53 271.28,155.55 275.47,159.32 279.65,164.82 283.83,151.63 288.01,173.96 292.19,175.45 296.38,175.11 300.56,184.54 304.74,181.11 308.92,198.62 313.11,190.12 317.29,204.93 321.47,224.54 325.65,231.34 329.84,229.89 334.02,235.69 338.20,240.67 342.38,237.71 346.57,247.13 350.75,247.90 354.93,243.46 359.11,228.92 363.30,224.65 367.48,209.38 371.66,205.35 375.84,205.38 380.02,225.47 384.21,236.37 388.39,239.21 392.57,232.87 396.75,235.74 400.94,230.90 405.12,240.37 409.30,242.81 413.48,240.88 417.67,244.08 421.85,243.07 426.03,249.05 430.21,245.71 434.40,246.43 438.58,246.85 442.76,264.20 446.94,270.20 451.13,283.46 455.31,297.76 459.49,302.15 463.67,303.58 467.86,301.06' fill='none' stroke='#F8766D' stroke-opacity='0.7' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' data-id='22' title='Walk Number: 22 | Step: 1 | y: 0'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e2' cx='53.8' cy='77.8' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 1 | y: 0'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e3' cx='57.98' cy='84.81' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 2 | y: -0.787'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e4' cx='62.16' cy='95.93' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 3 | y: -2.036'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e5' cx='66.35' cy='105.52' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 4 | y: -3.114'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e6' cx='70.53' cy='103.3' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 5 | y: -2.864'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e7' cx='74.71' cy='104.36' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 6 | y: -2.983'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e8' cx='78.89' cy='107.04' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 7 | y: -3.284'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e9' cx='83.08' cy='127.7' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 8 | y: -5.605'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e10' cx='87.26' cy='139.49' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 9 | y: -6.929'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e11' cx='91.44' cy='140.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 10 | y: -7.061'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e12' cx='95.62' cy='148.48' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 11 | y: -7.939'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e13' cx='99.81' cy='155.57' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 12 | y: -8.735'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e14' cx='103.99' cy='146.23' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 13 | y: -7.686'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e15' cx='108.17' cy='144.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 14 | y: -7.51'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e16' cx='112.35' cy='153.96' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 15 | y: -8.554'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e17' cx='116.53' cy='158.13' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 16 | y: -9.023'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e18' cx='120.72' cy='160.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 17 | y: -9.308'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e19' cx='124.9' cy='166.72' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 18 | y: -9.988'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e20' cx='129.08' cy='175.3' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 19 | y: -10.952'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e21' cx='133.26' cy='175.76' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 20 | y: -11.004'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e22' cx='137.45' cy='169.17' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 21 | y: -10.263'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e23' cx='141.63' cy='167.15' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 22 | y: -10.036'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e24' cx='145.81' cy='168.93' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 23 | y: -10.236'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e25' cx='149.99' cy='165.48' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 24 | y: -9.848'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e26' cx='154.18' cy='158.41' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 25 | y: -9.054'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e27' cx='158.36' cy='159.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 26 | y: -9.195'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e28' cx='162.54' cy='155.6' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 27 | y: -8.739'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e29' cx='166.72' cy='165.8' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 28 | y: -9.885'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e30' cx='170.91' cy='168.02' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 29 | y: -10.134'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e31' cx='175.09' cy='171.76' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 30 | y: -10.554'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e32' cx='179.27' cy='170.02' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 31 | y: -10.359'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e33' cx='183.45' cy='166.84' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 32 | y: -10.002'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e34' cx='187.64' cy='167.94' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 33 | y: -10.125'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e35' cx='191.82' cy='174.76' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 34 | y: -10.891'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e36' cx='196' cy='183.04' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 35 | y: -11.821'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e37' cx='200.18' cy='180.56' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 36 | y: -11.543'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e38' cx='204.36' cy='168.48' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 37 | y: -10.186'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e39' cx='208.55' cy='175.49' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 38 | y: -10.973'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e40' cx='212.73' cy='178.91' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 39 | y: -11.358'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e41' cx='216.91' cy='175.97' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 40 | y: -11.027'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e42' cx='221.09' cy='180.91' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 41 | y: -11.582'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e43' cx='225.28' cy='179.83' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 42 | y: -11.46'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e44' cx='229.46' cy='180.25' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 43 | y: -11.508'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e45' cx='233.64' cy='187.16' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 44 | y: -12.284'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e46' cx='237.82' cy='179.76' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 45 | y: -11.452'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e47' cx='242.01' cy='172.22' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 46 | y: -10.606'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e48' cx='246.19' cy='163.11' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 47 | y: -9.582'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e49' cx='250.37' cy='151.82' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 48 | y: -8.314'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e50' cx='254.55' cy='156.33' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 49 | y: -8.82'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e51' cx='258.74' cy='160.46' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 50 | y: -9.285'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e52' cx='262.92' cy='158.13' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 51 | y: -9.024'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e53' cx='267.1' cy='152.53' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 52 | y: -8.394'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e54' cx='271.28' cy='155.55' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 53 | y: -8.733'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e55' cx='275.47' cy='159.32' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 54 | y: -9.156'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e56' cx='279.65' cy='164.82' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 55 | y: -9.775'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e57' cx='283.83' cy='151.63' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 56 | y: -8.293'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e58' cx='288.01' cy='173.96' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 57 | y: -10.801'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e59' cx='292.19' cy='175.45' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 58 | y: -10.968'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e60' cx='296.38' cy='175.11' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 59 | y: -10.93'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e61' cx='300.56' cy='184.54' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 60 | y: -11.99'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e62' cx='304.74' cy='181.11' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 61 | y: -11.604'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e63' cx='308.92' cy='198.62' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 62 | y: -13.571'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e64' cx='313.11' cy='190.12' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 63 | y: -12.616'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e65' cx='317.29' cy='204.93' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 64 | y: -14.28'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e66' cx='321.47' cy='224.54' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 65 | y: -16.482'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e67' cx='325.65' cy='231.34' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 66 | y: -17.246'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e68' cx='329.84' cy='229.89' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 67 | y: -17.084'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e69' cx='334.02' cy='235.69' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 68 | y: -17.736'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e70' cx='338.2' cy='240.67' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 69 | y: -18.295'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e71' cx='342.38' cy='237.71' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 70 | y: -17.962'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e72' cx='346.57' cy='247.13' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 71 | y: -19.021'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e73' cx='350.75' cy='247.9' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 72 | y: -19.106'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e74' cx='354.93' cy='243.46' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 73 | y: -18.608'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e75' cx='359.11' cy='228.92' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 74 | y: -16.974'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e76' cx='363.3' cy='224.65' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 75 | y: -16.495'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e77' cx='367.48' cy='209.38' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 76 | y: -14.78'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e78' cx='371.66' cy='205.35' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 77 | y: -14.327'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e79' cx='375.84' cy='205.38' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 78 | y: -14.33'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e80' cx='380.02' cy='225.47' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 79 | y: -16.587'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e81' cx='384.21' cy='236.37' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 80 | y: -17.811'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e82' cx='388.39' cy='239.21' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 81 | y: -18.13'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e83' cx='392.57' cy='232.87' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 82 | y: -17.418'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e84' cx='396.75' cy='235.74' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 83 | y: -17.741'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e85' cx='400.94' cy='230.9' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 84 | y: -17.197'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e86' cx='405.12' cy='240.37' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 85 | y: -18.261'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e87' cx='409.3' cy='242.81' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 86 | y: -18.535'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e88' cx='413.48' cy='240.88' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 87 | y: -18.318'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e89' cx='417.67' cy='244.08' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 88 | y: -18.677'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e90' cx='421.85' cy='243.07' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 89 | y: -18.565'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e91' cx='426.03' cy='249.05' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 90 | y: -19.235'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e92' cx='430.21' cy='245.71' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 91 | y: -18.861'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e93' cx='434.4' cy='246.43' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 92 | y: -18.942'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e94' cx='438.58' cy='246.85' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 93 | y: -18.989'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e95' cx='442.76' cy='264.2' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 94 | y: -20.938'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e96' cx='446.94' cy='270.2' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 95 | y: -21.612'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e97' cx='451.13' cy='283.46' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 96 | y: -23.101'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e98' cx='455.31' cy='297.76' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 97 | y: -24.707'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e99' cx='459.49' cy='302.15' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 98 | y: -25.201'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e100' cx='463.67' cy='303.58' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 99 | y: -25.362'/>\n   <circle id='svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_e101' cx='467.86' cy='301.06' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 100 | y: -25.078'/>\n  <\/g>\n  <g clip-path='url(#svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2_c1)'>\n   <text x='15.44' y='303.52' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>-25<\/text>\n   <text x='15.44' y='259' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>-20<\/text>\n   <text x='15.44' y='214.49' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>-15<\/text>\n   <text x='15.44' y='169.98' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>-10<\/text>\n   <text x='20.34' y='125.46' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>-5<\/text>\n   <text x='23.27' y='80.95' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>0<\/text>\n   <text x='47.17' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>0<\/text>\n   <text x='149.28' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>25<\/text>\n   <text x='253.84' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>50<\/text>\n   <text x='358.4' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>75<\/text>\n   <text x='460.51' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>100<\/text>\n   <text x='249.51' y='338.57' font-size='8.25pt' font-family='Arial'>Step<\/text>\n   <text x='33.1' y='58.26' font-size='9.9pt' font-family='Arial'>y<\/text>\n   <text x='330.01' y='352.67' font-size='6.6pt' font-family='Arial'>1 dimensions, 100 steps, mu = 0, sd = 1.<\/text>\n   <text x='15.44' y='31.05' font-size='8.25pt' font-family='Arial'>Function: rw30<\/text>\n   <text x='15.44' y='14.93' font-size='9.9pt' font-family='Arial'>30 Random Walks<\/text>\n  <\/g>\n <\/g>\n<\/svg>","js":null,"uid":"svg_0c9cd6e6_6174_486a_a605_21a5772e5ec2","ratio":1.4,"settings":{"tooltip":{"css":".tooltip_SVGID_ { padding:5px;background:black;color:white;border-radius:2px 2px 2px 2px;text-align:left; ; position:absolute;pointer-events:none;z-index:999;}","placement":"doc","opacity":0.7,"offx":200,"offy":5,"use_cursor_pos":false,"use_fill":false,"use_stroke":false,"delay_over":200,"delay_out":500},"hover":{"css":".hover_data_SVGID_ { stroke:black;stroke-width:2pt; }","reactive":false,"nearest_distance":null},"hover_inv":{"css":".hover_inv_SVGID_ { opacity:0.4; }"},"hover_key":{"css":".hover_key_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_key_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_key_SVGID_ { fill:orange;stroke:black; }\nline.hover_key_SVGID_, polyline.hover_key_SVGID_ { fill:none;stroke:orange; }\nrect.hover_key_SVGID_, polygon.hover_key_SVGID_, path.hover_key_SVGID_ { fill:orange;stroke:none; }\nimage.hover_key_SVGID_ { stroke:orange; }","reactive":true},"hover_theme":{"css":".hover_theme_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_theme_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_theme_SVGID_ { fill:orange;stroke:black; }\nline.hover_theme_SVGID_, polyline.hover_theme_SVGID_ { fill:none;stroke:orange; }\nrect.hover_theme_SVGID_, polygon.hover_theme_SVGID_, path.hover_theme_SVGID_ { fill:orange;stroke:none; }\nimage.hover_theme_SVGID_ { stroke:orange; }","reactive":true},"select":{"css":".select_data_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_data_SVGID_ { stroke:none;fill:red; }\ncircle.select_data_SVGID_ { fill:red;stroke:black; }\nline.select_data_SVGID_, polyline.select_data_SVGID_ { fill:none;stroke:red; }\nrect.select_data_SVGID_, polygon.select_data_SVGID_, path.select_data_SVGID_ { fill:red;stroke:none; }\nimage.select_data_SVGID_ { stroke:red; }","type":"multiple","only_shiny":true,"selected":[]},"select_inv":{"css":""},"select_key":{"css":".select_key_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_key_SVGID_ { stroke:none;fill:red; }\ncircle.select_key_SVGID_ { fill:red;stroke:black; }\nline.select_key_SVGID_, polyline.select_key_SVGID_ { fill:none;stroke:red; }\nrect.select_key_SVGID_, polygon.select_key_SVGID_, path.select_key_SVGID_ { fill:red;stroke:none; }\nimage.select_key_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"select_theme":{"css":".select_theme_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_theme_SVGID_ { stroke:none;fill:red; }\ncircle.select_theme_SVGID_ { fill:red;stroke:black; }\nline.select_theme_SVGID_, polyline.select_theme_SVGID_ { fill:none;stroke:red; }\nrect.select_theme_SVGID_, polygon.select_theme_SVGID_, path.select_theme_SVGID_ { fill:red;stroke:none; }\nimage.select_theme_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"zoom":{"min":1,"max":5,"duration":300},"toolbar":{"position":"topright","pngname":"diagram","tooltips":null,"fixed":false,"hidden":[],"delay_over":200,"delay_out":500},"sizing":{"rescale":true,"width":1}}},"evals":[],"jsHooks":[]}</script>
</div>
<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;"># Subset by custom criteria</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">subset_walks</span>(walks, <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;">"both"</span>) <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;">visualize_walks</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.interactive =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output-display">
<div class="girafe html-widget html-fill-item" id="htmlwidget-ef21ebe1ba1bb7886db8" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-ef21ebe1ba1bb7886db8">{"x":{"html":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' class='ggiraph-svg' role='graphics-document' id='svg_13db3af7_5c79_452f_b102_263548f29962' viewBox='0 0 504 360'>\n <defs id='svg_13db3af7_5c79_452f_b102_263548f29962_defs'>\n  <clipPath id='svg_13db3af7_5c79_452f_b102_263548f29962_c1'>\n   <rect x='0' y='0' width='504' height='360'/>\n  <\/clipPath>\n  <clipPath id='svg_13db3af7_5c79_452f_b102_263548f29962_c2'>\n   <rect x='33.1' y='66.51' width='455.46' height='248.36'/>\n  <\/clipPath>\n  <clipPath id='svg_13db3af7_5c79_452f_b102_263548f29962_c3'>\n   <rect x='5.48' y='38.85' width='493.04' height='302.04'/>\n  <\/clipPath>\n <\/defs>\n <g id='svg_13db3af7_5c79_452f_b102_263548f29962_rootg' class='ggiraph-svg-rootg'>\n  <g clip-path='url(#svg_13db3af7_5c79_452f_b102_263548f29962_c1)'>\n   <rect x='0' y='0' width='504' height='360' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='0.75' stroke-linejoin='round' stroke-linecap='round' class='ggiraph-svg-bg'/>\n   <rect x='0' y='0' width='504' height='360' fill='#FFFFFF' fill-opacity='1' stroke='#FFFFFF' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='round'/>\n  <\/g>\n  <g clip-path='url(#svg_13db3af7_5c79_452f_b102_263548f29962_c2)'>\n   <polyline points='33.10,301.74 488.56,301.74' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,250.75 488.56,250.75' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,199.75 488.56,199.75' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,148.76 488.56,148.76' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,97.76 488.56,97.76' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='101.90,314.87 101.90,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='206.46,314.87 206.46,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='311.02,314.87 311.02,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='415.58,314.87 415.58,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='0.53' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,276.24 488.56,276.24' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,225.25 488.56,225.25' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,174.26 488.56,174.26' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,123.26 488.56,123.26' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='33.10,72.27 488.56,72.27' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='49.62,314.87 49.62,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='154.18,314.87 154.18,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='258.74,314.87 258.74,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='363.30,314.87 363.30,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline points='467.86,314.87 467.86,66.51' fill='none' stroke='#EBEBEB' stroke-opacity='1' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt'/>\n   <polyline id='svg_13db3af7_5c79_452f_b102_263548f29962_e1' points='53.80,174.26 57.98,170.53 62.16,173.53 66.35,173.52 70.53,162.23 74.71,157.29 78.89,153.37 83.08,159.02 87.26,163.03 91.44,151.39 95.62,156.96 99.81,155.87 103.99,151.32 108.17,146.12 112.35,140.57 116.53,141.40 120.72,145.58 124.90,147.15 129.08,151.75 133.26,148.55 137.45,142.84 141.63,131.99 145.81,130.13 149.99,134.59 154.18,129.36 158.36,124.75 162.54,125.96 166.72,133.91 170.91,130.03 175.09,124.27 179.27,125.77 183.45,123.04 187.64,124.45 191.82,120.97 196.00,121.56 200.18,123.32 204.36,122.75 208.55,124.20 212.73,127.21 216.91,128.82 221.09,128.86 225.28,127.81 229.46,119.99 233.64,126.92 237.82,127.93 242.01,124.71 246.19,115.73 250.37,113.56 254.55,113.63 258.74,115.20 262.92,113.08 267.10,108.04 271.28,108.98 275.47,108.14 279.65,107.04 283.83,103.32 288.01,97.65 292.19,96.23 296.38,96.61 300.56,89.50 304.74,88.66 308.92,80.62 313.11,80.93 317.29,77.80 321.47,85.69 325.65,86.26 329.84,86.37 334.02,90.24 338.20,95.52 342.38,90.68 346.57,86.02 350.75,92.65 354.93,90.48 359.11,96.15 363.30,101.51 367.48,98.84 371.66,102.33 375.84,97.27 380.02,97.07 384.21,94.34 388.39,97.01 392.57,102.88 396.75,98.21 400.94,97.00 405.12,98.22 409.30,97.87 413.48,91.10 417.67,94.66 421.85,98.49 426.03,101.65 430.21,109.73 434.40,105.55 438.58,104.57 442.76,103.51 446.94,103.73 451.13,106.33 455.31,110.53 459.49,106.19 463.67,113.46 467.86,111.22' fill='none' stroke='#F8766D' stroke-opacity='0.7' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' data-id='16' title='Walk Number: 16 | Step: 1 | y: 0'/>\n   <polyline id='svg_13db3af7_5c79_452f_b102_263548f29962_e2' points='53.80,174.26 57.98,178.27 62.16,184.64 66.35,190.13 70.53,188.86 74.71,189.47 78.89,191.00 83.08,202.84 87.26,209.59 91.44,210.26 95.62,214.74 99.81,218.80 103.99,213.45 108.17,212.55 112.35,217.88 116.53,220.27 120.72,221.72 124.90,225.19 129.08,230.10 133.26,230.37 137.45,226.59 141.63,225.43 145.81,226.45 149.99,224.48 154.18,220.43 158.36,221.14 162.54,218.82 166.72,224.66 170.91,225.93 175.09,228.08 179.27,227.08 183.45,225.26 187.64,225.89 191.82,229.80 196.00,234.54 200.18,233.12 204.36,226.20 208.55,230.21 212.73,232.17 216.91,230.49 221.09,233.31 225.28,232.69 229.46,232.94 233.64,236.90 237.82,232.66 242.01,228.34 246.19,223.12 250.37,216.65 254.55,219.23 258.74,221.60 262.92,220.27 267.10,217.06 271.28,218.79 275.47,220.95 279.65,224.10 283.83,216.54 288.01,229.33 292.19,230.19 296.38,229.99 300.56,235.40 304.74,233.43 308.92,243.46 313.11,238.59 317.29,247.07 321.47,258.31 325.65,262.20 329.84,261.37 334.02,264.70 338.20,267.55 342.38,265.85 346.57,271.25 350.75,271.69 354.93,269.15 359.11,260.82 363.30,258.37 367.48,249.63 371.66,247.32 375.84,247.33 380.02,258.84 384.21,265.08 388.39,266.71 392.57,263.08 396.75,264.72 400.94,261.95 405.12,267.38 409.30,268.77 413.48,267.67 417.67,269.50 421.85,268.92 426.03,272.34 430.21,270.44 434.40,270.85 438.58,271.09 442.76,281.03 446.94,284.46 451.13,292.06 455.31,300.25 459.49,302.76 463.67,303.58 467.86,302.14' fill='none' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='1.07' stroke-linejoin='round' stroke-linecap='butt' data-id='22' title='Walk Number: 22 | Step: 1 | y: 0'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e3' cx='53.8' cy='174.26' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 1 | y: 0'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e4' cx='57.98' cy='170.53' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 2 | y: 0.73'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e5' cx='62.16' cy='173.53' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 3 | y: 0.143'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e6' cx='66.35' cy='173.52' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 4 | y: 0.143'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e7' cx='70.53' cy='162.23' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 5 | y: 2.358'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e8' cx='74.71' cy='157.29' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 6 | y: 3.327'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e9' cx='78.89' cy='153.37' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 7 | y: 4.095'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e10' cx='83.08' cy='159.02' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 8 | y: 2.987'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e11' cx='87.26' cy='163.03' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 9 | y: 2.201'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e12' cx='91.44' cy='151.39' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 10 | y: 4.485'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e13' cx='95.62' cy='156.96' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 11 | y: 3.392'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e14' cx='99.81' cy='155.87' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 12 | y: 3.606'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e15' cx='103.99' cy='151.32' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 13 | y: 4.499'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e16' cx='108.17' cy='146.12' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 14 | y: 5.517'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e17' cx='112.35' cy='140.57' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 15 | y: 6.606'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e18' cx='116.53' cy='141.4' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 16 | y: 6.443'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e19' cx='120.72' cy='145.58' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 17 | y: 5.622'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e20' cx='124.9' cy='147.15' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 18 | y: 5.315'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e21' cx='129.08' cy='151.75' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 19 | y: 4.413'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e22' cx='133.26' cy='148.55' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 20 | y: 5.04'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e23' cx='137.45' cy='142.84' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 21 | y: 6.16'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e24' cx='141.63' cy='131.99' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 22 | y: 8.288'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e25' cx='145.81' cy='130.13' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 23 | y: 8.654'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e26' cx='149.99' cy='134.59' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 24 | y: 7.779'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e27' cx='154.18' cy='129.36' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 25 | y: 8.803'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e28' cx='158.36' cy='124.75' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 26 | y: 9.708'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e29' cx='162.54' cy='125.96' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 27 | y: 9.47'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e30' cx='166.72' cy='133.91' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 28 | y: 7.912'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e31' cx='170.91' cy='130.03' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 29 | y: 8.673'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e32' cx='175.09' cy='124.27' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 30 | y: 9.803'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e33' cx='179.27' cy='125.77' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 31 | y: 9.507'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e34' cx='183.45' cy='123.04' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 32 | y: 10.044'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e35' cx='187.64' cy='124.45' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 33 | y: 9.768'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e36' cx='191.82' cy='120.97' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 34 | y: 10.45'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e37' cx='196' cy='121.56' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 35 | y: 10.333'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e38' cx='200.18' cy='123.32' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 36 | y: 9.988'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e39' cx='204.36' cy='122.75' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 37 | y: 10.1'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e40' cx='208.55' cy='124.2' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 38 | y: 9.816'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e41' cx='212.73' cy='127.21' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 39 | y: 9.225'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e42' cx='216.91' cy='128.82' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 40 | y: 8.909'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e43' cx='221.09' cy='128.86' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 41 | y: 8.901'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e44' cx='225.28' cy='127.81' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 42 | y: 9.109'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e45' cx='229.46' cy='119.99' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 43 | y: 10.641'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e46' cx='233.64' cy='126.92' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 44 | y: 9.283'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e47' cx='237.82' cy='127.93' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 45 | y: 9.084'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e48' cx='242.01' cy='124.71' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 46 | y: 9.715'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e49' cx='246.19' cy='115.73' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 47 | y: 11.477'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e50' cx='250.37' cy='113.56' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 48 | y: 11.903'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e51' cx='254.55' cy='113.63' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 49 | y: 11.889'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e52' cx='258.74' cy='115.2' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 50 | y: 11.582'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e53' cx='262.92' cy='113.08' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 51 | y: 11.996'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e54' cx='267.1' cy='108.04' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 52 | y: 12.985'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e55' cx='271.28' cy='108.98' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 53 | y: 12.801'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e56' cx='275.47' cy='108.14' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 54 | y: 12.965'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e57' cx='279.65' cy='107.04' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 55 | y: 13.182'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e58' cx='283.83' cy='103.32' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 56 | y: 13.911'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e59' cx='288.01' cy='97.65' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 57 | y: 15.023'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e60' cx='292.19' cy='96.23' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 58 | y: 15.302'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e61' cx='296.38' cy='96.61' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 59 | y: 15.226'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e62' cx='300.56' cy='89.5' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 60 | y: 16.62'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e63' cx='304.74' cy='88.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 61 | y: 16.785'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e64' cx='308.92' cy='80.62' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 62 | y: 18.363'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e65' cx='313.11' cy='80.93' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 63 | y: 18.301'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e66' cx='317.29' cy='77.8' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 64 | y: 18.915'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e67' cx='321.47' cy='85.69' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 65 | y: 17.369'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e68' cx='325.65' cy='86.26' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 66 | y: 17.256'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e69' cx='329.84' cy='86.37' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 67 | y: 17.234'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e70' cx='334.02' cy='90.24' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 68 | y: 16.476'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e71' cx='338.2' cy='95.52' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 69 | y: 15.44'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e72' cx='342.38' cy='90.68' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 70 | y: 16.388'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e73' cx='346.57' cy='86.02' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 71 | y: 17.302'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e74' cx='350.75' cy='92.65' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 72 | y: 16.004'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e75' cx='354.93' cy='90.48' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 73 | y: 16.428'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e76' cx='359.11' cy='96.15' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 74 | y: 15.316'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e77' cx='363.3' cy='101.51' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 75 | y: 14.265'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e78' cx='367.48' cy='98.84' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 76 | y: 14.79'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e79' cx='371.66' cy='102.33' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 77 | y: 14.104'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e80' cx='375.84' cy='97.27' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 78 | y: 15.097'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e81' cx='380.02' cy='97.07' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 79 | y: 15.136'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e82' cx='384.21' cy='94.34' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 80 | y: 15.672'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e83' cx='388.39' cy='97.01' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 81 | y: 15.148'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e84' cx='392.57' cy='102.88' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 82 | y: 13.997'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e85' cx='396.75' cy='98.21' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 83 | y: 14.912'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e86' cx='400.94' cy='97' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 84 | y: 15.15'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e87' cx='405.12' cy='98.22' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 85 | y: 14.911'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e88' cx='409.3' cy='97.87' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 86 | y: 14.98'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e89' cx='413.48' cy='91.1' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 87 | y: 16.306'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e90' cx='417.67' cy='94.66' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 88 | y: 15.608'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e91' cx='421.85' cy='98.49' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 89 | y: 14.859'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e92' cx='426.03' cy='101.65' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 90 | y: 14.239'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e93' cx='430.21' cy='109.73' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 91 | y: 12.654'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e94' cx='434.4' cy='105.55' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 92 | y: 13.474'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e95' cx='438.58' cy='104.57' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 93 | y: 13.666'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e96' cx='442.76' cy='103.51' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 94 | y: 13.873'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e97' cx='446.94' cy='103.73' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 95 | y: 13.83'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e98' cx='451.13' cy='106.33' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 96 | y: 13.32'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e99' cx='455.31' cy='110.53' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 97 | y: 12.496'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e100' cx='459.49' cy='106.19' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 98 | y: 13.348'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e101' cx='463.67' cy='113.46' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 99 | y: 11.922'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e102' cx='467.86' cy='111.22' r='0.35pt' fill='#F8766D' fill-opacity='0.7' stroke='#F8766D' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='16' title='Walk Number: 16 | Step: 100 | y: 12.362'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e103' cx='53.8' cy='174.26' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 1 | y: 0'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e104' cx='57.98' cy='178.27' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 2 | y: -0.787'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e105' cx='62.16' cy='184.64' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 3 | y: -2.036'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e106' cx='66.35' cy='190.13' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 4 | y: -3.114'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e107' cx='70.53' cy='188.86' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 5 | y: -2.864'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e108' cx='74.71' cy='189.47' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 6 | y: -2.983'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e109' cx='78.89' cy='191' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 7 | y: -3.284'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e110' cx='83.08' cy='202.84' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 8 | y: -5.605'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e111' cx='87.26' cy='209.59' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 9 | y: -6.929'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e112' cx='91.44' cy='210.26' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 10 | y: -7.061'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e113' cx='95.62' cy='214.74' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 11 | y: -7.939'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e114' cx='99.81' cy='218.8' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 12 | y: -8.735'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e115' cx='103.99' cy='213.45' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 13 | y: -7.686'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e116' cx='108.17' cy='212.55' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 14 | y: -7.51'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e117' cx='112.35' cy='217.88' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 15 | y: -8.554'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e118' cx='116.53' cy='220.27' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 16 | y: -9.023'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e119' cx='120.72' cy='221.72' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 17 | y: -9.308'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e120' cx='124.9' cy='225.19' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 18 | y: -9.988'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e121' cx='129.08' cy='230.1' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 19 | y: -10.952'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e122' cx='133.26' cy='230.37' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 20 | y: -11.004'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e123' cx='137.45' cy='226.59' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 21 | y: -10.263'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e124' cx='141.63' cy='225.43' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 22 | y: -10.036'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e125' cx='145.81' cy='226.45' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 23 | y: -10.236'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e126' cx='149.99' cy='224.48' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 24 | y: -9.848'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e127' cx='154.18' cy='220.43' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 25 | y: -9.054'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e128' cx='158.36' cy='221.14' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 26 | y: -9.195'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e129' cx='162.54' cy='218.82' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 27 | y: -8.739'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e130' cx='166.72' cy='224.66' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 28 | y: -9.885'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e131' cx='170.91' cy='225.93' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 29 | y: -10.134'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e132' cx='175.09' cy='228.08' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 30 | y: -10.554'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e133' cx='179.27' cy='227.08' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 31 | y: -10.359'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e134' cx='183.45' cy='225.26' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 32 | y: -10.002'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e135' cx='187.64' cy='225.89' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 33 | y: -10.125'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e136' cx='191.82' cy='229.8' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 34 | y: -10.891'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e137' cx='196' cy='234.54' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 35 | y: -11.821'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e138' cx='200.18' cy='233.12' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 36 | y: -11.543'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e139' cx='204.36' cy='226.2' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 37 | y: -10.186'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e140' cx='208.55' cy='230.21' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 38 | y: -10.973'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e141' cx='212.73' cy='232.17' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 39 | y: -11.358'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e142' cx='216.91' cy='230.49' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 40 | y: -11.027'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e143' cx='221.09' cy='233.31' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 41 | y: -11.582'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e144' cx='225.28' cy='232.69' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 42 | y: -11.46'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e145' cx='229.46' cy='232.94' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 43 | y: -11.508'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e146' cx='233.64' cy='236.9' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 44 | y: -12.284'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e147' cx='237.82' cy='232.66' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 45 | y: -11.452'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e148' cx='242.01' cy='228.34' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 46 | y: -10.606'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e149' cx='246.19' cy='223.12' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 47 | y: -9.582'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e150' cx='250.37' cy='216.65' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 48 | y: -8.314'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e151' cx='254.55' cy='219.23' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 49 | y: -8.82'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e152' cx='258.74' cy='221.6' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 50 | y: -9.285'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e153' cx='262.92' cy='220.27' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 51 | y: -9.024'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e154' cx='267.1' cy='217.06' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 52 | y: -8.394'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e155' cx='271.28' cy='218.79' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 53 | y: -8.733'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e156' cx='275.47' cy='220.95' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 54 | y: -9.156'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e157' cx='279.65' cy='224.1' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 55 | y: -9.775'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e158' cx='283.83' cy='216.54' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 56 | y: -8.293'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e159' cx='288.01' cy='229.33' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 57 | y: -10.801'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e160' cx='292.19' cy='230.19' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 58 | y: -10.968'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e161' cx='296.38' cy='229.99' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 59 | y: -10.93'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e162' cx='300.56' cy='235.4' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 60 | y: -11.99'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e163' cx='304.74' cy='233.43' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 61 | y: -11.604'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e164' cx='308.92' cy='243.46' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 62 | y: -13.571'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e165' cx='313.11' cy='238.59' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 63 | y: -12.616'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e166' cx='317.29' cy='247.07' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 64 | y: -14.28'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e167' cx='321.47' cy='258.31' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 65 | y: -16.482'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e168' cx='325.65' cy='262.2' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 66 | y: -17.246'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e169' cx='329.84' cy='261.37' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 67 | y: -17.084'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e170' cx='334.02' cy='264.7' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 68 | y: -17.736'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e171' cx='338.2' cy='267.55' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 69 | y: -18.295'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e172' cx='342.38' cy='265.85' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 70 | y: -17.962'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e173' cx='346.57' cy='271.25' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 71 | y: -19.021'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e174' cx='350.75' cy='271.69' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 72 | y: -19.106'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e175' cx='354.93' cy='269.15' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 73 | y: -18.608'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e176' cx='359.11' cy='260.82' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 74 | y: -16.974'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e177' cx='363.3' cy='258.37' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 75 | y: -16.495'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e178' cx='367.48' cy='249.63' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 76 | y: -14.78'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e179' cx='371.66' cy='247.32' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 77 | y: -14.327'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e180' cx='375.84' cy='247.33' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 78 | y: -14.33'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e181' cx='380.02' cy='258.84' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 79 | y: -16.587'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e182' cx='384.21' cy='265.08' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 80 | y: -17.811'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e183' cx='388.39' cy='266.71' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 81 | y: -18.13'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e184' cx='392.57' cy='263.08' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 82 | y: -17.418'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e185' cx='396.75' cy='264.72' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 83 | y: -17.741'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e186' cx='400.94' cy='261.95' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 84 | y: -17.197'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e187' cx='405.12' cy='267.38' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 85 | y: -18.261'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e188' cx='409.3' cy='268.77' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 86 | y: -18.535'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e189' cx='413.48' cy='267.67' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 87 | y: -18.318'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e190' cx='417.67' cy='269.5' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 88 | y: -18.677'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e191' cx='421.85' cy='268.92' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 89 | y: -18.565'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e192' cx='426.03' cy='272.34' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 90 | y: -19.235'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e193' cx='430.21' cy='270.44' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 91 | y: -18.861'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e194' cx='434.4' cy='270.85' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 92 | y: -18.942'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e195' cx='438.58' cy='271.09' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 93 | y: -18.989'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e196' cx='442.76' cy='281.03' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 94 | y: -20.938'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e197' cx='446.94' cy='284.46' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 95 | y: -21.612'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e198' cx='451.13' cy='292.06' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 96 | y: -23.101'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e199' cx='455.31' cy='300.25' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 97 | y: -24.707'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e200' cx='459.49' cy='302.76' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 98 | y: -25.201'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e201' cx='463.67' cy='303.58' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 99 | y: -25.362'/>\n   <circle id='svg_13db3af7_5c79_452f_b102_263548f29962_e202' cx='467.86' cy='302.14' r='0.35pt' fill='#00BFC4' fill-opacity='0.7' stroke='#00BFC4' stroke-opacity='0.7' stroke-width='0.71' stroke-linejoin='round' stroke-linecap='round' data-id='22' title='Walk Number: 22 | Step: 100 | y: -25.078'/>\n  <\/g>\n  <g clip-path='url(#svg_13db3af7_5c79_452f_b102_263548f29962_c1)'>\n   <text x='15.44' y='279.39' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>-20<\/text>\n   <text x='15.44' y='228.4' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>-10<\/text>\n   <text x='23.27' y='177.41' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>0<\/text>\n   <text x='18.37' y='126.41' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>10<\/text>\n   <text x='18.37' y='75.42' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>20<\/text>\n   <text x='47.17' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>0<\/text>\n   <text x='149.28' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>25<\/text>\n   <text x='253.84' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>50<\/text>\n   <text x='358.4' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>75<\/text>\n   <text x='460.51' y='326.11' font-size='6.6pt' font-family='Arial' fill='#4D4D4D' fill-opacity='1'>100<\/text>\n   <text x='249.51' y='338.57' font-size='8.25pt' font-family='Arial'>Step<\/text>\n   <text x='33.1' y='58.26' font-size='9.9pt' font-family='Arial'>y<\/text>\n   <text x='330.01' y='352.67' font-size='6.6pt' font-family='Arial'>1 dimensions, 100 steps, mu = 0, sd = 1.<\/text>\n   <text x='15.44' y='31.05' font-size='8.25pt' font-family='Arial'>Function: rw30<\/text>\n   <text x='15.44' y='14.93' font-size='9.9pt' font-family='Arial'>30 Random Walks<\/text>\n  <\/g>\n <\/g>\n<\/svg>","js":null,"uid":"svg_13db3af7_5c79_452f_b102_263548f29962","ratio":1.4,"settings":{"tooltip":{"css":".tooltip_SVGID_ { padding:5px;background:black;color:white;border-radius:2px 2px 2px 2px;text-align:left; ; position:absolute;pointer-events:none;z-index:999;}","placement":"doc","opacity":0.7,"offx":200,"offy":5,"use_cursor_pos":false,"use_fill":false,"use_stroke":false,"delay_over":200,"delay_out":500},"hover":{"css":".hover_data_SVGID_ { stroke:black;stroke-width:2pt; }","reactive":false,"nearest_distance":null},"hover_inv":{"css":".hover_inv_SVGID_ { opacity:0.4; }"},"hover_key":{"css":".hover_key_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_key_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_key_SVGID_ { fill:orange;stroke:black; }\nline.hover_key_SVGID_, polyline.hover_key_SVGID_ { fill:none;stroke:orange; }\nrect.hover_key_SVGID_, polygon.hover_key_SVGID_, path.hover_key_SVGID_ { fill:orange;stroke:none; }\nimage.hover_key_SVGID_ { stroke:orange; }","reactive":true},"hover_theme":{"css":".hover_theme_SVGID_ { fill:orange;stroke:black;cursor:pointer; }\ntext.hover_theme_SVGID_ { stroke:none;fill:orange; }\ncircle.hover_theme_SVGID_ { fill:orange;stroke:black; }\nline.hover_theme_SVGID_, polyline.hover_theme_SVGID_ { fill:none;stroke:orange; }\nrect.hover_theme_SVGID_, polygon.hover_theme_SVGID_, path.hover_theme_SVGID_ { fill:orange;stroke:none; }\nimage.hover_theme_SVGID_ { stroke:orange; }","reactive":true},"select":{"css":".select_data_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_data_SVGID_ { stroke:none;fill:red; }\ncircle.select_data_SVGID_ { fill:red;stroke:black; }\nline.select_data_SVGID_, polyline.select_data_SVGID_ { fill:none;stroke:red; }\nrect.select_data_SVGID_, polygon.select_data_SVGID_, path.select_data_SVGID_ { fill:red;stroke:none; }\nimage.select_data_SVGID_ { stroke:red; }","type":"multiple","only_shiny":true,"selected":[]},"select_inv":{"css":""},"select_key":{"css":".select_key_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_key_SVGID_ { stroke:none;fill:red; }\ncircle.select_key_SVGID_ { fill:red;stroke:black; }\nline.select_key_SVGID_, polyline.select_key_SVGID_ { fill:none;stroke:red; }\nrect.select_key_SVGID_, polygon.select_key_SVGID_, path.select_key_SVGID_ { fill:red;stroke:none; }\nimage.select_key_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"select_theme":{"css":".select_theme_SVGID_ { fill:red;stroke:black;cursor:pointer; }\ntext.select_theme_SVGID_ { stroke:none;fill:red; }\ncircle.select_theme_SVGID_ { fill:red;stroke:black; }\nline.select_theme_SVGID_, polyline.select_theme_SVGID_ { fill:none;stroke:red; }\nrect.select_theme_SVGID_, polygon.select_theme_SVGID_, path.select_theme_SVGID_ { fill:red;stroke:none; }\nimage.select_theme_SVGID_ { stroke:red; }","type":"single","only_shiny":true,"selected":[]},"zoom":{"min":1,"max":5,"duration":300},"toolbar":{"position":"topright","pngname":"diagram","tooltips":null,"fixed":false,"hidden":[],"delay_over":200,"delay_out":500},"sizing":{"rescale":true,"width":1}}},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</section>
<section id="multi-column-visualization-with-visualize_walks" class="level2">
<h2 class="anchored" data-anchor-id="multi-column-visualization-with-visualize_walks"><strong>Multi-Column Visualization with <code>visualize_walks()</code></strong></h2>
<p>The enhanced <code>visualize_walks()</code> function now accepts vector inputs for the <code>.pluck</code> parameter, enabling simultaneous visualization of multiple walk types or simulations :</p>
<div class="cell">
<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;">set.seed</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">123</span>)</span>
<span id="cb6-2">rw <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;">random_normal_walk</span>()</span>
<span id="cb6-3"></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Visualize specific simulations</span></span>
<span id="cb6-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">visualize_walks</span>(rw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.pluck =</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;">3</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-08-19/index_files/figure-html/pluck_visualizations-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Compare multiple distribution types</span></span>
<span id="cb7-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">visualize_walks</span>(rw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.pluck =</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;">"cum_min"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cum_max"</span>))</span></code></pre></div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-08-19/index_files/figure-html/pluck_visualizations-2.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
</section>
</section>
<section id="multi-dimensional-modeling-revolution" class="level1">
<h1><strong>Multi-Dimensional Modeling Revolution</strong></h1>
<p>All 21 generator functions support multi-dimensional random walks through the <code>.dimensions</code> parameter, breaking the traditional 1D limitation. This capability transforms spatial modeling applications:</p>
<div class="cell">
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 2D random walks for spatial modeling</span></span>
<span id="cb8-2">walk_2d <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;">random_normal_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb8-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(walk_2d, <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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>            [,1]         
walk_number "1"          
step_number "1"          
x           "0.03563341" 
y           "-0.01747179"
cum_sum_x   "0.03563341" 
cum_sum_y   "-0.01747179"
cum_prod_x  "0"          
cum_prod_y  "0"          
cum_min_x   "0.03563341" 
cum_min_y   "-0.01747179"
cum_max_x   "0.03563341" 
cum_max_y   "-0.01747179"
cum_mean_x  "0.03563341" 
cum_mean_y  "-0.01747179"</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 3D random walks for complex spatial analysis  </span></span>
<span id="cb10-2">walk_3d <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;">random_uniform_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb10-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(walk_3d, <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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>            [,1]       
walk_number "1"        
step_number "1"        
x           "0.3000542"
y           "0.8856108"
z           "0.6017601"
cum_sum_x   "0.3000542"
cum_sum_y   "0.8856108"
cum_sum_z   "0.6017601"
cum_prod_x  "0"        
cum_prod_y  "0"        
cum_prod_z  "0"        
cum_min_x   "0.3000542"
cum_min_y   "0.8856108"
cum_min_z   "0.6017601"
cum_max_x   "0.3000542"
cum_max_y   "0.8856108"
cum_max_z   "0.6017601"
cum_mean_x  "0.3000542"
cum_mean_y  "0.8856108"
cum_mean_z  "0.6017601"</code></pre>
</div>
<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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Multi-dimensional discrete processes</span></span>
<span id="cb12-2">poisson_2d <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;">random_poisson_walk</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.dimensions =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb12-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(poisson_2d, <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;">|&gt;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">t</span>()</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>            [,1]
walk_number "1" 
step_number "1" 
x           "3" 
y           "1" 
cum_sum_x   "3" 
cum_sum_y   "1" 
cum_prod_x  "0" 
cum_prod_y  "0" 
cum_min_x   "3" 
cum_min_y   "1" 
cum_max_x   "3" 
cum_max_y   "1" 
cum_mean_x  "3" 
cum_mean_y  "1" </code></pre>
</div>
</div>
</section>
<section id="real-world-applications-across-industries" class="level1">
<h1><strong>Real-World Applications Across Industries</strong></h1>
<section id="financial-modeling-excellence" class="level2">
<h2 class="anchored" data-anchor-id="financial-modeling-excellence"><strong>Financial Modeling Excellence</strong></h2>
<p>The expanded distribution set revolutionizes financial applications:</p>
<ul>
<li><strong>Asset Pricing</strong>: Use <code>random_lognormal_walk()</code> for realistic stock price simulations</li>
<li><strong>Risk Management</strong>: Apply <code>random_weibull_walk()</code> for failure time analysis in portfolios</li>
<li><strong>Heavy-Tail Modeling</strong>: Leverage <code>random_cauchy_walk()</code> for extreme market events</li>
</ul>
</section>
<section id="engineering-and-reliability-analysis" class="level2">
<h2 class="anchored" data-anchor-id="engineering-and-reliability-analysis"><strong>Engineering and Reliability Analysis</strong></h2>
<p>Engineering applications benefit from specialized distributions:</p>
<ul>
<li><strong>Failure Analysis</strong>: <code>random_weibull_walk()</code> and <code>random_exponential_walk()</code> for system reliability</li>
<li><strong>Quality Control</strong>: <code>random_binomial_walk()</code> and <code>random_hypergeometric_walk()</code> for sampling processes</li>
<li><strong>System Testing</strong>: <code>random_chisquared_walk()</code> and <code>random_f_walk()</code> for statistical validation</li>
</ul>
</section>
<section id="biological-and-ecological-modeling" class="level2">
<h2 class="anchored" data-anchor-id="biological-and-ecological-modeling"><strong>Biological and Ecological Modeling</strong></h2>
<p>Life sciences gain powerful new modeling tools:</p>
<ul>
<li><strong>Population Dynamics</strong>: <code>random_negbinomial_walk()</code> for over-dispersed population counts</li>
<li><strong>Disease Modeling</strong>: <code>random_poisson_walk()</code> and <code>random_multinomial_walk()</code> for epidemic spread</li>
<li><strong>Genetic Analysis</strong>: <code>random_hypergeometric_walk()</code> for sampling genetic variants</li>
</ul>
</section>
</section>
<section id="your-turn" class="level1">
<h1><strong>Your Turn!</strong></h1>
<p><strong>Challenge</strong>: Create a portfolio simulation comparing three different risk models using normal, t-distribution, and Weibull random walks.</p>
<p><strong>Task</strong>:</p>
<ol type="1">
<li>Generate 1000-step walks for each distribution</li>
<li>Calculate risk metrics (max drawdown, volatility)</li>
<li>Compare performance characteristics</li>
</ol>
<details>
<summary>
Click here for Solution!
</summary>
<div class="cell">
<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"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Portfolio simulation with different risk models</span></span>
<span id="cb14-2">normal_portfolio <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;">random_normal_walk</span>(</span>
<span id="cb14-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, </span>
<span id="cb14-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, </span>
<span id="cb14-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.mu =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.05</span>, </span>
<span id="cb14-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.sd =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.2</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(walk_number, y)</span>
<span id="cb14-8"></span>
<span id="cb14-9">t_portfolio <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;">random_t_walk</span>(</span>
<span id="cb14-10">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, </span>
<span id="cb14-11">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, </span>
<span id="cb14-12">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.df =</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;">|&gt;</span></span>
<span id="cb14-13">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(walk_number, y)</span>
<span id="cb14-14"></span>
<span id="cb14-15">weibull_portfolio <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;">random_weibull_walk</span>(</span>
<span id="cb14-16">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.n =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, </span>
<span id="cb14-17">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.num_walks =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>, </span>
<span id="cb14-18">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.shape =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.5</span>, </span>
<span id="cb14-19">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.scale =</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;">|&gt;</span></span>
<span id="cb14-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(walk_number, y)</span>
<span id="cb14-21"></span>
<span id="cb14-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate risk metrics grouped on walk_number with y as the value</span></span>
<span id="cb14-23">calculate_risk_metrics <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>(walks) {</span>
<span id="cb14-24">  max_drawdowns <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> walks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span> </span>
<span id="cb14-25">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(walk_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-26">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_drawdowns =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">max</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cummax</span>(y) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-27">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-28">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(max_drawdowns)</span>
<span id="cb14-29">  volatility <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> walks <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-30">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(walk_number) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-31">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarize</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">volatility =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sd</span>(y)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-32">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ungroup</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">|&gt;</span></span>
<span id="cb14-33">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>(volatility)</span>
<span id="cb14-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb14-35">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">max_drawdown =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(max_drawdowns), </span>
<span id="cb14-36">      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">volatility =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mean</span>(volatility))</span>
<span id="cb14-37">      )</span>
<span id="cb14-38">}</span>
<span id="cb14-39"></span>
<span id="cb14-40"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">calculate_risk_metrics</span>(normal_portfolio)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$max_drawdown
[1] 1.200971

$volatility
[1] 0.2005195</code></pre>
</div>
<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;">calculate_risk_metrics</span>(t_portfolio)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$max_drawdown
[1] 21.07394

$volatility
[1] 1.698124</code></pre>
</div>
<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;">calculate_risk_metrics</span>(weibull_portfolio)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$max_drawdown
[1] 3.641576

$volatility
[1] 0.611113</code></pre>
</div>
</div>
</details>
</section>
<section id="quick-takeaways-key-points-for-r-programmers" class="level1">
<h1><strong>Quick Takeaways: Key Points for R Programmers</strong></h1>
<p>• <strong>Complete Coverage</strong>: 21 new distribution-based generators plus 2 enhanced utilities cover every major statistical distribution family</p>
<p>• <strong>Multi-Dimensional</strong>: All functions support 2D and 3D random walks through the <code>.dimensions</code> parameter</p>
<p>• <strong>Enhanced Utilities</strong>: <code>subset_walks()</code> gains <code>.value</code> parameter flexibility; <code>visualize_walks()</code> supports vector <code>.pluck</code> inputs</p>
<p>• <strong>Custom Flexibility</strong>: <code>random_displacement_walk()</code> enables unlimited user-defined step distributions</p>
<p>• <strong>Tidyverse Integration</strong>: Full compatibility with dplyr, ggplot2, and pipe-friendly workflows</p>
<p>• <strong>Industry Applications</strong>: Purpose-built functions for finance, engineering, biology, and statistical testing</p>
</section>
<section id="migration-and-best-practices" class="level1">
<h1><strong>Migration and Best Practices</strong></h1>
<p>Existing RandomWalker users can seamlessly integrate the new functions while maintaining backward compatibility. The package maintains its tidyverse-friendly design philosophy, ensuring smooth integration with existing R workflows .</p>
<p><strong>Documentation</strong>: Each function includes comprehensive documentation with parameter specifications, use cases, and practical examples.</p>
</section>
<section id="conclusion-the-future-of-stochastic-modeling-in-r" class="level1">
<h1><strong>Conclusion: The Future of Stochastic Modeling in R</strong></h1>
<p>The RandomWalker v0.3.0 update represents a paradigm shift in R-based stochastic modeling. With 23 new and enhanced functions covering the complete spectrum of statistical distributions, R programmers now have unprecedented power to model complex random processes across any domain.</p>
<p>Whether you’re simulating financial markets with heavy tailed distributions, modeling biological populations with discrete processes, or conducting advanced statistical testing with nonparametric approaches, RandomWalker delivers the tools needed for sophisticated analysis.</p>
<p><strong>Ready to explore the new capabilities?</strong> Install the latest version and discover how these powerful new functions can transform your stochastic modeling projects. The future of random walk simulation in R has arrived.</p>
<hr>
<p><em>Have you tried the new RandomWalker functions? Share your experiences and applications in the comments below, and don’t forget to spread the word about these exciting updates on social media!</em></p>
<hr>
<p>Happy Coding! 🚀</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://www.spsanderson.com/steveondata/posts/2025-08-19/todays_post.png" class="img-fluid figure-img"></p>
<figcaption>Update to RandomWalker</figcaption>
</figure>
</div>
<hr>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>

 ]]></description>
  <category>code</category>
  <category>rtip</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-08-19/</guid>
  <pubDate>Tue, 19 Aug 2025 04:00:00 GMT</pubDate>
</item>
<item>
  <title>The Complete Beginner’s Guide to Python Debugging: Assertions, Exceptions, Logging, and More</title>
  <dc:creator>Steven P. Sanderson II, MPH</dc:creator>
  <link>https://www.spsanderson.com/steveondata/posts/2025-08-06/</link>
  <description><![CDATA[ 





<div class="cell">
<div class="cell-output cell-output-stdout">
<pre><code>exit</code></pre>
</div>
</div>
<blockquote class="blockquote">
<p><strong>Key Takeaway:</strong> Python debugging doesn’t have to be intimidating. With the right tools and techniques—assertions, exception handling, logging, and systematic debugging approaches—you can quickly identify and fix issues in your code.</p>
</blockquote>
<p><em>Author’s Note: Dear Reader, I want to be completely honest with you from the start: I am learning Python debugging as I write this series. This isn’t coming from someone who has mastered every aspect of Python development, it’s coming from someone who is actively working through these concepts, making mistakes, and discovering better ways to debug code.</em></p>
<p><strong>P.S. - Keep a debugging journal! I wish I had started one earlier. Writing down the problems you solve helps you recognize patterns and builds your debugging intuition over time. Hopefully this blog series will serve as that.</strong></p>
<p><strong>Here is a list of links that will continue to grow and hopefully help out: <a href="https://app.dotadda.io/teams/ab732481-52f3-4388-896c-23d34e828b35/dots?date=2025-08-16&amp;timespan=month">Python on Dots</a></strong></p>
<hr>
<section id="understanding-python-debugging-fundamentals" class="level1">
<h1>Understanding Python Debugging Fundamentals</h1>
<p><strong>Debugging</strong> is the process of finding and fixing errors (bugs) in your code . As a beginner Python programmer, you’ll encounter various types of errors that can seem overwhelming at first. However, with the right approach and tools, debugging becomes much more manageable.</p>
<p>Python provides several built-in tools and techniques to help you identify and resolve issues:</p>
<ul>
<li><strong>Assertions</strong> for checking assumptions during development</li>
<li><strong>Exception handling</strong> for managing runtime errors gracefully</li>
<li><strong>Logging</strong> for tracking program execution and events</li>
<li><strong>Debug statements</strong> and interactive debugging tools</li>
</ul>
<p>Let’s explore each of these techniques with practical, working examples.</p>
<hr>
</section>
<section id="mastering-python-assertions" class="level1">
<h1>Mastering Python Assertions</h1>
<p><strong>Assertions</strong> are statements that check if a condition is true at a specific point in your code . If the condition is false, Python raises an <code>AssertionError</code> and stops execution, helping you catch bugs early in development.</p>
<section id="basic-assertion-syntax" class="level2">
<h2 class="anchored" data-anchor-id="basic-assertion-syntax">Basic Assertion Syntax</h2>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> condition, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Optional error message"</span></span></code></pre></div>
<p>The assertion checks if the <code>condition</code> is <code>True</code>. If it’s <code>False</code>, Python raises an <code>AssertionError</code> with your optional message.</p>
</section>
<section id="working-example-input-validation" class="level2">
<h2 class="anchored" data-anchor-id="working-example-input-validation">Working Example: Input Validation</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> calculate_square_root(x):</span>
<span id="cb3-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Calculate square root with assertion check."""</span></span>
<span id="cb3-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Input must be non-negative for square root"</span></span>
<span id="cb3-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> x <span class="op" 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>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test successful case</span></span>
<span id="cb3-7"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Square root of 9 = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>calculate_square_root(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">9</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Works fine</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Square root of 9 = 3.0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test assertion failure</span></span>
<span id="cb5-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb5-3">    calculate_square_root(<span class="op" 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="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This will raise AssertionError</span></span>
<span id="cb5-4"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AssertionError</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb5-5">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"AssertionError: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>AssertionError: Input must be non-negative for square root</code></pre>
</div>
</div>
</section>
<section id="when-to-use-assertions" class="level2">
<h2 class="anchored" data-anchor-id="when-to-use-assertions">When to Use Assertions</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 57%">
<col style="width: 42%">
</colgroup>
<thead>
<tr class="header">
<th><strong>Use Assertions For</strong></th>
<th><strong>Don’t Use Assertions For</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Internal self-checks during development</td>
<td>Handling user input errors</td>
</tr>
<tr class="even">
<td>Verifying algorithm assumptions</td>
<td>Production error handling</td>
</tr>
<tr class="odd">
<td>Checking data structure integrity</td>
<td>Validating external data</td>
</tr>
<tr class="even">
<td>Testing function preconditions</td>
<td>Runtime error management</td>
</tr>
</tbody>
</table>
</section>
<section id="practical-assertion-example-data-validation" class="level2">
<h2 class="anchored" data-anchor-id="practical-assertion-example-data-validation">Practical Assertion Example: Data Validation</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> process_student_grades(grades):</span>
<span id="cb7-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Process a list of student grades with validation."""</span></span>
<span id="cb7-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(grades, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">list</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grades must be a list"</span></span>
<span id="cb7-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(grades) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Grades list cannot be empty"</span></span>
<span id="cb7-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">all</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> grade <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> grade <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> grades), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"All grades must be between 0 and 100"</span></span>
<span id="cb7-6">    </span>
<span id="cb7-7">    average <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">sum</span>(grades) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(grades)</span>
<span id="cb7-8">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">round</span>(average, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>)</span>
<span id="cb7-9"></span>
<span id="cb7-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Valid case</span></span>
<span id="cb7-11">valid_grades <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">96</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">88</span>]</span>
<span id="cb7-12"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Average grade: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>process_student_grades(valid_grades)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Average grade: 87.8</code></pre>
</div>
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Invalid case (grade out of range)</span></span>
<span id="cb9-2"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb9-3">    invalid_grades <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">85</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">92</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">105</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">78</span>]  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 105 is invalid</span></span>
<span id="cb9-4">    process_student_grades(invalid_grades)</span>
<span id="cb9-5"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AssertionError</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb9-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Validation failed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Validation failed: All grades must be between 0 and 100</code></pre>
</div>
</div>
<hr>
</section>
</section>
<section id="exception-handling-managing-runtime-errors" class="level1">
<h1>Exception Handling: Managing Runtime Errors</h1>
<p><strong>Exception handling</strong> allows your program to respond to runtime errors gracefully instead of crashing . Python uses a <code>try-except</code> structure to catch and handle different types of errors.</p>
<section id="common-python-exceptions" class="level2">
<h2 class="anchored" data-anchor-id="common-python-exceptions">Common Python Exceptions</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 36%">
<col style="width: 36%">
<col style="width: 26%">
</colgroup>
<thead>
<tr class="header">
<th><strong>Exception Type</strong></th>
<th><strong>When It Occurs</strong></th>
<th><strong>Example</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>ValueError</code></td>
<td>Invalid value for a function</td>
<td><code>int("abc")</code></td>
</tr>
<tr class="even">
<td><code>TypeError</code></td>
<td>Operation on incompatible types</td>
<td><code>"a" + 1</code></td>
</tr>
<tr class="odd">
<td><code>ZeroDivisionError</code></td>
<td>Division by zero</td>
<td><code>10 / 0</code></td>
</tr>
<tr class="even">
<td><code>IndexError</code></td>
<td>List index out of range</td>
<td>1, 2]<code>| |</code>KeyError<code>| Dictionary key not found |</code>[“missing”]<code>| |</code>FileNotFoundError<code>| File doesn't exist |</code>open(“missing.txt”)`</td>
</tr>
</tbody>
</table>
</section>
<section id="basic-exception-handling" class="level2">
<h2 class="anchored" data-anchor-id="basic-exception-handling">Basic Exception Handling</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> safe_divide(a, b):</span>
<span id="cb11-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Safely divide two numbers with exception handling."""</span></span>
<span id="cb11-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb11-4">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> a <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> b</span>
<span id="cb11-5">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result</span>
<span id="cb11-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ZeroDivisionError</span>:</span>
<span id="cb11-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error: Cannot divide </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>a<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> by zero!"</span>)</span>
<span id="cb11-8">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb11-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">TypeError</span>:</span>
<span id="cb11-10">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error: Both arguments must be numbers"</span>)</span>
<span id="cb11-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb11-12"></span>
<span id="cb11-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test cases</span></span>
<span id="cb11-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"10 ÷ 2 = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>safe_divide(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Works: 5.0</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>10 ÷ 2 = 5.0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"10 ÷ 0 = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>safe_divide(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Handles error gracefully</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Error: Cannot divide 10 by zero!
10 ÷ 0 = None</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"'hi' ÷ 5 = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>safe_divide(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'hi'</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Handles type error</span></span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Error: Both arguments must be numbers
'hi' ÷ 5 = None</code></pre>
</div>
</div>
</section>
<section id="complete-exception-handling-structure" class="level2">
<h2 class="anchored" data-anchor-id="complete-exception-handling-structure">Complete Exception Handling Structure</h2>
<div class="sourceCode" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> read_file_safely(filename):</span>
<span id="cb17-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Demonstrate complete exception handling structure."""</span></span>
<span id="cb17-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb17-4">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">with</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">open</span>(filename, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'r'</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>:</span>
<span id="cb17-5">            content <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>.read()</span>
<span id="cb17-6">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> content</span>
<span id="cb17-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">FileNotFoundError</span>:</span>
<span id="cb17-8">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error: File '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>filename<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">' not found"</span>)</span>
<span id="cb17-9">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb17-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">PermissionError</span>:</span>
<span id="cb17-11">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Error: Permission denied for '</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>filename<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">'"</span>)</span>
<span id="cb17-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb17-13">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb17-14">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This runs only if no exception occurred</span></span>
<span id="cb17-15">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Successfully read </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>filename<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb17-16">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">finally</span>:</span>
<span id="cb17-17">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This always runs</span></span>
<span id="cb17-18">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"File operation completed"</span>)</span></code></pre></div>
<p>The <code>try-except-else-finally</code> structure provides complete control: - <strong>try</strong>: Code that might raise an exception - <strong>except</strong>: Handle specific exceptions - <strong>else</strong>: Runs only if no exception occurs - <strong>finally</strong>: Always runs (cleanup code)</p>
<hr>
</section>
</section>
<section id="python-logging-better-than-print-statements" class="level1">
<h1>Python Logging: Better Than Print Statements</h1>
<p><strong>Logging</strong> is the process of recording events during program execution [[4]]. Unlike print statements, logging provides levels, timestamps, and flexible output options.</p>
<section id="logging-levels-explained" class="level2">
<h2 class="anchored" data-anchor-id="logging-levels-explained">Logging Levels Explained</h2>
<table class="caption-top table">
<colgroup>
<col style="width: 25%">
<col style="width: 29%">
<col style="width: 45%">
</colgroup>
<thead>
<tr class="header">
<th><strong>Level</strong></th>
<th><strong>Purpose</strong></th>
<th><strong>Example Use Case</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>DEBUG</code></td>
<td>Detailed diagnostic information</td>
<td>Variable values, function calls</td>
</tr>
<tr class="even">
<td><code>INFO</code></td>
<td>Confirmation things work as expected</td>
<td>Process completed successfully</td>
</tr>
<tr class="odd">
<td><code>WARNING</code></td>
<td>Something unexpected happened</td>
<td>Deprecated feature used</td>
</tr>
<tr class="even">
<td><code>ERROR</code></td>
<td>Serious problem occurred</td>
<td>Database connection failed</td>
</tr>
<tr class="odd">
<td><code>CRITICAL</code></td>
<td>Very serious error</td>
<td>System crash imminent</td>
</tr>
</tbody>
</table>
</section>
<section id="basic-logging-setup" class="level2">
<h2 class="anchored" data-anchor-id="basic-logging-setup">Basic Logging Setup</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb18-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> logging</span>
<span id="cb18-2"></span>
<span id="cb18-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Configure logging</span></span>
<span id="cb18-4">logging.basicConfig(</span>
<span id="cb18-5">    level<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>logging.DEBUG,</span>
<span id="cb18-6">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(asctime)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> - </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(levelname)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> - </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(message)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>,</span>
<span id="cb18-7">    datefmt<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'%H:%M:%S'</span></span>
<span id="cb18-8">)</span>
<span id="cb18-9"></span>
<span id="cb18-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create logger</span></span>
<span id="cb18-11">logger <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> logging.getLogger(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span>)</span>
<span id="cb18-12"></span>
<span id="cb18-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use different logging levels</span></span>
<span id="cb18-14">logger.debug(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This is detailed debug information"</span>)</span>
<span id="cb18-15">logger.info(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This confirms normal operation"</span>)</span>
<span id="cb18-16">logger.warning(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This warns about unexpected events"</span>)</span>
<span id="cb18-17">logger.error(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This reports serious problems"</span>)</span>
<span id="cb18-18">logger.critical(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"This reports critical system failures"</span>)</span></code></pre></div>
</div>
</section>
<section id="logging-in-functions-practical-example" class="level2">
<h2 class="anchored" data-anchor-id="logging-in-functions-practical-example">Logging in Functions: Practical Example</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb19-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> calculate_factorial(n):</span>
<span id="cb19-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Calculate factorial with comprehensive logging."""</span></span>
<span id="cb19-3">    logger.info(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Starting factorial calculation for n=</span><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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-4">    </span>
<span id="cb19-5">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Input validation with logging</span></span>
<span id="cb19-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(n, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>):</span>
<span id="cb19-7">        logger.error(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Invalid input type: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">type</span>(n)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, expected int"</span>)</span>
<span id="cb19-8">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb19-9">    </span>
<span id="cb19-10">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> n <span class="op" style="color: #5E5E5E;
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="cb19-11">        logger.error(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Negative input not allowed: </span><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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb19-13">    </span>
<span id="cb19-14">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> n <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>:</span>
<span id="cb19-15">        logger.warning(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Large input </span><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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> may cause overflow"</span>)</span>
<span id="cb19-16">    </span>
<span id="cb19-17">    logger.debug(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Input validation passed for n=</span><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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-18">    </span>
<span id="cb19-19">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calculate factorial</span></span>
<span id="cb19-20">    result <span class="op" 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="cb19-21">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">range</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, n <span class="op" 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="cb19-22">        result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*=</span> i</span>
<span id="cb19-23">        logger.debug(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Step </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">: result = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-24">    </span>
<span id="cb19-25">    logger.info(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Factorial calculation complete: </span><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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">! = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb19-26">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> result</span>
<span id="cb19-27"></span>
<span id="cb19-28"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test the function</span></span>
<span id="cb19-29">factorial_5 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> calculate_factorial(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb19-30"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"5! = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>factorial_5<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>5! = 120</code></pre>
</div>
</div>
<hr>
</section>
</section>
<section id="debugging-techniques-and-strategies" class="level1">
<h1>Debugging Techniques and Strategies</h1>
<section id="strategic-print-statement-debugging" class="level2">
<h2 class="anchored" data-anchor-id="strategic-print-statement-debugging">Strategic Print Statement Debugging</h2>
<p>While logging is preferred for production code, print statements are useful for quick debugging during development:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb21-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> find_maximum_in_list(numbers):</span>
<span id="cb21-2">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""Find maximum with debug print statements."""</span></span>
<span id="cb21-3">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"DEBUG: Starting with list = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>numbers<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-4">    </span>
<span id="cb21-5">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> numbers:</span>
<span id="cb21-6">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"DEBUG: Empty list provided"</span>)</span>
<span id="cb21-7">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span></span>
<span id="cb21-8">    </span>
<span id="cb21-9">    max_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> numbers[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>]</span>
<span id="cb21-10">    max_index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span></span>
<span id="cb21-11">    </span>
<span id="cb21-12">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> i, value <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">enumerate</span>(numbers):</span>
<span id="cb21-13">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"DEBUG: Checking index </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>i<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, value = </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> max_value:</span>
<span id="cb21-15">            max_value <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> value</span>
<span id="cb21-16">            max_index <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> i</span>
<span id="cb21-17">            <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"DEBUG: New maximum: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>max_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> at index </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>max_index<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-18">    </span>
<span id="cb21-19">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"DEBUG: Final result: max_value=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>max_value<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, index=</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>max_index<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb21-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> max_value, max_index</span>
<span id="cb21-21"></span>
<span id="cb21-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test</span></span>
<span id="cb21-23">result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> find_maximum_in_list([<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;">7</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;">9</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;">5</span>])</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>DEBUG: Starting with list = [3, 7, 2, 9, 1, 5]
DEBUG: Checking index 0, value = 3
DEBUG: Checking index 1, value = 7
DEBUG: New maximum: 7 at index 1
DEBUG: Checking index 2, value = 2
DEBUG: Checking index 3, value = 9
DEBUG: New maximum: 9 at index 3
DEBUG: Checking index 4, value = 1
DEBUG: Checking index 5, value = 5
DEBUG: Final result: max_value=9, index=3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb23-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Maximum: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result[<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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> at position </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>result[<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="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Maximum: 9 at position 3</code></pre>
</div>
</div>
</section>
<section id="using-pythons-built-in-debugger-pdb" class="level2">
<h2 class="anchored" data-anchor-id="using-pythons-built-in-debugger-pdb">Using Python’s Built-in Debugger (pdb)</h2>
<p>Python’s <code>pdb</code> module allows interactive debugging:</p>
<div class="sourceCode" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> pdb</span>
<span id="cb25-2"></span>
<span id="cb25-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> problematic_function(x, y):</span>
<span id="cb25-4">    pdb.set_trace()  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Execution will pause here</span></span>
<span id="cb25-5">    result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> y</span>
<span id="cb25-6">    final_result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> result <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span> (x <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span> y)</span>
<span id="cb25-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> final_result</span>
<span id="cb25-8"></span>
<span id="cb25-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># When you run this, you can inspect variables interactively</span></span></code></pre></div>
<p><strong>Common pdb commands:</strong></p>
<ul>
<li><code>n</code> (next line)</li>
<li><code>s</code> (step into function)</li>
<li><code>c</code> (continue execution)</li>
<li><code>p variable_name</code> (print variable value)</li>
<li><code>q</code> (quit debugger)</li>
</ul>
<hr>
</section>
</section>
<section id="your-turn-practice-exercise" class="level1">
<h1>Your Turn! Practice Exercise</h1>
<p><strong>Challenge:</strong> Create a simple bank account class that uses all the debugging techniques we’ve covered.</p>
<p><strong>Requirements:</strong> 1. Use assertions to validate inputs 2. Handle exceptions for invalid operations 3. Add logging for all transactions 4. Include debug information for troubleshooting</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> logging</span>
<span id="cb26-2"></span>
<span id="cb26-3"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> BankAccount:</span>
<span id="cb26-4">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, account_number, initial_balance<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>):</span>
<span id="cb26-5">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here</span></span>
<span id="cb26-6">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb26-7">    </span>
<span id="cb26-8">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deposit(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount):</span>
<span id="cb26-9">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here</span></span>
<span id="cb26-10">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb26-11">    </span>
<span id="cb26-12">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> withdraw(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount):</span>
<span id="cb26-13">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here</span></span>
<span id="cb26-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb26-15">    </span>
<span id="cb26-16">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_balance(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb26-17">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Your code here</span></span>
<span id="cb26-18">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">pass</span></span>
<span id="cb26-19"></span>
<span id="cb26-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test your implementation</span></span>
<span id="cb26-21">account <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BankAccount(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12345"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb26-22">account.deposit(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>)</span>
<span id="cb26-23">account.withdraw(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb26-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Final balance: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>account<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>get_balance()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Final balance: $None</code></pre>
</div>
</div>
<details>
<summary>
Click here for Solution!
</summary>
<div class="cell">
<div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb28-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> logging</span>
<span id="cb28-2"></span>
<span id="cb28-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Configure logging</span></span>
<span id="cb28-4">logging.basicConfig(level<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>logging.INFO, </span>
<span id="cb28-5">                   <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">format</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(asctime)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> - </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(levelname)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;"> - </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%(message)s</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'</span>)</span>
<span id="cb28-6">logger <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> logging.getLogger(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">__name__</span>)</span>
<span id="cb28-7"></span>
<span id="cb28-8"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">class</span> BankAccount:</span>
<span id="cb28-9">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">__init__</span>(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, account_number, initial_balance<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>):</span>
<span id="cb28-10">        <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Use assertions to validate inputs</span></span>
<span id="cb28-11">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(account_number, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">str</span>), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Account number must be a string"</span></span>
<span id="cb28-12">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">len</span>(account_number) <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Account number cannot be empty"</span></span>
<span id="cb28-13">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(initial_balance, (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Initial balance must be a number"</span></span>
<span id="cb28-14">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> initial_balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Initial balance cannot be negative"</span></span>
<span id="cb28-15">        </span>
<span id="cb28-16">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.account_number <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> account_number</span>
<span id="cb28-17">        <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> initial_balance</span>
<span id="cb28-18">        </span>
<span id="cb28-19">        logger.info(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Account </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>account_number<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> created with balance $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>initial_balance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-20">    </span>
<span id="cb28-21">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> deposit(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount):</span>
<span id="cb28-22">        logger.debug(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Deposit request: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>amount<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> to account </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>account_number<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-23">        </span>
<span id="cb28-24">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb28-25">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Validate input</span></span>
<span id="cb28-26">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(amount, (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deposit amount must be a number"</span></span>
<span id="cb28-27">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> amount <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deposit amount must be positive"</span></span>
<span id="cb28-28">            </span>
<span id="cb28-29">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Process deposit</span></span>
<span id="cb28-30">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+=</span> amount</span>
<span id="cb28-31">            logger.info(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Deposited $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>amount<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">. New balance: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>balance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-32">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb28-33">            </span>
<span id="cb28-34">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AssertionError</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb28-35">            logger.error(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Deposit failed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-36">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb28-37">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb28-38">            logger.critical(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Unexpected error during deposit: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-39">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb28-40">    </span>
<span id="cb28-41">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> withdraw(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>, amount):</span>
<span id="cb28-42">        logger.debug(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Withdrawal request: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>amount<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;"> from account </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>account_number<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-43">        </span>
<span id="cb28-44">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb28-45">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Validate input</span></span>
<span id="cb28-46">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">isinstance</span>(amount, (<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">int</span>, <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">float</span>)), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Withdrawal amount must be a number"</span></span>
<span id="cb28-47">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">assert</span> amount <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Withdrawal amount must be positive"</span></span>
<span id="cb28-48">            </span>
<span id="cb28-49">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check sufficient funds</span></span>
<span id="cb28-50">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> amount <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.balance:</span>
<span id="cb28-51">                <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">raise</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Insufficient funds. Balance: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>balance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">, Requested: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>amount<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-52">            </span>
<span id="cb28-53">            <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Process withdrawal</span></span>
<span id="cb28-54">            <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.balance <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> amount</span>
<span id="cb28-55">            logger.info(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Withdrew $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>amount<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">. New balance: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>balance<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-56">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span></span>
<span id="cb28-57">            </span>
<span id="cb28-58">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> (<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">AssertionError</span>, <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">ValueError</span>) <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb28-59">            logger.error(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Withdrawal failed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-60">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb28-61">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb28-62">            logger.critical(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Unexpected error during withdrawal: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-63">            <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span></span>
<span id="cb28-64">    </span>
<span id="cb28-65">    <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> get_balance(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>):</span>
<span id="cb28-66">        logger.debug(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Balance inquiry for account </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>account_number<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-67">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">self</span>.balance</span>
<span id="cb28-68"></span>
<span id="cb28-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Test the implementation</span></span>
<span id="cb28-70"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb28-71">    account <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> BankAccount(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"12345"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>)</span>
<span id="cb28-72">    account.deposit(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">500</span>)</span>
<span id="cb28-73">    account.withdraw(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">200</span>)</span>
<span id="cb28-74">    account.withdraw(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2000</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This should fail</span></span>
<span id="cb28-75">    <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Final balance: $</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>account<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">.</span>get_balance()<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb28-76"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb28-77">    logger.critical(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"Account creation failed: </span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>True
True
False
Final balance: $1300</code></pre>
</div>
</div>
</details>
<hr>
</section>
<section id="quick-takeaways" class="level1">
<h1>Quick Takeaways</h1>
<p>• <strong>Assertions</strong> are your first line of defense against logic errors—use them to verify assumptions during development</p>
<p>• <strong>Exception handling</strong> prevents crashes by gracefully managing runtime errors with try-except blocks</p>
<p>• <strong>Logging</strong> is superior to print statements for tracking program execution—it provides levels, timestamps, and flexible output</p>
<p>• <strong>Strategic debugging</strong> involves reading error messages carefully, using print statements judiciously, and leveraging Python’s built-in debugger</p>
<p>• <strong>Always validate inputs</strong> and handle edge cases to make your code more robust</p>
<p>• <strong>Read error messages from bottom to top</strong>—the most relevant information is usually at the end</p>
<p>• <strong>Test your code incrementally</strong> rather than writing large chunks before testing</p>
<hr>
</section>
<section id="debugging-techniques-comparison" class="level1">
<h1>Debugging Techniques Comparison</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 24%">
<col style="width: 24%">
<col style="width: 26%">
<col style="width: 24%">
</colgroup>
<thead>
<tr class="header">
<th><strong>Technique</strong></th>
<th><strong>Best Used For</strong></th>
<th><strong>When NOT to Use</strong></th>
<th><strong>Example</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>Assertions</strong></td>
<td>Internal validation, algorithm invariants</td>
<td>Production error handling</td>
<td><code>assert x &gt; 0, "Value must be positive"</code></td>
</tr>
<tr class="even">
<td><strong>Exceptions</strong></td>
<td>User input errors, file operations</td>
<td>Internal logic checks</td>
<td><code>try: ... except ValueError: ...</code></td>
</tr>
<tr class="odd">
<td><strong>Logging</strong></td>
<td>Production monitoring, detailed tracking</td>
<td>Simple one-time debugging</td>
<td><code>logging.info("Process started")</code></td>
</tr>
<tr class="even">
<td><strong>Print Statements</strong></td>
<td>Quick debugging, temporary inspection</td>
<td>Production code</td>
<td><code>print(f"DEBUG: x = {x}")</code></td>
</tr>
<tr class="odd">
<td><strong>PDB Debugger</strong></td>
<td>Complex bugs, step-by-step analysis</td>
<td>Simple issues</td>
<td><code>import pdb; pdb.set_trace()</code></td>
</tr>
</tbody>
</table>
<hr>
</section>
<section id="common-python-errors-and-solutions" class="level1">
<h1>Common Python Errors and Solutions</h1>
<table class="caption-top table">
<colgroup>
<col style="width: 30%">
<col style="width: 36%">
<col style="width: 33%">
</colgroup>
<thead>
<tr class="header">
<th><strong>Error Type</strong></th>
<th><strong>Common Cause</strong></th>
<th><strong>Prevention</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>SyntaxError</code></td>
<td>Missing colons, incorrect indentation</td>
<td>Use IDE with syntax highlighting</td>
</tr>
<tr class="even">
<td><code>NameError</code></td>
<td>Using undefined variables</td>
<td>Initialize variables before use</td>
</tr>
<tr class="odd">
<td><code>TypeError</code></td>
<td>Wrong data types in operations</td>
<td>Use type hints and validation</td>
</tr>
<tr class="even">
<td><code>ValueError</code></td>
<td>Invalid values for functions</td>
<td>Add input validation</td>
</tr>
<tr class="odd">
<td><code>IndexError</code></td>
<td>List index out of range</td>
<td>Use <code>len()</code> to check bounds</td>
</tr>
<tr class="even">
<td><code>KeyError</code></td>
<td>Dictionary key not found</td>
<td>Use <code>dict.get()</code> with defaults</td>
</tr>
<tr class="odd">
<td><code>ZeroDivisionError</code></td>
<td>Division by zero</td>
<td>Add zero checks before division</td>
</tr>
</tbody>
</table>
<hr>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>Python debugging doesn’t have to be a frustrating experience. By mastering <strong>assertions</strong>, <strong>exception handling</strong>, <strong>logging</strong>, and systematic debugging approaches, you can quickly identify and resolve issues in your code.</p>
<p>Remember these key principles: - <strong>Use assertions</strong> to catch bugs early during development - <strong>Handle exceptions</strong> to make your programs robust and user-friendly<br>
- <strong>Implement logging</strong> for better visibility into your program’s behavior - <strong>Debug systematically</strong> by reading error messages carefully and testing incrementally</p>
<p>The techniques covered in this guide will serve you well throughout your Python programming journey. As you practice and encounter more complex problems, these debugging skills will become second nature.</p>
<p><strong>Ready to level up your Python debugging skills?</strong> Start by implementing these techniques in your current projects, and don’t forget to keep that debugging journal—you’ll be amazed at how much you learn from tracking the problems you solve!</p>
<hr>
</section>
<section id="frequently-asked-questions" class="level1">
<h1>Frequently Asked Questions</h1>
<p><strong>Q: Should I use assertions in production code?</strong> A: No, assertions can be disabled with Python’s <code>-O</code> flag and should only be used during development for internal checks. Use proper exception handling for production error management.</p>
<p><strong>Q: When should I use logging instead of print statements?</strong> A: Use logging when you need different severity levels, want to output to files, need timestamps, or are working on production code. Print statements are fine for quick debugging during development.</p>
<p><strong>Q: What’s the difference between errors and exceptions in Python?</strong> A: In Python, “errors” and “exceptions” are often used interchangeably. Technically, exceptions are a type of error that can be caught and handled with try-except blocks.</p>
<p><strong>Q: How do I read Python error messages effectively?</strong> A: Start from the bottom of the traceback and work your way up. The last line contains the error type and message, while preceding lines show the call stack that led to the error.</p>
<p><strong>Q: Is it okay to use bare except clauses?</strong> A: No, avoid using <code>except:</code> without specifying exception types. This can hide unexpected errors and make debugging harder. Always catch specific exceptions when possible.</p>
<hr>
<p><em>Found this guide helpful? Share your debugging experiences in the comments below and let us know which technique you found most useful! Don’t forget to bookmark this page for future reference.</em> 📚✨</p>
<hr>
</section>
<section id="references" class="level1">
<h1>References</h1>
<ol type="1">
<li><p>Real Python Team. (2024). Python’s assert: Debug and test your code like a pro. <em>Real Python</em>. <a href="https://realpython.com/python-assert-statement/">https://realpython.com/python-assert-statement/</a></p></li>
<li><p>Real Python Team. (2024, December 1). Python exceptions: An introduction. <em>Real Python</em>. <a href="https://realpython.com/python-exceptions/">https://realpython.com/python-exceptions/</a></p></li>
<li><p>Sweigart, A. (n.d.). Chapter 11: Debugging. In <em>Automate the boring stuff with Python</em> (2nd ed.). <a href="https://automatetheboringstuff.com/2e/chapter11/">https://automatetheboringstuff.com/2e/chapter11/</a></p></li>
<li><p>W3Schools. (n.d.). Python assert keyword. <em>W3Schools</em>. <a href="https://www.w3schools.com/python/ref_keyword_assert.asp">https://www.w3schools.com/python/ref_keyword_assert.asp</a></p></li>
</ol>
<hr>
<p>Happy Coding! 🚀</p>
<section id="python-debugging" class="level2">
<h2 class="anchored" data-anchor-id="python-debugging"><img src="https://www.spsanderson.com/steveondata/posts/2025-08-06/todays_post.png" class="img-fluid" alt="Python Debugging!"></h2>
<p><em>You can connect with me at any one of the below</em>:</p>
<p><em>Telegram Channel here</em>: <a href="https://t.me/steveondata" class="uri">https://t.me/steveondata</a></p>
<p><em>LinkedIn Network here</em>: <a href="https://www.linkedin.com/in/spsanderson/" class="uri">https://www.linkedin.com/in/spsanderson/</a></p>
<p><em>Mastadon Social here</em>: <a href="https://mstdn.social/@stevensanderson">https://mstdn.social/@stevensanderson</a></p>
<p><em>RStats Network here</em>: <a href="https://rstats.me/@spsanderson">https://rstats.me/@spsanderson</a></p>
<p><em>GitHub Network here</em>: <a href="https://github.com/spsanderson" class="uri">https://github.com/spsanderson</a></p>
<p><em>Bluesky Network here</em>: <a href="https://bsky.app/profile/spsanderson.com" class="uri">https://bsky.app/profile/spsanderson.com</a></p>
<p><em>My Book: Extending Excel with Python and R</em> here: <a href="https://packt.link/oTyZJ" class="uri">https://packt.link/oTyZJ</a></p>
<p><em>You.com Referral Link</em>: <a href="https://you.com/join/EHSLDTL6" class="uri">https://you.com/join/EHSLDTL6</a></p>
<hr>
<script src="https://giscus.app/client.js" data-repo="spsanderson/steveondata" data-repo-id="R_kgDOIIxnLw" data-category="Comments" data-category-id="DIC_kwDOIIxnL84ChTk8" data-mapping="url" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="top" data-theme="dark" data-lang="en" data-loading="lazy" crossorigin="anonymous" async="">
</script>


</section>
</section>

 ]]></description>
  <category>code</category>
  <category>python</category>
  <guid>https://www.spsanderson.com/steveondata/posts/2025-08-06/</guid>
  <pubDate>Wed, 06 Aug 2025 04:00:00 GMT</pubDate>
</item>
</channel>
</rss>
