<?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>Vishal Katti&#39;s Blog</title>
<link>https://vishalkatti.com/</link>
<atom:link href="https://vishalkatti.com/index.xml" rel="self" type="application/rss+xml"/>
<description>Vishal Katti&#39;s Blog</description>
<image>
<url>https://vishalkatti.com/VK Logo.png</url>
<title>Vishal Katti&#39;s Blog</title>
<link>https://vishalkatti.com/</link>
<height>144</height>
<width>144</width>
</image>
<generator>quarto-1.5.56</generator>
<lastBuildDate>Mon, 19 Feb 2024 18:30:00 GMT</lastBuildDate>
<item>
  <title>Beyond 01/31/2024</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/date-format/</link>
  <description><![CDATA[ 





<section id="intro" class="level1">
<h1>Intro</h1>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://imgs.xkcd.com/comics/iso_8601.png" class="img-fluid figure-img"></p>
<figcaption>Source: XKCD</figcaption>
</figure>
</div>
<p>Dates are everywhere!!! They appear in documents, digital displays, our phones and conversations. In day-to-day life, outside of work, most of us are usually aware of which month and year is going on. However, at work, for communication, file-management, meeting scheduling, reporting, especially with International clients and MNCs, choosing the right date format can make a significant difference in clarity, consistency, and ease of use. Among various formats like MM/DD/YYYY, DD/MM/YYYY, and others, the <strong>YYYY-MM-DD</strong> format stands out as the best choice. While some might argue it’s just a matter of preference, there’s actually a strong case for one format reigning supreme.</p>
</section>
<section id="ambiguity-and-confusion" class="level1">
<h1>Ambiguity and Confusion</h1>
<p>Formats like DD/MM/YYYY and MM/DD/YYYY cause confusion, especially in Orgs. that operate across geographies and multi-national companies (MNC) At a smaller scale, individual gig-workers working with MS Excel files from different clients get confused if the date format is different.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://i.redd.it/5ck7dzvzp3721.png" class="img-fluid figure-img"></p>
<figcaption>Source: Reddit</figcaption>
</figure>
</div>
<p>The YYYY-MM-DD format follows a logical order: Year, &gt; Month &gt; Day, from largest to smallest unit. The logic is similar to geographical hierarchy i.e.&nbsp;Earth (Planet) &gt; Asia (Continent) &gt; India (Country) &gt; Karnataka (State) &gt; Bengaluru (City). Largest to Smallest!</p>
<p>This logical structure ensures clarity and consistency in understanding dates across different cultures and languages. For instance:</p>
<ul>
<li><p><strong>January 31, 2024</strong> is represented as <strong>2024-01-31</strong>. The sequence is intuitive and unambiguous, reducing the risk of misinterpretation.</p></li>
<li><p>In contrast, formats like MM/DD/YYYY or DD/MM/YYYY can lead to confusion, especially in international communication. For example, 02/03/2024 can mean February 3rd or March 2nd, depending on the chosen format, leading to potential errors.</p></li>
</ul>
</section>
<section id="sorting-and-organization" class="level1">
<h1>Sorting and Organization</h1>
<p>The YYYY-MM-DD format facilitates sorting dates effortlessly, both alphabetically and chronologically. Whether it’s arranging files on a computer, organizing data in spreadsheets, or storing information in databases, the YYYY-MM-DD format simplifies the process.</p>
<ul>
<li><p>Alphabetical sorting aligns with chronological order, making it easier for computers and humans to process information consistently.</p></li>
<li><p>Other formats may pose challenges in sorting, especially when combining different date formats within the same dataset. Ambiguities arise, leading to errors and confusion during analysis.</p></li>
</ul>
<p>Sorting is done based on the Alphabetical order of the characters in the filenames. Let’s evaluate each format on the basis of sorting.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true">MM-DD-YYYY</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false">DD-MM-YYYY</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false">MMM-DD-YYYY</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-4-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-4" aria-controls="tabset-1-4" aria-selected="false">DD-MMM-YYYY</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-5-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-5" aria-controls="tabset-1-5" aria-selected="false">👑 YYYY-MM-DD 👑</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<p>The MM-DD-YYYY format in filenames pulls Month and Days for different years together.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://vishalkatti.com/posts/date-format/sorting_MMDDYYYY.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<p>DD-MM-YYYY format in filenames pulls the days together for all months and years. This is worse than MM-DD-YYYY in this case!</p>
<p><img src="https://vishalkatti.com/posts/date-format/sorting_DDMMYYYY.png" class="img-fluid"></p>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<p>Month Names cause bigger problems due to Alphabetical ordering. Shows all days of April first followed by all days of August, December, February and so on...</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://vishalkatti.com/posts/date-format/sorting_MMMDDYYYY.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</div>
<div id="tabset-1-4" class="tab-pane" aria-labelledby="tabset-1-4-tab">
<p>Month Names cause bigger problems due to Alphabetical ordering. Shows same day of April first followed by same of August, December, February and so on...</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://vishalkatti.com/posts/date-format/sorting_DDMMMYYYY.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</div>
<div id="tabset-1-5" class="tab-pane" aria-labelledby="tabset-1-5-tab">
<p>YYYY-MM-DD gives the best sorting as per chronological order. No other format gives this order.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://vishalkatti.com/posts/date-format/sorting_best.png" class="img-fluid quarto-figure quarto-figure-center figure-img"></p>
</figure>
</div>
</div>
</div>
</div>
</section>
<section id="compatibility-and-standardization" class="level1">
<h1>Compatibility and Standardization</h1>
<p>YYYY-MM-DD format is widely recognized and accepted in various industries, including technology, finance, and healthcare. Its standardized nature promotes interoperability and data exchange across different systems and platforms.</p>
<p>Many programming languages like  and , databases, and software applications prefer the YYYY-MM-DD format for date handling and manipulation. This consistency streamlines development and integration processes. International standards bodies like ISO (International Organization for Standardization) endorse the YYYY-MM-DD format (ISO 8601), further solidifying its status as a global standard.</p>
</section>
<section id="but-i-only-work-in-microsofts-excel-how-does-this-matter" class="level1">
<h1>But, I only work in Microsoft’s Excel!! How does this matter?</h1>
<p>Well, you are right!</p>
<p>Spreadsheet applications like MS Excel store Dates as numbers behind the scenes and display them as per the system’s Locale. An Excel file containing Dates (formatted as proper Dates, not dates stored as Text!!! 😱 ) will change the date format accordingly because the underlying number doesn’t change. However, at one glance, it is not possible to guess if a date-like text is a proper Date or Text. Dates stored as text will not change as per system’s Locale and hence may cause errors in calculations and sorting issues. While the YYYY-MM-DD format will resolve sorting issues (due to its inherent alphabetical order) even if stored as text, calculation errors will still occur if dates are stored as text.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://vishalkatti.com/posts/date-format/excel-dates.png" class="img-fluid figure-img"></p>
<figcaption>Excel stores dates as numbers!! A date 20th Feb 2024 represented by the number 45342 means it is 45342 days since 1st Jan 1900.</figcaption>
</figure>
</div>
<p>Exploring date-related horrors in MS Excel is a topic for a whole other post, which will come soon!</p>
</section>
<section id="incidents" class="level1">
<h1>Incidents</h1>
<p>While there might not be a single major incident directly caused by unambiguous date formats, the lack of standardized date formats has contributed to numerous misunderstandings, errors, and inefficiencies in various contexts. Here are a few examples:</p>
<ol type="1">
<li><p><strong>Y2K Bug</strong>: One of the most well-known incidents related to date formatting issues is the Y2K (Year 2000) bug. Many computer systems used a two-digit representation for the year, assuming the first two digits were “19” by default (e.g., “98” for 1998). As the year 2000 approached, there was concern that these systems would interpret “00” as 1900 instead of 2000, potentially causing malfunctions or system failures. While not directly caused by unambiguous date formats, the Y2K issue highlighted the importance of standardized and unambiguous date representations in computer systems.</p></li>
<li><p><strong>Medical Errors</strong>: In healthcare settings, incorrect interpretation of dates can lead to medical errors, missed appointments, and treatment delays. For example, confusion between MM/DD/YYYY and DD/MM/YYYY formats could result in misinterpreting appointment dates, prescription refills, or test results, potentially compromising patient care and safety.</p></li>
<li><p><strong>Financial Transactions</strong>: In the financial sector, accurate date representations are crucial for transactions, accounting, and regulatory compliance. Ambiguous date formats can lead to errors in financial reporting, billing, and tax calculations, causing financial discrepancies and regulatory violations.</p></li>
<li><p><strong>Legal Documents</strong>: Ambiguous date formats in legal documents such as contracts, agreements, and court filings can lead to disputes, legal challenges, and contract breaches. Inconsistencies in date interpretation may affect the validity and enforceability of legal agreements, resulting in costly litigation and legal proceedings.</p></li>
</ol>
</section>
<section id="closing" class="level1">
<h1>Closing</h1>
<p>While these incidents may not be directly attributed to unambiguous date formats, they underscore the importance of standardized date representations in ensuring accuracy, clarity, and interoperability across different systems, industries, and domains. Standardized date formats, such as those defined in ISO 8601, help mitigate the risk of errors, misunderstandings, and disruptions caused by inconsistent date representations, promoting efficiency, reliability, and trust in data exchange and communication.</p>
<p>While it is highly doubtful that this article says anything new compared to already existing literature, the switch to using YYYY-MM-DD format consistently is a small step towards a more inclusive and unambiguous system of time-keeping!</p>
<p>Guess, I woke up and chose violence today!</p>
<p><img src="https://vishalkatti.com/posts/date-format/choose-choice.gif" class="img-fluid"></p>
</section>
<section id="references" class="level1">
<h1>References</h1>
<p><a href="https://en.wikipedia.org/wiki/ISO_8601">Wikipedia - ISO 8601</a></p>
<p><a href="https://en.wikipedia.org/wiki/Year_2000_problem">Wikipedia - Y2K Bug</a></p>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2024,
  author = {Katti, Vishal},
  title = {Beyond 01/31/2024},
  date = {2024-02-20},
  url = {https://vishalkatti.com/posts/date-format/},
  langid = {en},
  abstract = {This post talks about the superiority of ISO 8601 Date
    format and why it matters in our day-to-day life.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2024" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2024. <span>“Beyond 01/31/2024.”</span> February 20,
2024. <a href="https://vishalkatti.com/posts/date-format/">https://vishalkatti.com/posts/date-format/</a>.
</div></div></section></div> ]]></description>
  <category>Date</category>
  <category>ISO8601</category>
  <category>R</category>
  <category>Python</category>
  <category>Excel</category>
  <guid>https://vishalkatti.com/posts/date-format/</guid>
  <pubDate>Mon, 19 Feb 2024 18:30:00 GMT</pubDate>
  <media:content url="https://vishalkatti.com/posts/date-format/choose-choice.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Python - List Comprehension</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/python-list-comprehension/</link>
  <description><![CDATA[ 





<section id="intro" class="level1">
<h1>Intro</h1>
<p>In the world of Python, lists are the most versatile containers for managing data. While <code>for</code> loops offer a traditional approach to creating and manipulating lists, Python offers a more elegant and efficient alternative: list comprehensions. Let’s dive into this concise syntax and explore its advantages over traditional for loops.</p>
</section>
<section id="basic-syntax" class="level1">
<h1>Basic Syntax</h1>
<p>List comprehensions pack a powerful punch in a compact syntax. They allow you to create lists in a single line, combining iteration and expression evaluation within square brackets. Here’s the most basic structure:</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb1-1">new_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [expression <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> iterable]</span></code></pre></div>
<p>where</p>
<ul>
<li><code>expression</code> is the value or logic applied to each item, which will create the items of the new list</li>
<li><code>item</code> is the variable that represents each element of the iterable</li>
<li><code>iterable</code> is the variable over which we iterate or ‘loop’. This could be a list, tuple, dictionary, string or anything which can be considered an <code>iterable</code> in Python.</li>
</ul>
<p>The equivalent <code>for</code> loop for the above operation is as follows:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb2-1">new_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb2-2"><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> iterable:</span>
<span id="cb2-3">    new_list.append(expression)</span></code></pre></div>
<p>Let’s understand this with some working code. Suppose I have a list of numbers and I want a list that contains the same numbers multipled by 2 i.e.&nbsp;doubled.</p>
<div class="cell" data-execution_count="1">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb3-1">my_nums <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;">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="cb3-2">doubled_nums <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [num<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="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> my_nums]</span>
<span id="cb3-3"></span>
<span id="cb3-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same operation using `for` loop</span></span>
<span id="cb3-5">doubled_nums_for <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb3-6"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> my_nums:</span>
<span id="cb3-7">  doubled_nums_for.append(num<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="cb3-8"></span>
<span id="cb3-9"></span>
<span id="cb3-10"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(doubled_nums)</span>
<span id="cb3-11"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(doubled_nums_for) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># identical to `doubled_nums`</span></span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10]</code></pre>
</div>
</div>
<p>In the above Python code, <code>my_nums</code> is the iterable, the <code>num</code> variable in 2nd line represents each item in <code>my_nums</code> and <code>num*2</code> is the expression or logic that we apply to each <code>num</code>.</p>
<p>let’s take one more example with a <code>dictionary</code>.</p>
<div class="cell" data-execution_count="2">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb5-1">my_dict <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;">'Actor'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Tom'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Director'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Tony'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Writer'</span>:<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Jim'</span>}  </span>
<span id="cb5-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Bonus for guessing the movie!</span></span>
<span id="cb5-3"></span>
<span id="cb5-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># new_list = [expression for item in iterable]</span></span>
<span id="cb5-5">roles <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [role <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> role <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> my_dict.keys()]</span>
<span id="cb5-6">people <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [person <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> person <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> my_dict.values()]</span>
<span id="cb5-7"></span>
<span id="cb5-8"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(roles)</span>
<span id="cb5-9"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(people)</span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>['Actor', 'Director', 'Writer']
['Tom', 'Tony', 'Jim']</code></pre>
</div>
</div>
<p>As you can see, the <code>item</code> variable can be named anything as this variable is active only within the scope of the list comprehension.</p>
</section>
<section id="advanced-syntax-filtering" class="level1">
<h1>Advanced Syntax: Filtering</h1>
<p>We use the following syntax when we want to create a new list with items that satisfy some condition.</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb7-1">filtered_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [expression <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> iterable <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> condition]</span></code></pre></div>
<p>where</p>
<ul>
<li><code>condition</code> is any logical expression that return <code>True</code> or <code>False</code></li>
</ul>
<p>Let’s see an example. Suppose I have a list of sentences and I want to filtered list which has the word ‘whisper’ in them.</p>
<div class="cell" data-execution_count="3">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb8-1">sentence_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [</span>
<span id="cb8-2">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Sunrise paints the clouds in fiery hues, a silent alarm"</span>,</span>
<span id="cb8-3">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Raindrops pitter-patter on cobblestones, a playful melo"</span>,</span>
<span id="cb8-4">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ocean waves whisper secrets to the sandy shore, tales o"</span>,</span>
<span id="cb8-5">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Owl's amber eyes pierce the moonlit forest, a silent gu"</span>,</span>
<span id="cb8-6">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Butterfly wings, stained glass windows fluttering throu"</span>,</span>
<span id="cb8-7">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Laughter spills from a cozy cafe window, a warm invitat"</span>,</span>
<span id="cb8-8">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Cracked pavement whispers forgotten stories, echoes of "</span>,</span>
<span id="cb8-9">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Starry sky, a canvas splashed with diamonds, whispers o"</span>,</span>
<span id="cb8-10">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Spice-laden wind dances through the market, teasing the"</span>,</span>
<span id="cb8-11">   <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Tiny snail embarks on a grand journey, a blade of grass"</span></span>
<span id="cb8-12">]</span>
<span id="cb8-13"></span>
<span id="cb8-14"></span>
<span id="cb8-15">filtered_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [sen <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sen <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sentence_list <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'whisper'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sen]</span>
<span id="cb8-16"></span>
<span id="cb8-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same operation using `for` loop</span></span>
<span id="cb8-18">filtered_list_for <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb8-19"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sen <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sentence_list:</span>
<span id="cb8-20">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'whisper'</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sen:</span>
<span id="cb8-21">    filtered_list_for.append(sen)</span>
<span id="cb8-22"></span>
<span id="cb8-23"></span>
<span id="cb8-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(filtered_list)</span>
<span id="cb8-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(filtered_list_for) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># identical to `filtered_list`</span></span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>['Ocean waves whisper secrets to the sandy shore, tales o', 'Cracked pavement whispers forgotten stories, echoes of ', 'Starry sky, a canvas splashed with diamonds, whispers o']
['Ocean waves whisper secrets to the sandy shore, tales o', 'Cracked pavement whispers forgotten stories, echoes of ', 'Starry sky, a canvas splashed with diamonds, whispers o']</code></pre>
</div>
</div>
<p>In the above code, the <code>condition</code> is <code>'whisper' in sen</code> which returns True or False for every <code>sen</code> sentence.</p>
<p>Let’s look at a more useful example. Here we create a JSON-formatted string using List Comprehension</p>
<div class="cell" data-execution_count="4">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines 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> pandas <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> pd</span>
<span id="cb10-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> json</span>
<span id="cb10-3"></span>
<span id="cb10-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sample DataFrame</span></span>
<span id="cb10-5">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;">'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="cb10-6">        <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'age'</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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>], </span>
<span id="cb10-7">        <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;">'New York'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'London'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Paris'</span>]</span>
<span id="cb10-8">        }</span>
<span id="cb10-9">df <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> pd.DataFrame(data)</span>
<span id="cb10-10"></span>
<span id="cb10-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert DataFrame to JSON using list comprehension</span></span>
<span id="cb10-12">json_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [row.to_json() <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> index, row <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> df.iterrows()]</span>
<span id="cb10-13"></span>
<span id="cb10-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Convert list to JSON and print</span></span>
<span id="cb10-15"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> json_string <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> json_list:</span>
<span id="cb10-16">  <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(json.dumps(json.loads(json_string), 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>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>{
    "name": "Alice",
    "age": 25,
    "city": "New York"
}
{
    "name": "Bob",
    "age": 30,
    "city": "London"
}
{
    "name": "Charlie",
    "age": 20,
    "city": "Paris"
}</code></pre>
</div>
</div>
<p>Here’s what is happening in the code above.</p>
<ol type="1">
<li><p>Import Libraries:<br>
<code>import pandas as pd</code>: Imports the pandas library for working with DataFrames.<br>
<code>import json</code>: Imports the json library for working with JSON data.</p></li>
<li><p>Create DataFrame:<br>
<code>data = {...}</code>: Creates a dictionary containing data for three columns: ‘name’, ‘age’, and ‘city’.<br>
<code>df = pd.DataFrame(data)</code>: Creates a DataFrame <code>df</code> from the dictionary <code>data.</code></p></li>
<li><p>Convert DataFrame to JSON List:<br>
<code>json_list = [row.to_json() for index, row in df.iterrows()]</code>: This line uses list comprehension to convert each row of the DataFrame into a JSON string and stores them in a list called <code>json_list.</code><br>
<code>iterrows()</code> iterates over the DataFrame, yielding index and row pairs.<br>
<code>row.to_json()</code> converts each row into a JSON string.</p></li>
<li><p>Print Pretty-Printed JSON:<br>
<code>for json_string in json_list:</code>: This loop iterates over each JSON string in the <code>json_list.</code><br>
<code>print(json.dumps(json.loads(json_string), indent=4))</code>: This line prints the JSON string with proper indentation:<br>
<code>json.loads(json_string)</code> parses the JSON string into a Python dictionary.<br>
<code>json.dumps()</code> re-serializes the dictionary back into a JSON string, applying indentation for readability.</p></li>
</ol>
</section>
<section id="advanced-syntax-if-else" class="level1">
<h1>Advanced Syntax: If-Else</h1>
<p>The If-Else syntax allows us to take one action if the item satisfies a condition and another action if it does not. The syntax is as follows:</p>
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb12-1">new_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [true_expr <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> condition <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> false_expr <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> iterable] </span></code></pre></div>
<p>where</p>
<ul>
<li><code>true_expr</code> is the expression which is evaluated when the <code>item</code> satisfies the <code>condition</code></li>
<li><code>false_expr</code> is the expression which is evaluated when the <code>item</code> does not satisfy the <code>condition</code></li>
</ul>
<p>Let’s look at an example of this If-Else syntax. Suppose I have list of numbers with missing values. I want replace the missing values with the average value of the numbers.</p>
<div class="cell" data-execution_count="5">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb13-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> statistics</span>
<span id="cb13-2"></span>
<span id="cb13-3">num_list <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 class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">40</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</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;">10</span>]</span>
<span id="cb13-4"></span>
<span id="cb13-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Filtering Syntax: Calculate mean with only the numbers which are not None</span></span>
<span id="cb13-6">mean <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> statistics.mean(num <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> num_list <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> num <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="cb13-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"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>mean<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</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="cb13-8"></span>
<span id="cb13-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If-Else Syntax</span></span>
<span id="cb13-10">clean_list <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [num <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> num <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 class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> mean <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> num_list]</span>
<span id="cb13-11"></span>
<span id="cb13-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This can also be written as</span></span>
<span id="cb13-13">clean_list2 <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [mean <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> num <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> num_list]</span>
<span id="cb13-14"></span>
<span id="cb13-15"></span>
<span id="cb13-16"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same operation using `for` loop</span></span>
<span id="cb13-17">clean_list_for <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb13-18"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> num_list:</span>
<span id="cb13-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> num <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">is</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">None</span>:</span>
<span id="cb13-20">    clean_list_for.append(mean)</span>
<span id="cb13-21">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span>:</span>
<span id="cb13-22">    clean_list_for.append(num)</span>
<span id="cb13-23"></span>
<span id="cb13-24"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(clean_list)</span>
<span id="cb13-25"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(clean_list2)</span>
<span id="cb13-26"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(clean_list_for) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Identical to `clean_list` and `clean_list2`</span></span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>mean=20
[10, 20, 20, 40, 20, 20, 10]
[10, 20, 20, 40, 20, 20, 10]
[10, 20, 20, 40, 20, 20, 10]</code></pre>
</div>
</div>
</section>
<section id="real-world-usage" class="level1">
<h1>Real-world Usage</h1>
<p>I have personally encountered various scenarios in my data journey where I have come across List of Lists! List comprehension is a great way to quickly flatten list of lists in one line of code.</p>
<div class="cell" data-execution_count="6">
<details open="" class="code-fold">
<summary>Code</summary>
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode numberSource python number-lines code-with-copy"><code class="sourceCode python"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a list of lists containing strings</span></span>
<span id="cb15-2">words <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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"world"</span>], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"how"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"are"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"you"</span>], [<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"today"</span>]]</span>
<span id="cb15-3"></span>
<span id="cb15-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Nested Syntax</span></span>
<span id="cb15-5">flattened_words <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> [word <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sublist <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> words <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> word <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sublist]</span>
<span id="cb15-6"></span>
<span id="cb15-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Same Operation using `for` loop</span></span>
<span id="cb15-8">flattened_words_for <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> []</span>
<span id="cb15-9"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> sublist <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> words:</span>
<span id="cb15-10">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> word <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> sublist:</span>
<span id="cb15-11">    flattened_words_for.append(word)</span>
<span id="cb15-12"></span>
<span id="cb15-13"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(flattened_words)</span>
<span id="cb15-14"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(flattened_words_for) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Identical to `flattened_words`</span></span></code></pre></div>
</details>
<div class="cell-output cell-output-stdout">
<pre><code>['hello', 'world', 'how', 'are', 'you', 'today']
['hello', 'world', 'how', 'are', 'you', 'today']</code></pre>
</div>
</div>
</section>
<section id="so-whats-best" class="level1">
<h1>So what’s best?</h1>
<p>List comprehensions are ideal when:</p>
<ul>
<li>Creating a new list based on an existing iterable.</li>
<li>Applying simple transformations or filtering to elements.</li>
<li>Prioritizing concise and readable code.</li>
</ul>
<p>For loops are preferable when:</p>
<ul>
<li>Performing complex operations within the loop.</li>
<li>Needing more control over the iteration process.</li>
<li>Requiring side effects beyond list creation (e.g., printing, modifying variables).</li>
</ul>
</section>
<section id="conclusion" class="level1">
<h1>Conclusion</h1>
<p>While list comprehensions offer a concise approach to list creation, for loops remain essential for broader iteration tasks in Python. For new developers, for loops are easier to understand and make far more sense than list comprehensions. They provide greater flexibility and control, allowing for complex operations, multiple statements within each iteration, and handling side effects (like printing, logging) that go beyond mere list creation. However, when the goal is straightforward list generation with simple transformations or filtering, list comprehensions often deliver a more elegant and efficient solution.</p>
</section>
<section id="references" class="level1">
<h1>References</h1>


</section>

<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2024,
  author = {Katti, Vishal},
  title = {Python - {List} {Comprehension}},
  date = {2024-01-06},
  url = {https://vishalkatti.com/posts/python-list-comprehension/},
  langid = {en},
  abstract = {This post demonstrates Python’s List Comprehension
    compared with the `for` loop and its usage.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2024" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2024. <span>“Python - List Comprehension.”</span> January
6, 2024. <a href="https://vishalkatti.com/posts/python-list-comprehension/">https://vishalkatti.com/posts/python-list-comprehension/</a>.
</div></div></section></div> ]]></description>
  <category>Python</category>
  <category>List</category>
  <guid>https://vishalkatti.com/posts/python-list-comprehension/</guid>
  <pubDate>Fri, 05 Jan 2024 18:30:00 GMT</pubDate>
  <media:content url="https://i.imgur.com/JIedILu.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Pivoting your tables with Tidyr: Part II</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/tidyr-pivot-wider/</link>
  <description><![CDATA[ <section id="intro" class="level1"><h1>Intro</h1>
<p>We discussed the advantages of using the long format during data analysis, most users feel that the wide format is more readable by human. This is why most reports tend to have the data arranged in the wide format.</p>
<p>The wide format has at least one column which acts as a primary key i.e.&nbsp;it is unique and each value appears only once. It can also have multiple column whose unique combination acts as a primary key i.e.&nbsp;each combination appears only once.</p>
<p>Read more about wide vs.&nbsp;long formats <a href="https://www.statology.org/long-vs-wide-data/">here</a>.</p>
<p>While the long format is preferred and is desirable for data and plotting operations using R, Python or other data processing programming languages, the wide format is more human-readable. The {tidyr} R package has functions that allow you to transform your tabular data between the two formats.</p>
<p>In this post, we will see how to convert a long dataframe to wide format using the <em>pivot_wider()</em> function from {tidyr} package.</p>
</section><section id="the-long-one" class="level1"><h1>The long one</h1>
<p>Consider the following data table. It has been created from the famous Gapminder dataset. This table shows the average life expectancy in each continent for 2 years. While some of you may say that Gapminder data contains records for a lot more number of years, here we consider just the latest 2 years for ease of explanation and visual purposes. We have added an extra <code>id</code> column for teaching purpose.</p>
<div style="text-align:center;">
<div id="fig-long" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-long-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/pV7FveQ.png" title="The Long One" class="img-fluid figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-long-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Continent-wise Average Life-expectancy over last 2 years, in Long format
</figcaption></figure>
</div>
</div>
<p><code>my_data</code> is in the long format as we have continent names and year in their own column and average life expectancy values for each unique combination of year and continent. If we want to compare life expectancy across years for each continent, we need to have the life expectancy values for each continent side-by-side for easier viewing i.e.&nbsp;we need to convert to the wide format. To convert this tibble to the wide format, we need to push the <code>year</code> values into the headers and the <code>average_life_expectancy</code> values under the corresponding year column.</p>
</section><section id="the-wide-one" class="level1"><h1>The wide one</h1>
<p>The wide format of this table would ideally have only <code>continent</code> and columns having each unique value in the <code>year</code> column as a header. In this case, the wide one would look something like the table below.</p>
<div style="text-align:center;">
<div id="fig-wide" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-wide-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/KUS6maA.png" title="The Wide One" class="img-fluid figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-wide-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Same as Figure&nbsp;1 but in Wide format
</figcaption></figure>
</div>
</div>
<p>The wide format has unique values of the column that are not pushed into headers. In this case, the <code>continent</code> column becomes unique for each row.</p>
<p>Let’s recreate the above transformation in R. First, we create the <code>my_data</code> table.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1">my_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-2">  <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;">10</span>,</span>
<span id="cb1-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year =</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;">2002</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2002</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2002</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2002</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2002</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2007</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2007</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2007</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2007</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2007</span>L),</span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">continent =</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;">"Africa"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Americas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Asia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Europe"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oceania"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Africa"</span>, </span>
<span id="cb1-5">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Americas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Asia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Europe"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oceania"</span>),</span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">average_life_expectancy =</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;">53.33</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">72.42</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">69.23</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">76.7</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">79.74</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">54.81</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">73.61</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">70.73</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">77.65</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">80.72</span>)</span>
<span id="cb1-7">)</span>
<span id="cb1-8"></span>
<span id="cb1-9">knitr<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;">kable</span>(my_data)</span></code></pre></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">id</th>
<th style="text-align: right;">year</th>
<th style="text-align: left;">continent</th>
<th style="text-align: right;">average_life_expectancy</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">53.33</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">72.42</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">69.23</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">76.70</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">79.74</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">54.81</td>
</tr>
<tr class="odd">
<td style="text-align: right;">7</td>
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">73.61</td>
</tr>
<tr class="even">
<td style="text-align: right;">8</td>
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">70.73</td>
</tr>
<tr class="odd">
<td style="text-align: right;">9</td>
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">77.65</td>
</tr>
<tr class="even">
<td style="text-align: right;">10</td>
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">80.72</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>To convert this table into wide format, we use the <em>pivot_wider()</em> function from {tidyr} R package. Let us see how to use this function.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Use <code>formals</code> to view all the formal arguments of a function and their default values. <code>formals</code> returns a named list.</p>
</div>
</div>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines 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>(tidyr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quietly =</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;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formals</span>(pivot_wider)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>$data


$id_cols
NULL

$id_expand
[1] FALSE

$names_from
name

$names_prefix
[1] ""

$names_sep
[1] "_"

$names_glue
NULL

$names_sort
[1] FALSE

$names_vary
[1] "fastest"

$names_expand
[1] FALSE

$names_repair
[1] "check_unique"

$values_from
value

$values_fill
NULL

$values_fn
NULL

$unused_fn
NULL

$...</code></pre>
</div>
</div>
<p>The result of <code>formals(pivot_wider)</code> tells us that the minimum information needed to use this function is to provide values to the <code>data</code>,<code>names_from</code> and <code>values_from</code> arguments as all other arguments have default values and hence, are optional.</p>
<p>Using only the minimum arguments with <code>pivot_wider()</code>, we get a wide formatted tibble but with missing data!</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb4-1">wide_minimal <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;">pivot_wider</span>(</span>
<span id="cb4-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data        =</span> my_data,</span>
<span id="cb4-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from  =</span> year,</span>
<span id="cb4-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> average_life_expectancy</span>
<span id="cb4-5">                        )</span>
<span id="cb4-6"></span>
<span id="cb4-7">knitr<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;">kable</span>(wide_minimal)</span></code></pre></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">id</th>
<th style="text-align: left;">continent</th>
<th style="text-align: right;">2002</th>
<th style="text-align: right;">2007</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">53.33</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">72.42</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">69.23</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">76.70</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">79.74</td>
<td style="text-align: right;">NA</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">54.81</td>
</tr>
<tr class="odd">
<td style="text-align: right;">7</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">73.61</td>
</tr>
<tr class="even">
<td style="text-align: right;">8</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">70.73</td>
</tr>
<tr class="odd">
<td style="text-align: right;">9</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">77.65</td>
</tr>
<tr class="even">
<td style="text-align: right;">10</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">80.72</td>
</tr>
</tbody>
</table>
</div>
</div>
<p><strong>So why did <code>NA</code>s appear in the result?</strong></p>
<p><code>pivot_wider()</code> creates unique combinations of all columns not included in <code>names_from</code> or <code>values_from</code> argument. Therefore, if your dataframe/tibble had a primary key prior to the transformation, the primary key of your transformed “wide” dataframe is your old primary key + unique combinations of all columns not included in <code>names_from</code> or <code>values_from</code> argument. We do have <code>id</code> column as a primary key in the original tibble. This gives an unusable output with <code>NA</code>s for each combination.</p>
<p>To specify which column/s to be made unique, pass their name to the <code>id_cols</code> argument. Here we pass the <code>continent</code> column to the <code>id_cols</code> argument.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb5-1">my_data_longer <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;">pivot_wider</span>(</span>
<span id="cb5-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data        =</span> my_data,</span>
<span id="cb5-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id_cols     =</span> continent, </span>
<span id="cb5-4">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from  =</span> year,</span>
<span id="cb5-5">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> average_life_expectancy</span>
<span id="cb5-6">                        )</span>
<span id="cb5-7"></span>
<span id="cb5-8">knitr<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;">kable</span>(my_data_longer)</span></code></pre></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: left;">continent</th>
<th style="text-align: right;">2002</th>
<th style="text-align: right;">2007</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">53.33</td>
<td style="text-align: right;">54.81</td>
</tr>
<tr class="even">
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">72.42</td>
<td style="text-align: right;">73.61</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">69.23</td>
<td style="text-align: right;">70.73</td>
</tr>
<tr class="even">
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">76.70</td>
<td style="text-align: right;">77.65</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">79.74</td>
<td style="text-align: right;">80.72</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>If you are a visual person like me and wish to see this transformation with explanations, check out this GIF I made using good ol’ PowerPoint.</p>
<div style="text-align:center;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://i.imgur.com/A9SfEdJ.gif" class="img-fluid figure-img"></p>
<figcaption>{tidyr} pivot_wider() explained</figcaption></figure>
</div>
</div>
</section><section id="conclusion" class="level1"><h1>Conclusion</h1>
<p><code>pivot_wider()</code> is the successor for the great <code>spread()</code> function and has many advantages over the latter. This function has many other arguments that allow some truly great transformations. Mastering this function (and its long counterpart) is a great skill upgrade while massaging your data to make it “tidy”.</p>
<p>Happy Spreading!</p>
</section><section id="references" class="level1"><h1>References</h1>
<ul>
<li><a href="https://www.statology.org/long-vs-wide-data/">Long vs.&nbsp;Wide Data: What’s the Difference?</a></li>
<li>Hadley Wickham and Maximilian Girlich (2022). tidyr: Tidy Messy Data. R package version 1.2.0. <a href="https://CRAN.R-project.org/package=tidyr" class="uri">https://CRAN.R-project.org/package=tidyr</a>
</li>
<li>Yihui Xie (2022). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.39.</li>
</ul>


</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2022,
  author = {Katti, Vishal},
  title = {Pivoting Your Tables with {Tidyr:} {Part} {II}},
  date = {2022-08-29},
  url = {https://vishalkatti.com/posts/tidyr-pivot-wider/},
  langid = {en},
  abstract = {This post demonstrates how to use `pivot\_wider()` to
    convert your long data to wide data. This is part 2 of the Pivoting
    your tables with Tidyr series.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2022" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2022. <span>“Pivoting Your Tables with Tidyr: Part
II.”</span> August 29, 2022. <a href="https://vishalkatti.com/posts/tidyr-pivot-wider/">https://vishalkatti.com/posts/tidyr-pivot-wider/</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>functions</category>
  <category>tidyr</category>
  <category>pivot</category>
  <guid>https://vishalkatti.com/posts/tidyr-pivot-wider/</guid>
  <pubDate>Sun, 28 Aug 2022 18:30:00 GMT</pubDate>
  <media:content url="https://i.imgur.com/A9SfEdJ.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Pivoting your tables with Tidyr: Part I</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/tidyr-pivot-longer/</link>
  <description><![CDATA[ <section id="intro" class="level1"><h1>Intro</h1>
<p>One of the primary data manipulation operations is pivoting your tabular data from “wide” format to “long” format and vice-versa.</p>
<p>The idea is to make your tabular data “tidy” i.e.</p>
<ul>
<li>Every column is a variable.</li>
<li>Every row is an observation.</li>
<li>Every cell is a single value.</li>
</ul>
<p>In other words, every column contains just one type of information, every row in the table is a snapshot or a version of the information your table captures and every cell contains just one piece of information.<sup>1</sup></p>
<p>While the wide format is more human-readable, the long format is preferred and is desirable for data and plotting operations using R, Python or other data processing programming languages. The {tidyr} R package has functions that allow you to transform your tabular data between the two formats.</p>
<p>In this post, we will see how to convert a wide dataframe to long format using the <em>pivot_longer()</em> function from {tidyr} package.</p>
</section><section id="the-wide-one" class="level1"><h1>The wide one</h1>
<p>Consider the following data table. It has been created from the famous Gapminder dataset. This table shows the average life expectancy in each continent for 2 years. While some of you may say that Gapminder data contains records for a lot more number of years, here we consider just the latest 2 years for ease of explanation and visual purposes.</p>
<div style="text-align:center;">
<div id="fig-wide" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-wide-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/sMJDveZ.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-wide-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Continent-wise Average Life Expectancy over last 2 years
</figcaption></figure>
</div>
</div>
<div style="text-align:center;">
<div id="fig-wide2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-wide2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/kbMDurH.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-wide2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: The wide one
</figcaption></figure>
</div>
</div>
<p><code>my_data</code> is in the wide format as we have continent names in column headers and average life expectancy values in each of those columns. To convert this tibble to the long format, we need to pull together the continent names in one column and their corresponding values into another column. See Figure&nbsp;2</p>
</section><section id="the-long-one" class="level1"><h1>The long one</h1>
<p>The long format of this table would ideally have only <code>year</code>, <code>continent</code> and <code>average_life_expectancy</code> columns and look something like the table below.</p>
<div style="text-align:center;">
<div id="fig-long" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-long-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/63PMzJ3.png" class="img-fluid figure-img" style="width:60.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-long-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: The long one
</figcaption></figure>
</div>
</div>
<p>The long format has repeated values of the column that are not gathered/collected. In this case, the <code>year</code> column gets its values repeated for each row.</p>
<p>Let’s recreate the above transformation in R. First, we create the <code>my_data</code> table.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1">my_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-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">year     =</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;">2002</span>L, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2007</span>L), </span>
<span id="cb1-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Africa   =</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;">53.33</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">54.81</span>), </span>
<span id="cb1-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Americas =</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;">72.42</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">73.61</span>), </span>
<span id="cb1-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Asia     =</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;">69.23</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">70.73</span>), </span>
<span id="cb1-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Europe   =</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;">76.70</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">77.65</span>), </span>
<span id="cb1-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Oceania  =</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;">79.74</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">80.72</span>)</span>
<span id="cb1-8">)</span>
<span id="cb1-9"></span>
<span id="cb1-10">knitr<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;">kable</span>(my_data)</span></code></pre></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">year</th>
<th style="text-align: right;">Africa</th>
<th style="text-align: right;">Americas</th>
<th style="text-align: right;">Asia</th>
<th style="text-align: right;">Europe</th>
<th style="text-align: right;">Oceania</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2002</td>
<td style="text-align: right;">53.33</td>
<td style="text-align: right;">72.42</td>
<td style="text-align: right;">69.23</td>
<td style="text-align: right;">76.70</td>
<td style="text-align: right;">79.74</td>
</tr>
<tr class="even">
<td style="text-align: right;">2007</td>
<td style="text-align: right;">54.81</td>
<td style="text-align: right;">73.61</td>
<td style="text-align: right;">70.73</td>
<td style="text-align: right;">77.65</td>
<td style="text-align: right;">80.72</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>To convert this table into long format, we use the <em>pivot_longer()</em> function from {tidyr} R package. Let us see how to use this function.</p>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Use `formals` to view all the formal arguments of a function and their default values. `formals` returns a named list.</p>
</div>
</div>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines 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>(tidyr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quietly =</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;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb2-2"></span>
<span id="cb2-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">formals</span>(pivot_longer)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>$data


$cols


$names_to
[1] "name"

$names_prefix
NULL

$names_sep
NULL

$names_pattern
NULL

$names_ptypes
NULL

$names_transform
NULL

$names_repair
[1] "check_unique"

$values_to
[1] "value"

$values_drop_na
[1] FALSE

$values_ptypes
NULL

$values_transform
NULL

$...</code></pre>
</div>
</div>
<p>The result of <code>formals(pivot_longer)</code> tells us that the minimum information needed to use this function is to provide values to the <code>data</code> and <code>cols</code> arguments as all other arguments have default values and hence, are optional.</p>
<p>Using only the minimum arguments with <code>pivot_longer()</code>, we get a long formatted tibble with the columns <code>year</code>, <code>name</code> and <code>value</code>.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb4-1">long_minimal <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;">pivot_longer</span>(</span>
<span id="cb4-2">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data      =</span> my_data,</span>
<span id="cb4-3">                        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols      =</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;">"Africa"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Americas"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Asia"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Europe"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oceania"</span>)</span>
<span id="cb4-4">                        )</span>
<span id="cb4-5"></span>
<span id="cb4-6">knitr<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;">kable</span>(long_minimal)</span></code></pre></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">year</th>
<th style="text-align: left;">name</th>
<th style="text-align: right;">value</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">53.33</td>
</tr>
<tr class="even">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">72.42</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">69.23</td>
</tr>
<tr class="even">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">76.70</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">79.74</td>
</tr>
<tr class="even">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">54.81</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">73.61</td>
</tr>
<tr class="even">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">70.73</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">77.65</td>
</tr>
<tr class="even">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">80.72</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Notice that the continent names and their corresponding average life expectancy values appear in columns named <code>name</code> and <code>value</code>. These are the default column names. We can change these column names by providing our own names to the arguments <code>names_to</code> and <code>values_to</code>.</p>
<p>Since the <code>year</code> column is the only one that remains as is, we can rewrite the above <code>pivot_longer</code> statement as below</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb5-1">my_data_longer <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;">pivot_longer</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data      =</span> my_data,</span>
<span id="cb5-2">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols      =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>year,</span>
<span id="cb5-3">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to  =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"continent"</span>,</span>
<span id="cb5-4">                               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"average_life_expectancy"</span>)</span>
<span id="cb5-5"></span>
<span id="cb5-6">knitr<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;">kable</span>(my_data_longer)</span></code></pre></div>
</details><div class="cell-output-display">
<table class="caption-top table table-sm table-striped small">
<thead><tr class="header">
<th style="text-align: right;">year</th>
<th style="text-align: left;">continent</th>
<th style="text-align: right;">average_life_expectancy</th>
</tr></thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">53.33</td>
</tr>
<tr class="even">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">72.42</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">69.23</td>
</tr>
<tr class="even">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">76.70</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2002</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">79.74</td>
</tr>
<tr class="even">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Africa</td>
<td style="text-align: right;">54.81</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Americas</td>
<td style="text-align: right;">73.61</td>
</tr>
<tr class="even">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Asia</td>
<td style="text-align: right;">70.73</td>
</tr>
<tr class="odd">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Europe</td>
<td style="text-align: right;">77.65</td>
</tr>
<tr class="even">
<td style="text-align: right;">2007</td>
<td style="text-align: left;">Oceania</td>
<td style="text-align: right;">80.72</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>If you are a visual person like me and wish to see this transformation with explanations, check out this GIF I made using good ol’ Powerpoint.</p>
<div style="text-align:center;">
<div id="fig-gif" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-gif-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/0qMbFC1.gif" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-gif-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: {tidyr} pivot_longer() explained
</figcaption></figure>
</div>
</div>
</section><section id="conclusion" class="level1"><h1>Conclusion</h1>
<p><code>pivot_longer()</code> is the successor for the great <code>gather()</code> function and has many advantages over the latter. <code>pivot_longer()</code> repeats all the values in the columns that are not included in the <code>cols</code> argument. Therefore, if your dataframe/tibble had a primary key prior to the transformation, the primary key of your transformed “longer” dataframe is your old primary key + the new column created by <code>names_to</code>. This function has many other arguments that allow some truly great transformations. Mastering this function (and its wide counterpart) is a great skill upgrade while massaging your data to make it “tidy”.</p>
<p>Happy Gathering!</p>
</section><section id="references" class="level1"><h1>References</h1>
<ul>
<li>Hadley Wickham and Maximilian Girlich (2022). tidyr: Tidy Messy Data. R package version 1.2.0. <a href="https://CRAN.R-project.org/package=tidyr" class="uri">https://CRAN.R-project.org/package=tidyr</a>
</li>
<li>Yihui Xie (2022). knitr: A General-Purpose Package for Dynamic Report Generation in R. R package version 1.39.</li>
</ul>


</section><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p><strong>Long vs.&nbsp;Wide Data: What’s the Difference?</strong> https://www.statology.org/long-vs-wide-data/↩︎</p></li>
</ol></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2022,
  author = {Katti, Vishal},
  title = {Pivoting Your Tables with {Tidyr:} {Part} {I}},
  date = {2022-07-08},
  url = {https://vishalkatti.com/posts/tidyr-pivot-longer/},
  langid = {en},
  abstract = {This post demonstrates how to use `pivot\_longer()` to
    convert your wide data to long data. This is part 1 of the Pivoting
    your tables with Tidyr series.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2022" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2022. <span>“Pivoting Your Tables with Tidyr: Part
I.”</span> July 8, 2022. <a href="https://vishalkatti.com/posts/tidyr-pivot-longer/">https://vishalkatti.com/posts/tidyr-pivot-longer/</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>functions</category>
  <category>tidyr</category>
  <category>pivot</category>
  <guid>https://vishalkatti.com/posts/tidyr-pivot-longer/</guid>
  <pubDate>Thu, 07 Jul 2022 18:30:00 GMT</pubDate>
  <media:content url="https://i.imgur.com/0qMbFC1.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Writing Robust R Functions</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/writing-robust-functions/</link>
  <description><![CDATA[ <section id="introduction" class="level2"><h2 class="anchored" data-anchor-id="introduction">Introduction</h2>
<p>Functions in R ( or any other programming language in general) allow us to encapsulate some lines of code that we want to run again and again. Functions are the natural outcome of the <strong>DRY</strong><sup>1</sup> principle. Functions group together a couple of lines of consistent logic making our code modular and consequently, easy to manage. However, when we write functions, we need to ensure that they behave exactly as we want them to and are able to handle whatever we throw at them. By whatever, I mean any and all kinds of inputs. The idea of creating unbreakable code is idealistic. I say this since creating robust functions requires additional code to handle the unwanted inputs and most useRs write functions during some one-time analysis. Hence we need to be pragmatic about how much time and effort we spend trying to make our functions robust. Maybe, we need our functions to be just robust enough! All I am saying is, if you are creating functions that will be used by you and only you i.e.&nbsp;if you have absolute control over what inputs would be provided to your functions, then you can forego certain checks and the functions need not be unbreakable. But, if you intend to write functions that will be used by a larger audience, you need to ensure that such functions are able to handle all kinds of innocent and malicious intents.</p>
</section><section id="what-do-we-mean-by-robust-functions" class="level2"><h2 class="anchored" data-anchor-id="what-do-we-mean-by-robust-functions">What do we mean by Robust Functions?</h2>
<p>You must be familiar with the <em>Garbage-In-Garbage-Out</em> philosophy of Software engineering. We can think of it in terms of functions, that, given garbage or bad input, you get garbage or bad output. For a function to be robust, it must behave in a consistent manner for known and correct inputs, however, more importantly, it mustn’t give us garbage for bad inputs. Rather, it must provide useful output (as messages or instructions) which can be further used to inform the end-user about possible problems in the inputs to drive proper usage. The useful output/s in case of bad inputs would ideally be a combination of clean early exit and easy-to-understand error messages. So we shall try to implement <em>Garbage-In-Useful-Info-Out</em> by looking at some ways we can build well-behaved and reliable functions.</p>
<p>Input values passed to a function are more popularly known as arguments or parameters. A robust function must validate the function arguments before proceeding to implement the function logic. If this is not done, then the bad arguments will cause some errors in the logic and display error messages that the end-user may not be familiar with. Worst-case scenario is when the function doesn’t encounter any errors and just gives bad results!! Surely, we do not want this unpredictable behavior.</p>
<div style="text-align:center;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://i.imgflip.com/28bxz3.jpg" class="img-fluid figure-img" style="width:60.0%"></p>
<figcaption>Source: imgflip.com</figcaption></figure>
</div>
</div>
</section><section id="our-sweet-innocent-and-naive-function" class="level2"><h2 class="anchored" data-anchor-id="our-sweet-innocent-and-naive-function">Our sweet, innocent and naive Function</h2>
<p>Consider the following function <code>make_date</code> that takes 3 numeric inputs <code>yyyy</code>, <code>mm</code> and <code>dd</code> and returns a single `Date` object.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1">make_date <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>(yyyy, mm, dd) {</span>
<span id="cb1-2">  </span>
<span id="cb1-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic : Concatenate the values and convert to Date</span></span>
<span id="cb1-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb1-5">}</span>
<span id="cb1-6"></span>
<span id="cb1-7">my_date <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;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span>
<span id="cb1-8">my_date</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] "2022-01-31"</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(my_date)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] "Date"</code></pre>
</div>
</div>
<p>We will use <code>make_date</code> to demonstrate a couple of scenarios where this function can fail and the methods to safeguard against such scenarios.</p>
</section><section id="scenario-1-missing-arguments" class="level2"><h2 class="anchored" data-anchor-id="scenario-1-missing-arguments">Scenario 1: Missing Arguments</h2>
<p>The most basic check we should perform before running the function logic is to confirm if all the required arguments are available. Think about how your function should behave if one of the arguments, suppose <code>mm</code> is missing.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span></code></pre></div>
</details><pre style="color: red;"><code>Error in paste(yyyy, mm, dd, sep = "-"): argument "mm" is missing, with no default
</code></pre>
</div>
<p>Note that the error message shown to the user, is triggered, not from our function <code>make_date</code> but from the internal <code>paste</code> function. We do not have any control over what error messages are shown when errors occur. In this case, we know specifically that this error is due to a missing argument.</p>
<p>There are two ways to handle missing arguments:</p>
<section id="early-exit" class="level3"><h3 class="anchored" data-anchor-id="early-exit">1.1 Early Exit</h3>
<p>If a certain required argument is missing, we can stop the execution of the function and show informative error message about which argument is missing. Your friends here are the <code>missing</code> and <code>stop</code> functions. The <code>missing</code> function checks if the given argument is missing or is set to NULL and returns TRUE, else it returns FALSE. The <code>stop</code> function stops the execution and displays the custom error message we provide. Using these functions inside an <code>if</code> condition will let us check for missing arguments. Let us modify our naive function to stop early when required arguments are missing.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb6-1">make_date <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>(yyyy, mm, dd) {</span>
<span id="cb6-2">  </span>
<span id="cb6-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing arguments</span></span>
<span id="cb6-4">  <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;">missing</span>(yyyy)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` is required."</span>)</span>
<span id="cb6-5">  <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;">missing</span>(mm))   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` is required."</span>)</span>
<span id="cb6-6">  <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;">missing</span>(dd))   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` is required."</span>)</span>
<span id="cb6-7">  </span>
<span id="cb6-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic</span></span>
<span id="cb6-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb6-10">}</span>
<span id="cb6-11"></span>
<span id="cb6-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calling the function without `mm` argument</span></span>
<span id="cb6-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>)</span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = 2022, dd = 31): argument `mm` is required.
</code></pre>
</div>
<p>Note that here, we add three <code>if-missing-stop</code> statements, one for each required argument. We must do this if we want to display specific error messages for each argument. There is another way to do the same but we will look at it later. If we want to display a single error message, we can do so by clubbing the <code>missing</code> functions inside an <code>any</code> which will return TRUE if any one of the arguments is missing. However, providing clear error messages becomes challenging in this method.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb7-1">dummy_fun <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>(a, b, c) { </span>
<span id="cb7-2">  <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">missing</span>(a), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">missing</span>(b), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">missing</span>(c))) {</span>
<span id="cb7-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"One or more required arguments missing."</span>)</span>
<span id="cb7-4">  }</span>
<span id="cb7-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Do something...</span></span>
<span id="cb7-6">}</span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_fun</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></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_fun(a = 1): One or more required arguments missing.
</code></pre>
</div>
</section><section id="sensible-defaults-with-warnings" class="level3"><h3 class="anchored" data-anchor-id="sensible-defaults-with-warnings">1.2 Sensible defaults with warnings</h3>
<p>In some cases, we may need the function to use some sensible default value for the required arguments and continue execution. Here, we display a warning message instead of an error message. This is required when the argument value is either considered to be obvious or the argument is not necessarily the most important one and is used only in extreme customization. <strong>Providing default values to arguments makes them optional arguments</strong>. An example of default argument values can be seen in the <code>paste</code> function we have used earlier. The default value of the separator argument <code>sep</code> is a single whitespace character.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">args</span>(paste)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>function (..., sep = " ", collapse = NULL, recycle0 = FALSE) 
NULL</code></pre>
</div>
</div>
<p>Similarly, we can provide some sensible defaults for the <code>make_date</code> function. Let’s modify the function further to provide defaults for the <code>mm</code> and <code>dd</code> arguments only.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb10-1">make_date <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>(yyyy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb10-2">  </span>
<span id="cb10-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing arguments</span></span>
<span id="cb10-4">  <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;">missing</span>(yyyy))  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` is required."</span>) </span>
<span id="cb10-5">  <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;">missing</span>(mm)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` is missing. Using default value mm = 1 instead"</span>) </span>
<span id="cb10-6">  <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;">missing</span>(dd)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` is missing. Using default value dd = 1 instead"</span>)</span>
<span id="cb10-7">  </span>
<span id="cb10-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic</span></span>
<span id="cb10-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb10-10">}</span>
<span id="cb10-11"></span>
<span id="cb10-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calling the function without `mm` and `dd` arguments</span></span>
<span id="cb10-13"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># here, only `yyyy` is the required argument.</span></span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in make_date(yyyy = 2022): argument `mm` is missing. Using default value
mm = 1 instead
</code></pre>
<pre style="color: orange;"><code>Warning in make_date(yyyy = 2022): argument `dd` is missing. Using default value
dd = 1 instead
</code></pre>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "2022-01-01"</code></pre>
</div>
</div>
<p>There are a few concerns about using warnings instead of error messages. Some are listed here in this article from RBloggers <a href="https://www.r-bloggers.com/2012/05/a-warning-about-warning/">A Warning About <code>warning</code></a>.</p>
</section></section><section id="scenario-2-invalid-argument-data-type" class="level2"><h2 class="anchored" data-anchor-id="scenario-2-invalid-argument-data-type">Scenario 2: Invalid Argument Data Type</h2>
<p>We have defined <code>make_date</code> to accept 3 numeric arguments i.e.&nbsp;all 3 must be numbers. What would happen if someone tried to call <code>make_date</code> with character, factor or boolean inputs?</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2022"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"20"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># works!! why?</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] "2022-05-20"</code></pre>
</div>
</div>
<p>In this case, the function works because when the arguments are combined into a single string using <code>paste</code> , it matches the <code>format</code> argument of the <code>as.Date</code> function in the main logic of <code>make_date</code> which is <code>as.Date(paste(yyyy, mm, dd, sep = "-"), format = "%Y-%m-%d")</code></p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb14-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2022"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"May"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># works but shows NA !!!</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] NA</code></pre>
</div>
</div>
<p>In this case, all the arguments pass the checks but since we pass <code>2022-May-1</code> to <code>as.Date</code> which doesn’t match the <code>format = '%Y-%m-%d'</code> thus giving <code>NA</code>.</p>
<p>How do we check if the values provided to the arguments are numbers or number-like? If the values are numbers, we let them pass. But if they are non-numeric, we must check if they can be converted to numbers i.e. we must check if they are number-like. By number-like, I mean, will coercing the value using <code>as.numeric</code> give us a numeric value or <code>NA</code> ? You guessed it right, we will pass the values through <code>as.numeric</code> and check if the output is <code>NA</code> or not.</p>
<p>What are the various data types in R that are not <code>numeric</code> but can look like numbers? We have <code>character</code>, <code>factor</code> and <code>boolean</code> data types which can behave like numbers sometimes. Let’s see a few scenarios.</p>
<section id="character-arguments" class="level3"><h3 class="anchored" data-anchor-id="character-arguments">Character arguments</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb16-1">Year <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;">"2022"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TwentyTwo"</span>)</span>
<span id="cb16-2">Year_num <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.numeric</span>(Year) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this should show a warning about NAs introduced by coercion</span></span></code></pre></div>
</details><pre style="color: orange;"><code>Warning: NAs introduced by coercion
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb17-1">Year_num <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># must show the number 2022 without quotes and one NA</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 2022   NA</code></pre>
</div>
</div>
<p>As you can see in above example, when passed through <code>as.numeric</code>, the value “2022” gets converted to the number 2022 but the value “TwentyTwo” does not. Hence we can say “2022” is number-like but “TwentyTwo” is not.</p>
</section><section id="factor-arguments" class="level3"><h3 class="anchored" data-anchor-id="factor-arguments">Factor arguments</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb19-1">Year <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;">factor</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;">"2022"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TwentyTwo"</span>))</span>
<span id="cb19-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(Year)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 2 1 3</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb21-1">YearX <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;">factor</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;">"2022"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X"</span>))</span>
<span id="cb21-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(YearX)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 1 2</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb23-1">YearY <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;">factor</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>)</span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(YearY)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
</div>
<p>As you can see from above examples, <code>factor</code> values do get converted to numeric but do not give the right results. So we can safely say that factors are not number-like.</p>
<p>I will ignore <code>boolean</code> data types hoping that useRs are bright enough to not use Booleans while creating a Date!</p>
<p>From the above examples, we can conclude that <code>numeric</code> values and number-like <code>character</code> values are the only valid data types that should be allowed. Modifying our <code>make_date</code> function to include data type checks.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb25" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb25-1">make_date <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>(yyyy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb25-2">  </span>
<span id="cb25-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing arguments</span></span>
<span id="cb25-4">  <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;">missing</span>(yyyy))  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` is required."</span>) </span>
<span id="cb25-5">  <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;">missing</span>(mm)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` is missing. Using default value mm = 1 instead"</span>) </span>
<span id="cb25-6">  <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;">missing</span>(dd)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` is missing. Using default value dd = 1 instead"</span>)</span>
<span id="cb25-7">  </span>
<span id="cb25-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check data types</span></span>
<span id="cb25-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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.numeric</span>(yyyy) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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.character</span>(yyyy)) {</span>
<span id="cb25-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` must be numeric"</span>)</span>
<span id="cb25-11">  } <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> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.character</span>(yyyy) <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(yyyy))) {</span>
<span id="cb25-12">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` must be numeric"</span>)</span>
<span id="cb25-13">  }</span>
<span id="cb25-14">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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.numeric</span>(mm) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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.character</span>(mm)) {</span>
<span id="cb25-15">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` must be numeric"</span>)</span>
<span id="cb25-16">  } <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> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.character</span>(mm) <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(mm))) {</span>
<span id="cb25-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` must be numeric"</span>)</span>
<span id="cb25-18">  }</span>
<span id="cb25-19">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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.numeric</span>(dd) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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.character</span>(dd)) {</span>
<span id="cb25-20">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` must be numeric"</span>)</span>
<span id="cb25-21">  } <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> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.character</span>(dd) <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(dd))) {</span>
<span id="cb25-22">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` must be numeric"</span>)</span>
<span id="cb25-23">  }</span>
<span id="cb25-24">  </span>
<span id="cb25-25">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic</span></span>
<span id="cb25-26">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb25-27">}</span>
<span id="cb25-28"></span>
<span id="cb25-29"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calling the function with new datatype checks</span></span>
<span id="cb25-30"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2022"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"May"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>)</span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in make_date(yyyy = "2022", mm = "May", dd = "1"): NAs introduced by
coercion
</code></pre>
<pre style="color: red;"><code>Error in make_date(yyyy = "2022", mm = "May", dd = "1"): argument `mm` must be numeric
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb26" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb26-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2022"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>)</span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = "2022", mm = factor("5"), dd = "1"): argument `mm` must be numeric
</code></pre>
</div>
<p>Notice that the datatype check is lengthy and similar for all 3 arguments. We can apply <strong>DRY</strong> principle again and encapsulate that code into a small function <code>is_numberlike</code> which will return <code>TRUE</code> or <code>FALSE</code> . Note that <code>is_numberlike</code> has no checks because it is an internal function.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb27-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function check if value is number or number-like.</span></span>
<span id="cb27-2">is_numberlike <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="cb27-3">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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.numeric</span>(x) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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.character</span>(x)) {</span>
<span id="cb27-4">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Early Exit 1 if value is neither numeric nor character</span></span>
<span id="cb27-5">    <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;">FALSE</span>) </span>
<span id="cb27-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> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.character</span>(x) <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>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(x))) {</span>
<span id="cb27-7">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Early Exit 2 if character value is not number-like.</span></span>
<span id="cb27-8">    <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;">FALSE</span>) </span>
<span id="cb27-9">  }</span>
<span id="cb27-10">  <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;">TRUE</span>)</span>
<span id="cb27-11">}</span></code></pre></div>
</details>
</div>
<p>Thus our <code>make_date</code> function with data types check will look as below.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb28" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb28-1">make_date <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>(yyyy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb28-2">  </span>
<span id="cb28-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing arguments</span></span>
<span id="cb28-4">  <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;">missing</span>(yyyy))  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` is required."</span>) </span>
<span id="cb28-5">  <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;">missing</span>(mm)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` is missing. Using default value mm = 1 instead"</span>) </span>
<span id="cb28-6">  <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;">missing</span>(dd)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` is missing. Using default value dd = 1 instead"</span>)</span>
<span id="cb28-7">  </span>
<span id="cb28-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check data types</span></span>
<span id="cb28-9">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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_numberlike</span>(yyyy)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` must be numeric"</span>)</span>
<span id="cb28-10">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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_numberlike</span>(mm))   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` must be numeric"</span>)</span>
<span id="cb28-11">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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_numberlike</span>(dd))   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` must be numeric"</span>)</span>
<span id="cb28-12">  </span>
<span id="cb28-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic</span></span>
<span id="cb28-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb28-15">}</span>
<span id="cb28-16"></span>
<span id="cb28-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calling the function with new datatype checks</span></span>
<span id="cb28-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"TwentyTwo"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in is_numberlike(yyyy): NAs introduced by coercion
</code></pre>
<pre style="color: red;"><code>Error in make_date(yyyy = "TwentyTwo", mm = "5", dd = 1): argument `yyyy` must be numeric
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb29-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2022"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"5"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>)</span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = "2022", mm = factor("5"), dd = "1"): argument `mm` must be numeric
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb30" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb30-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one"</span>)</span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in is_numberlike(dd): NAs introduced by coercion
</code></pre>
<pre style="color: red;"><code>Error in make_date(yyyy = 2022, mm = 5, dd = "one"): argument `dd` must be numeric
</code></pre>
</div>
<p>One of the most interesting features of R is vectorization! Due to this feature, our function <code>make_date</code> behaves in interesting ways. In some cases, it is desirable and sometimes it is not.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb31-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>)</span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in if (is.character(x) &amp; is.na(as.numeric(x))) {: the condition has
length &gt; 1 and only the first element will be used
</code></pre>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "2022-01-01" "2022-02-01" "2022-03-01" "2022-04-01" "2022-05-01"
 [6] "2022-06-01" "2022-07-01" "2022-08-01" "2022-09-01" "2022-10-01"
[11] "2022-11-01" "2022-12-01"</code></pre>
</div>
</div>
<p>Note the above warnings. These warnings appear because the <code>if</code> statement checks if the condition provided results in a single <code>TRUE</code> or <code>FALSE</code> value. However, the output of the check <code>is.na(as.numeric(mm))</code> is a boolean vector of length 12. But <code>if</code> needs only 1 <code>TRUE</code> or <code>FALSE</code>.</p>
<p>The output contains 12 date values since <code>paste</code> is vectorised, it recycles the values for <code>yyyy</code> and <code>dd</code> to give us 12 dates!</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb33-1">mm <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span></span>
<span id="cb33-2"><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;">"Month"</span>, mm)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code> [1] "Month 1"  "Month 2"  "Month 3"  "Month 4"  "Month 5"  "Month 6" 
 [7] "Month 7"  "Month 8"  "Month 9"  "Month 10" "Month 11" "Month 12"</code></pre>
</div>
</div>
<p>What do we do if we want <code>make_date</code> to return just one date?</p>
</section></section><section id="scenario-3-incorrect-argument-size" class="level2"><h2 class="anchored" data-anchor-id="scenario-3-incorrect-argument-size">Scenario 3: Incorrect Argument Size</h2>
<p>To ensure <code>make_date</code> gives you just one date, we must ensure that the arguments have just value and is not a vector of multiple values i.e. <code>length(arg)==1</code>. Let’s further add a few checks for the data size of the arguments and rearrange the checks.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb35-1">make_date <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>(yyyy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb35-2">  </span>
<span id="cb35-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing arguments</span></span>
<span id="cb35-4">  <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;">missing</span>(yyyy))  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` is required."</span>) </span>
<span id="cb35-5">  <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;">missing</span>(mm)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` is missing. Using default value mm = 1 instead"</span>) </span>
<span id="cb35-6">  <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;">missing</span>(dd)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` is missing. Using default value dd = 1 instead"</span>)</span>
<span id="cb35-7">  </span>
<span id="cb35-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check argument lengths</span></span>
<span id="cb35-9">  <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;">length</span>(yyyy)<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Length of argument `yyyy` is "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(yyyy),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">". Must be only 1."</span>))</span>
<span id="cb35-10">  <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;">length</span>(mm)<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Length of argument `mm` is "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(mm),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">". Must be only 1."</span>))</span>
<span id="cb35-11">  <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;">length</span>(dd)<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="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Length of argument `dd` is "</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(dd),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">". Must be only 1."</span>))</span>
<span id="cb35-12">  </span>
<span id="cb35-13">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check data types</span></span>
<span id="cb35-14">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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_numberlike</span>(yyyy)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` must be numeric"</span>)</span>
<span id="cb35-15">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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_numberlike</span>(mm))   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` must be numeric"</span>)</span>
<span id="cb35-16">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</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_numberlike</span>(dd))   <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` must be numeric"</span>)</span>
<span id="cb35-17">  </span>
<span id="cb35-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic</span></span>
<span id="cb35-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb35-20">}</span>
<span id="cb35-21"></span>
<span id="cb35-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Calling function with new data size checks</span></span>
<span id="cb35-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"01"</span>)</span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = 2022, mm = 1:12, dd = "01"): Length of argument `mm` is 12. Must be only 1.
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb36" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb36-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</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;">"2021"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2022"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"1"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = c("2021", "2022"), mm = "1", dd = 1): Length of argument `yyyy` is 2. Must be only 1.
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</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;">"1"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2"</span>))</span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = 2022, mm = 1, dd = c("1", "2")): Length of argument `dd` is 2. Must be only 1.
</code></pre>
</div>
</section><section id="a-little-detour" class="level2"><h2 class="anchored" data-anchor-id="a-little-detour">A little detour…</h2>
<p>So far we checked for missing arguments, arguments with bad data types and arguments with incorrect sizes. We’ve used the <code>stop</code> function along with <code>if</code> to check for all failure conditions and show appropriate error messages. When we use <code>stop</code>, we must specify all the failure conditions and the number of specific error messages goes up as number of arguments increases.</p>
<p>In case of our <code>make_date</code>, if an argument is not missing, it must be a number-like value of length 1. To reduce the number of error messages, we can combine the error messages for data type and length. for eg, the error message could be <em>argument <code>yyyy</code> must be a number-like value of length 1.</em></p>
<p>Wouldn’t it be easier if we just specify what is the success condition aka the <strong>“happy path”</strong>, and show error for all other conditions? To do this, we can use the <code>stopifnot</code> function that let’s us specify all the happy paths. See example below.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb38-1">dummy_sum <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>(a, b, c){</span>
<span id="cb38-2">  </span>
<span id="cb38-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing</span></span>
<span id="cb38-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</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;">missing</span>(a) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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;">missing</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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;">missing</span>(c))</span>
<span id="cb38-5">  </span>
<span id="cb38-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check argument values</span></span>
<span id="cb38-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</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>(a) <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.numeric</span>(a) <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;">length</span>(a)<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="cb38-8">            <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.numeric</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(b)<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="cb38-9">            <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>(c) <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.numeric</span>(c) <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;">length</span>(c)<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="cb38-10">            )</span>
<span id="cb38-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(a, b, c)</span>
<span id="cb38-12">}</span>
<span id="cb38-13"></span>
<span id="cb38-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a is missing</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(b = 2, c = 3): !missing(a) &amp; !missing(b) &amp; !missing(c) is not TRUE
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb39" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb39-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_integer_</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a has NA value</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(a = NA_integer_, b = 2, c = 3): !is.na(a) &amp; is.numeric(a) &amp; length(a) == 1 is not TRUE
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</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="st" style="color: #20794D;
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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># b has non-numeric value</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(a = 1, b = "2", c = 3): !is.na(b) &amp; is.numeric(b) &amp; length(b) == 1 is not TRUE
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb41" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb41-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</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;">5</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;">7</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># c has length != 1</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(a = 1, b = 2, c = 5:7): !is.na(c) &amp; is.numeric(c) &amp; length(c) == 1 are not all TRUE
</code></pre>
</div>
<p>Note the error messages above. They are not so user-friendly. Luckily, we can specify error messages in <code>stopifnot</code> by providing the error messages as the names of the “happy path” conditions.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb42-1">dummy_sum <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>(a, b, c){</span>
<span id="cb42-2">  </span>
<span id="cb42-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing</span></span>
<span id="cb42-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"one or more required arguments missing"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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;">missing</span>(a) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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;">missing</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</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;">missing</span>(c))</span>
<span id="cb42-5">  </span>
<span id="cb42-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check argument values</span></span>
<span id="cb42-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `a` must not be NA, must be a number of length 1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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>(a) <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.numeric</span>(a) <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;">length</span>(a)<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="cb42-8">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `b` must not be NA, must be a number of length 1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.numeric</span>(b) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&amp;</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">length</span>(b)<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="cb42-9">            <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `c` must not be NA, must be a number of length 1"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</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>(c) <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.numeric</span>(c) <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;">length</span>(c)<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="cb42-10">            )</span>
<span id="cb42-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(a, b, c)</span>
<span id="cb42-12">}</span>
<span id="cb42-13"></span>
<span id="cb42-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a is missing</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(b = 2, c = 3): one or more required arguments missing
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb43" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb43-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">a =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_integer_</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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a has NA value</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(a = NA_integer_, b = 2, c = 3): argument `a` must not be NA, must be a number of length 1
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</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="st" style="color: #20794D;
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 class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># b has non-numeric value</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(a = 1, b = "2", c = 3): argument `b` must not be NA, must be a number of length 1
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb45" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb45-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">dummy_sum</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;">5</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;">7</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># c has length != 1</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in dummy_sum(a = 1, b = 2, c = 5:7): argument `c` must not be NA, must be a number of length 1
</code></pre>
</div>
<p>Using <code>stopifnot</code> in our <code>make_date</code> function to combine the datatype and length checks, we get…</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb46-1">make_date <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>(yyyy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb46-2">  </span>
<span id="cb46-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing arguments</span></span>
<span id="cb46-4">  <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;">missing</span>(yyyy))  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` is required."</span>) </span>
<span id="cb46-5">  <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;">missing</span>(mm)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` is missing. Using default value mm = 1 instead"</span>) </span>
<span id="cb46-6">  <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;">missing</span>(dd)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` is missing. Using default value dd = 1 instead"</span>)</span>
<span id="cb46-7">  </span>
<span id="cb46-8">  </span>
<span id="cb46-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check argument types and length</span></span>
<span id="cb46-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(</span>
<span id="cb46-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` must be numeric with length 1"</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;">is_numberlike</span>(yyyy) <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;">length</span>(yyyy)<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="cb46-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` must be numeric with length 1"</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;">is_numberlike</span>(mm)   <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;">length</span>(mm)<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="cb46-13">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` must be numeric with length 1"</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;">is_numberlike</span>(dd)   <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;">length</span>(dd)<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="cb46-14">  )</span>
<span id="cb46-15">  </span>
<span id="cb46-16">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic</span></span>
<span id="cb46-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb46-18">}</span>
<span id="cb46-19"></span>
<span id="cb46-20"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># no arguments provided</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(): argument `yyyy` is required.
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb47" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb47-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Length mm not equal to 1</span></span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in if (is.character(x) &amp; is.na(as.numeric(x))) {: the condition has
length &gt; 1 and only the first element will be used
</code></pre>
<pre style="color: red;"><code>Error in make_date(yyyy = 2022, mm = 1:12, dd = 31): argument `mm` must be numeric with length 1
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Jan"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># mm is not number-like</span></span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in is_numberlike(mm): NAs introduced by coercion
</code></pre>
<pre style="color: red;"><code>Error in make_date(yyyy = 2022, mm = "Jan", dd = 31): argument `mm` must be numeric with length 1
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb49" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb49-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># argument mm is missing but should work using default value</span></span></code></pre></div>
</details><pre style="color: orange;"><code>Warning in make_date(yyyy = 2022, dd = 31): argument `mm` is missing. Using
default value mm = 1 instead
</code></pre>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "2022-01-31"</code></pre>
</div>
</div>
</section><section id="scenario-4-values-of-arguments-that-result-in-invalid-outputs" class="level2"><h2 class="anchored" data-anchor-id="scenario-4-values-of-arguments-that-result-in-invalid-outputs">Scenario 4: Values of Arguments that result in invalid outputs</h2>
<p>Finally, what do we do when the arguments provided will definitely give us bad results despite passing all checks? In our case, <code>make_date</code> creates a date but if we give it values that will result in an invalid date, it will give us invalid results (remember <em>Garbage-In-Garbage-Out</em>?).</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb51" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb51-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</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;"># is there a 13th month?</span></span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1] NA</code></pre>
</div>
</div>
<p>We get <code>NA</code> because <code>as.Date</code> returns NA for invalid inputs with no error messages or warnings! We can check the output and provide a generic error message.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb53" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb53-1">make_date <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>(yyyy, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb53-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check missing arguments</span></span>
<span id="cb53-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;">missing</span>(yyyy))  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` is required."</span>) </span>
<span id="cb53-4">  <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;">missing</span>(mm)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` is missing. Using default value mm = 1 instead"</span>) </span>
<span id="cb53-5">  <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;">missing</span>(dd)) <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">warning</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` is missing. Using default value dd = 1 instead"</span>)</span>
<span id="cb53-6">  </span>
<span id="cb53-7">  </span>
<span id="cb53-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Check argument types and length</span></span>
<span id="cb53-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(</span>
<span id="cb53-10">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `yyyy` must be numeric with length 1"</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;">is_numberlike</span>(yyyy) <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;">length</span>(yyyy)<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="cb53-11">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `mm` must be numeric with length 1"</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;">is_numberlike</span>(mm)   <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;">length</span>(mm)<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="cb53-12">    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"argument `dd` must be numeric with length 1"</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;">is_numberlike</span>(dd)   <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;">length</span>(dd)<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="cb53-13">  )</span>
<span id="cb53-14">  </span>
<span id="cb53-15">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># main logic</span></span>
<span id="cb53-16">  out <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.Date</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste</span>(yyyy, mm, dd, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"-"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">format =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y-%m-%d"</span>)</span>
<span id="cb53-17">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(out)) {</span>
<span id="cb53-18">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stop</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Invalid values provided. Please check your inputs."</span>)</span>
<span id="cb53-19">  }</span>
<span id="cb53-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span>(out)</span>
<span id="cb53-21">}</span>
<span id="cb53-22"></span>
<span id="cb53-23"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">13</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dd =</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;"># is there a 13th month?</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = 2022, mm = 13, dd = 1): Invalid values provided. Please check your inputs.
</code></pre>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb54-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">make_date</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yyyy =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2022</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mm =</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;">dd =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">31</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># are there 31 days in February?</span></span></code></pre></div>
</details><pre style="color: red;"><code>Error in make_date(yyyy = 2022, mm = 2, dd = 31): Invalid values provided. Please check your inputs.
</code></pre>
</div>
<p>Do you think our function <code>make_date</code> is robust enough?</p>
<div style="text-align:center;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://i.imgur.com/hbjbLMN.gif" class="img-fluid figure-img"></p>
<figcaption>As robust as Superman! Source: Imgur</figcaption></figure>
</div>
</div>
</section><section id="conclusion" class="level2"><h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Making functions robust requires some prior thought about its intended use and audience. Based on this, we can decide what checks to implement, what to skip, whether to stop execution using error messages or to use default values with warnings. Checking for “happy paths” is simpler compared to checking each and every bad input and providing specific error messages. Too many different error messages for the same argument could become a source of frustration of the end user, so consider combining some checks and their error messages to be informative and precise. Robustness, like everything else, in moderation, is good and getting it “just right” takes time and dedicated effort. Happy Coding!</p>
</section><section id="citations-references" class="level2"><h2 class="anchored" data-anchor-id="citations-references">Citations &amp; References</h2>
<ul>
<li><a href="https://www.lexjansen.com/phuse/2014/ts/TS03.pdf">Techniques for writing robust R programs - LexJansen</a></li>
<li><a href="https://bookdown.org/rdpeng/rprogdatascience/functions.html">R Programming for Data Science</a></li>
<li><a href="https://www.r-bloggers.com/2012/05/a-warning-about-warning/">A Warning About warning</a></li>
</ul>


</section><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p><strong>D</strong>on’t <strong>R</strong>epeat <strong>Y</strong>ourself!↩︎</p></li>
</ol></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2022,
  author = {Katti, Vishal},
  title = {Writing {Robust} {R} {Functions}},
  date = {2022-01-18},
  url = {https://vishalkatti.com/posts/writing-robust-functions/},
  langid = {en},
  abstract = {This post demonstrates some techniques to make your R
    user-defined functions unbreakable (well, almost!) by checking if
    function arguments are missing, incorrect data type or just
    down-right invalid values and how to return meaningful error
    messages.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2022" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2022. <span>“Writing Robust R Functions.”</span> January
18, 2022. <a href="https://vishalkatti.com/posts/writing-robust-functions/">https://vishalkatti.com/posts/writing-robust-functions/</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>functions</category>
  <guid>https://vishalkatti.com/posts/writing-robust-functions/</guid>
  <pubDate>Mon, 17 Jan 2022 18:30:00 GMT</pubDate>
  <media:content url="https://i.imgur.com/hbjbLMN.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Unholy Trinity of R, Excel VBA and Powerpoint - Part 2 of 2</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/R2VBA2PPT2/</link>
  <description><![CDATA[ <p>This is part 2 of 2. Read part 1 <a href="https://www.vishalkatti.com/posts/R2VBA2PPT1/" title="link to Part 1 of this article.">here</a>.</p>
<section id="quick-recap" class="level2"><h2 class="anchored" data-anchor-id="quick-recap">Quick Recap</h2>
<p>In the previous post, we create the <code>.potx</code> template from the <code>.pptx</code> file we wanted to automate and the Excel template with the macro <code>.xlsm</code> that uses the PowerPoint template to create a new <code>.pptx</code> file with given data using VBA.</p>
<p>The report we want to automate is…</p>
<div style="text-align:center;">
<div id="fig-gapminder" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-gapminder-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/LL6UwWH.gif" title="The Gapminder Report" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-gapminder-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: The Gapminder Report : The PowerPoint presentation we want to automate
</figcaption></figure>
</div>
</div>
<p>…and the Excel and PowerPoint template we created are shown in Figure&nbsp;2.</p>
<div style="text-align:center;">
<div id="fig-excel-with-macro" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-excel-with-macro-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/tB3QxhJ.png" title="Excel Template with VBA macro" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-excel-with-macro-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Excel Template with VBA macro
</figcaption></figure>
</div>
</div>
<p>In this post, we will write the R script that will first massage the data into desired format and then load the data for one region into the Excel template and execute the VBA macro that will create the PowerPoint file with that data.</p>
</section><section id="strategy" class="level2"><h2 class="anchored" data-anchor-id="strategy">Strategy</h2>
<p>Before we dive into code, we need to check a few things. We wish to create a presentation for each continent in the Gapminder data. A closer look at the Presentation will tell you what kind of data we need for each slide/graph/table while the Excel template will reveal what should the structure of each dataset should be. While looking into this structure, some questions will pop-up. The idea here is to create the datasets in such a way that they can be easily filtered for each continent and the resultant table can be written to the Excel template without any or very little modification. Let us proceed slide-by-slide.</p>
</section><section id="creating-the-datasets" class="level2"><h2 class="anchored" data-anchor-id="creating-the-datasets">Creating the datasets</h2>
<section id="slide-1" class="level3"><h3 class="anchored" data-anchor-id="slide-1">Slide 1</h3>
<p>Slide 1 is the title page and needs 2 strings; one for Title, one for Subtitle. The Title for the base presentation is “World Population”. For each continent, it could be “&lt;continent_name&gt; Population”. The subtitle is a combination of Author Name and Created Date. So we need a string like “&lt;author_name&gt; | &lt;created_date&gt;” where created_date is the formatted system date.</p>
<p>These strings can be created while writing the data to the Excel template.</p>
</section><section id="slide-2" class="level3"><h3 class="anchored" data-anchor-id="slide-2">Slide 2</h3>
<p>The chart on slide 2 needs raw data structured as below. You will notice that at a continent-level, this table needs a minimum of 5 countries. Do we have any continents in the Gapminder data with less than 5 countries? Yes, we have Oceania with only Australia and New Zealand. For ease of use, let us include these countries along with Asian countries in a new <code>Region</code> variable.</p>
<div style="text-align:center;">
<div id="fig-slide2" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-slide2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/KvGxnyt.png" title="02_chart" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-slide2-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: 02_chart
</figcaption></figure>
</div>
</div>
<p>We will create the <code>region</code> variable in the gapminder data. But first, let us load some relevant packages.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">options</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">tidyverse.quiet =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># duh!</span></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rmarkdown) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to display the tables interactively in this post. Not really needed for the final solution.</span></span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(openxlsx) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to write the data to the Excel Template.</span></span>
<span id="cb1-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># library(RDCOMClient) # to load and run the Excel macro post data load.</span></span></code></pre></div>
</details>
</div>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Read in Gapminder data</span></span>
<span id="cb2-2">gp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gapminder<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>gapminder</span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create new region variable</span></span>
<span id="cb2-5">gp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb2-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;">region =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">if_else</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(continent) <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;">"Asia"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Oceania"</span>),</span>
<span id="cb2-7">                          <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Asia-Pacific"</span>, </span>
<span id="cb2-8">                          <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(continent)),</span>
<span id="cb2-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">country =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.character</span>(country))</span>
<span id="cb2-10"></span>
<span id="cb2-11"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Keep only relevant columns</span></span>
<span id="cb2-12">gp <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(region, country, year, pop)</span>
<span id="cb2-13"></span>
<span id="cb2-14"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># View details</span></span>
<span id="cb2-15"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(gp)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>Rows: 1,704
Columns: 4
$ region  &lt;chr&gt; "Asia-Pacific", "Asia-Pacific", "Asia-Pacific", "Asia-Pacific"~
$ country &lt;chr&gt; "Afghanistan", "Afghanistan", "Afghanistan", "Afghanistan", "A~
$ year    &lt;int&gt; 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 20~
$ pop     &lt;int&gt; 8425333, 9240934, 10267083, 11537966, 13079460, 14880372, 1288~</code></pre>
</div>
</div>
<p>Now that we have the source data available, we must now create the datasets we need that we can write to the Excel template for each region.</p>
<p>The required table in Figure&nbsp;3 shows the top 4 countries (based on 2007 population) and all other countries clubbed into ‘others’ in a given region and then the total population of the region on a yearly basis. This table has to be created for all 4 regions.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb4-1">pop_trend <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gp <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;">group_by</span>(region, country, year) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(pop, <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-4">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb4-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;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(pop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E6</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;">%&gt;%</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># population in millions</span></span>
<span id="cb4-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_wider</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_from =</span> year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_from =</span> pop, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_sort =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2007</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;"># sort by max pop to min pop in latest year i.e. 2007</span></span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(pop_trend)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["country"],"name":[2],"type":["chr"],"align":["left"]},{"label":["1952"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["1957"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["1962"],"name":[5],"type":["dbl"],"align":["right"]},{"label":["1967"],"name":[6],"type":["dbl"],"align":["right"]},{"label":["1972"],"name":[7],"type":["dbl"],"align":["right"]},{"label":["1977"],"name":[8],"type":["dbl"],"align":["right"]},{"label":["1982"],"name":[9],"type":["dbl"],"align":["right"]},{"label":["1987"],"name":[10],"type":["dbl"],"align":["right"]},{"label":["1992"],"name":[11],"type":["dbl"],"align":["right"]},{"label":["1997"],"name":[12],"type":["dbl"],"align":["right"]},{"label":["2002"],"name":[13],"type":["dbl"],"align":["right"]},{"label":["2007"],"name":[14],"type":["dbl"],"align":["right"]}],"data":[{"1":"Asia-Pacific","2":"China","3":"556","4":"637","5":"666","6":"755","7":"862","8":"943","9":"1000","10":"1084","11":"1165","12":"1230","13":"1280","14":"1319"},{"1":"Asia-Pacific","2":"India","3":"372","4":"409","5":"454","6":"506","7":"567","8":"634","9":"708","10":"788","11":"872","12":"959","13":"1034","14":"1110"},{"1":"Americas","2":"United States","3":"158","4":"172","5":"187","6":"199","7":"210","8":"220","9":"232","10":"243","11":"257","12":"273","13":"288","14":"301"},{"1":"Asia-Pacific","2":"Indonesia","3":"82","4":"90","5":"99","6":"109","7":"121","8":"137","9":"153","10":"169","11":"185","12":"199","13":"211","14":"224"},{"1":"Americas","2":"Brazil","3":"57","4":"66","5":"76","6":"88","7":"101","8":"114","9":"129","10":"143","11":"156","12":"169","13":"180","14":"190"},{"1":"Asia-Pacific","2":"Pakistan","3":"41","4":"47","5":"53","6":"61","7":"69","8":"78","9":"91","10":"105","11":"120","12":"136","13":"153","14":"169"},{"1":"Asia-Pacific","2":"Bangladesh","3":"47","4":"51","5":"57","6":"63","7":"71","8":"80","9":"93","10":"104","11":"114","12":"123","13":"136","14":"150"},{"1":"Africa","2":"Nigeria","3":"33","4":"37","5":"42","6":"47","7":"54","8":"62","9":"73","10":"82","11":"93","12":"106","13":"120","14":"135"},{"1":"Asia-Pacific","2":"Japan","3":"86","4":"92","5":"96","6":"101","7":"107","8":"114","9":"118","10":"122","11":"124","12":"126","13":"127","14":"127"},{"1":"Americas","2":"Mexico","3":"30","4":"35","5":"41","6":"48","7":"56","8":"64","9":"72","10":"80","11":"88","12":"96","13":"102","14":"109"},{"1":"Asia-Pacific","2":"Philippines","3":"22","4":"26","5":"30","6":"35","7":"41","8":"47","9":"53","10":"60","11":"67","12":"75","13":"83","14":"91"},{"1":"Asia-Pacific","2":"Vietnam","3":"26","4":"29","5":"34","6":"39","7":"45","8":"51","9":"56","10":"63","11":"70","12":"76","13":"81","14":"85"},{"1":"Europe","2":"Germany","3":"69","4":"71","5":"74","6":"76","7":"79","8":"78","9":"78","10":"78","11":"81","12":"82","13":"82","14":"82"},{"1":"Africa","2":"Egypt","3":"22","4":"25","5":"28","6":"32","7":"35","8":"39","9":"46","10":"53","11":"59","12":"66","13":"73","14":"80"},{"1":"Africa","2":"Ethiopia","3":"21","4":"23","5":"25","6":"28","7":"31","8":"35","9":"38","10":"43","11":"52","12":"60","13":"68","14":"77"},{"1":"Europe","2":"Turkey","3":"22","4":"26","5":"30","6":"33","7":"37","8":"42","9":"47","10":"53","11":"58","12":"63","13":"67","14":"71"},{"1":"Asia-Pacific","2":"Iran","3":"17","4":"20","5":"23","6":"27","7":"31","8":"35","9":"43","10":"52","11":"60","12":"63","13":"67","14":"69"},{"1":"Africa","2":"Congo, Dem. Rep.","3":"14","4":"16","5":"17","6":"20","7":"23","8":"26","9":"31","10":"35","11":"42","12":"48","13":"55","14":"65"},{"1":"Asia-Pacific","2":"Thailand","3":"21","4":"25","5":"29","6":"34","7":"39","8":"44","9":"49","10":"53","11":"57","12":"60","13":"63","14":"65"},{"1":"Europe","2":"France","3":"42","4":"44","5":"47","6":"50","7":"52","8":"53","9":"54","10":"56","11":"57","12":"59","13":"60","14":"61"},{"1":"Europe","2":"United Kingdom","3":"50","4":"51","5":"53","6":"55","7":"56","8":"56","9":"56","10":"57","11":"58","12":"59","13":"60","14":"61"},{"1":"Europe","2":"Italy","3":"48","4":"49","5":"51","6":"53","7":"54","8":"56","9":"57","10":"57","11":"57","12":"57","13":"58","14":"58"},{"1":"Asia-Pacific","2":"Korea, Rep.","3":"21","4":"23","5":"26","6":"30","7":"34","8":"36","9":"39","10":"42","11":"44","12":"46","13":"48","14":"49"},{"1":"Asia-Pacific","2":"Myanmar","3":"20","4":"22","5":"24","6":"26","7":"28","8":"32","9":"35","10":"38","11":"41","12":"43","13":"46","14":"48"},{"1":"Africa","2":"South Africa","3":"14","4":"16","5":"18","6":"21","7":"24","8":"27","9":"31","10":"36","11":"40","12":"43","13":"44","14":"44"},{"1":"Americas","2":"Colombia","3":"12","4":"14","5":"17","6":"20","7":"23","8":"25","9":"28","10":"31","11":"34","12":"38","13":"41","14":"44"},{"1":"Africa","2":"Sudan","3":"9","4":"10","5":"11","6":"13","7":"15","8":"17","9":"20","10":"25","11":"28","12":"32","13":"37","14":"42"},{"1":"Americas","2":"Argentina","3":"18","4":"20","5":"21","6":"23","7":"25","8":"27","9":"29","10":"32","11":"34","12":"36","13":"38","14":"40"},{"1":"Europe","2":"Spain","3":"29","4":"30","5":"31","6":"33","7":"35","8":"36","9":"38","10":"39","11":"40","12":"40","13":"40","14":"40"},{"1":"Europe","2":"Poland","3":"26","4":"28","5":"30","6":"32","7":"33","8":"35","9":"36","10":"38","11":"38","12":"39","13":"39","14":"39"},{"1":"Africa","2":"Tanzania","3":"8","4":"9","5":"11","6":"13","7":"15","8":"17","9":"20","10":"23","11":"27","12":"31","13":"35","14":"38"},{"1":"Africa","2":"Kenya","3":"6","4":"7","5":"9","6":"10","7":"12","8":"15","9":"18","10":"21","11":"25","12":"28","13":"31","14":"36"},{"1":"Africa","2":"Morocco","3":"10","4":"11","5":"13","6":"15","7":"17","8":"18","9":"20","10":"23","11":"26","12":"29","13":"31","14":"34"},{"1":"Africa","2":"Algeria","3":"9","4":"10","5":"11","6":"13","7":"15","8":"17","9":"20","10":"23","11":"26","12":"29","13":"31","14":"33"},{"1":"Americas","2":"Canada","3":"15","4":"17","5":"19","6":"21","7":"22","8":"24","9":"25","10":"27","11":"29","12":"30","13":"32","14":"33"},{"1":"Asia-Pacific","2":"Afghanistan","3":"8","4":"9","5":"10","6":"12","7":"13","8":"15","9":"13","10":"14","11":"16","12":"22","13":"25","14":"32"},{"1":"Africa","2":"Uganda","3":"6","4":"7","5":"8","6":"9","7":"10","8":"11","9":"13","10":"15","11":"18","12":"21","13":"25","14":"29"},{"1":"Americas","2":"Peru","3":"8","4":"9","5":"11","6":"12","7":"14","8":"16","9":"18","10":"20","11":"22","12":"25","13":"27","14":"29"},{"1":"Asia-Pacific","2":"Nepal","3":"9","4":"10","5":"10","6":"11","7":"12","8":"14","9":"16","10":"18","11":"20","12":"23","13":"26","14":"29"},{"1":"Asia-Pacific","2":"Saudi Arabia","3":"4","4":"4","5":"5","6":"6","7":"6","8":"8","9":"11","10":"15","11":"17","12":"21","13":"25","14":"28"},{"1":"Asia-Pacific","2":"Iraq","3":"5","4":"6","5":"7","6":"9","7":"10","8":"12","9":"14","10":"17","11":"18","12":"21","13":"24","14":"27"},{"1":"Americas","2":"Venezuela","3":"5","4":"7","5":"8","6":"10","7":"12","8":"14","9":"16","10":"18","11":"20","12":"22","13":"24","14":"26"},{"1":"Asia-Pacific","2":"Malaysia","3":"7","4":"8","5":"9","6":"10","7":"11","8":"13","9":"14","10":"16","11":"18","12":"20","13":"23","14":"25"},{"1":"Africa","2":"Ghana","3":"6","4":"6","5":"7","6":"8","7":"9","8":"11","9":"11","10":"14","11":"16","12":"18","13":"21","14":"23"},{"1":"Asia-Pacific","2":"Korea, Dem. Rep.","3":"9","4":"9","5":"11","6":"13","7":"15","8":"16","9":"18","10":"19","11":"21","12":"22","13":"22","14":"23"},{"1":"Asia-Pacific","2":"Taiwan","3":"9","4":"10","5":"12","6":"14","7":"15","8":"17","9":"19","10":"20","11":"21","12":"22","13":"22","14":"23"},{"1":"Asia-Pacific","2":"Yemen, Rep.","3":"5","4":"5","5":"6","6":"7","7":"7","8":"8","9":"10","10":"11","11":"13","12":"16","13":"19","14":"22"},{"1":"Europe","2":"Romania","3":"17","4":"18","5":"19","6":"19","7":"21","8":"22","9":"22","10":"23","11":"23","12":"23","13":"22","14":"22"},{"1":"Africa","2":"Mozambique","3":"6","4":"7","5":"8","6":"9","7":"10","8":"11","9":"13","10":"13","11":"13","12":"17","13":"18","14":"20"},{"1":"Asia-Pacific","2":"Australia","3":"9","4":"10","5":"11","6":"12","7":"13","8":"14","9":"15","10":"16","11":"17","12":"19","13":"20","14":"20"},{"1":"Asia-Pacific","2":"Sri Lanka","3":"8","4":"9","5":"10","6":"12","7":"13","8":"14","9":"15","10":"16","11":"18","12":"19","13":"20","14":"20"},{"1":"Africa","2":"Madagascar","3":"5","4":"5","5":"6","6":"6","7":"7","8":"8","9":"9","10":"11","11":"12","12":"14","13":"16","14":"19"},{"1":"Asia-Pacific","2":"Syria","3":"4","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9","10":"11","11":"13","12":"15","13":"17","14":"19"},{"1":"Africa","2":"Cameroon","3":"5","4":"5","5":"6","6":"6","7":"7","8":"8","9":"9","10":"11","11":"12","12":"14","13":"16","14":"18"},{"1":"Africa","2":"Cote d'Ivoire","3":"3","4":"3","5":"4","6":"5","7":"6","8":"7","9":"9","10":"11","11":"13","12":"15","13":"16","14":"18"},{"1":"Europe","2":"Netherlands","3":"10","4":"11","5":"12","6":"13","7":"13","8":"14","9":"14","10":"15","11":"15","12":"16","13":"16","14":"17"},{"1":"Americas","2":"Chile","3":"6","4":"7","5":"8","6":"9","7":"10","8":"11","9":"11","10":"12","11":"14","12":"15","13":"15","14":"16"},{"1":"Africa","2":"Burkina Faso","3":"4","4":"5","5":"5","6":"5","7":"5","8":"6","9":"7","10":"8","11":"9","12":"10","13":"12","14":"14"},{"1":"Americas","2":"Ecuador","3":"4","4":"4","5":"5","6":"5","7":"6","8":"7","9":"8","10":"10","11":"11","12":"12","13":"13","14":"14"},{"1":"Asia-Pacific","2":"Cambodia","3":"5","4":"5","5":"6","6":"7","7":"7","8":"7","9":"7","10":"8","11":"10","12":"12","13":"13","14":"14"},{"1":"Africa","2":"Malawi","3":"3","4":"3","5":"4","6":"4","7":"5","8":"6","9":"7","10":"8","11":"10","12":"10","13":"12","14":"13"},{"1":"Africa","2":"Niger","3":"3","4":"4","5":"4","6":"5","7":"5","8":"6","9":"6","10":"7","11":"8","12":"10","13":"11","14":"13"},{"1":"Americas","2":"Guatemala","3":"3","4":"4","5":"4","6":"5","7":"5","8":"6","9":"6","10":"7","11":"8","12":"10","13":"11","14":"13"},{"1":"Africa","2":"Angola","3":"4","4":"5","5":"5","6":"5","7":"6","8":"6","9":"7","10":"8","11":"9","12":"10","13":"11","14":"12"},{"1":"Africa","2":"Mali","3":"4","4":"4","5":"5","6":"5","7":"6","8":"6","9":"7","10":"8","11":"8","12":"9","13":"11","14":"12"},{"1":"Africa","2":"Senegal","3":"3","4":"3","5":"3","6":"4","7":"5","8":"5","9":"6","10":"7","11":"8","12":"10","13":"11","14":"12"},{"1":"Africa","2":"Zambia","3":"3","4":"3","5":"3","6":"4","7":"5","8":"5","9":"6","10":"7","11":"8","12":"9","13":"11","14":"12"},{"1":"Africa","2":"Zimbabwe","3":"3","4":"4","5":"4","6":"5","7":"6","8":"7","9":"8","10":"9","11":"11","12":"11","13":"12","14":"12"},{"1":"Americas","2":"Cuba","3":"6","4":"7","5":"7","6":"8","7":"9","8":"10","9":"10","10":"10","11":"11","12":"11","13":"11","14":"11"},{"1":"Europe","2":"Greece","3":"8","4":"8","5":"8","6":"9","7":"9","8":"9","9":"10","10":"10","11":"10","12":"11","13":"11","14":"11"},{"1":"Europe","2":"Portugal","3":"9","4":"9","5":"9","6":"9","7":"9","8":"10","9":"10","10":"10","11":"10","12":"10","13":"10","14":"11"},{"1":"Africa","2":"Chad","3":"3","4":"3","5":"3","6":"3","7":"4","8":"4","9":"5","10":"5","11":"6","12":"8","13":"9","14":"10"},{"1":"Africa","2":"Guinea","3":"3","4":"3","5":"3","6":"3","7":"4","8":"4","9":"5","10":"6","11":"7","12":"8","13":"9","14":"10"},{"1":"Africa","2":"Tunisia","3":"4","4":"4","5":"4","6":"5","7":"5","8":"6","9":"7","10":"8","11":"9","12":"9","13":"10","14":"10"},{"1":"Europe","2":"Belgium","3":"9","4":"9","5":"9","6":"10","7":"10","8":"10","9":"10","10":"10","11":"10","12":"10","13":"10","14":"10"},{"1":"Europe","2":"Czech Republic","3":"9","4":"10","5":"10","6":"10","7":"10","8":"10","9":"10","10":"10","11":"10","12":"10","13":"10","14":"10"},{"1":"Europe","2":"Hungary","3":"10","4":"10","5":"10","6":"10","7":"10","8":"11","9":"11","10":"11","11":"10","12":"10","13":"10","14":"10"},{"1":"Europe","2":"Serbia","3":"7","4":"7","5":"8","6":"8","7":"8","8":"9","9":"9","10":"9","11":"10","12":"10","13":"10","14":"10"},{"1":"Africa","2":"Rwanda","3":"3","4":"3","5":"3","6":"3","7":"4","8":"5","9":"6","10":"6","11":"7","12":"7","13":"8","14":"9"},{"1":"Africa","2":"Somalia","3":"3","4":"3","5":"3","6":"3","7":"4","8":"4","9":"6","10":"7","11":"6","12":"7","13":"8","14":"9"},{"1":"Americas","2":"Bolivia","3":"3","4":"3","5":"4","6":"4","7":"5","8":"5","9":"6","10":"6","11":"7","12":"8","13":"8","14":"9"},{"1":"Americas","2":"Dominican Republic","3":"2","4":"3","5":"3","6":"4","7":"5","8":"5","9":"6","10":"7","11":"7","12":"8","13":"9","14":"9"},{"1":"Americas","2":"Haiti","3":"3","4":"4","5":"4","6":"4","7":"5","8":"5","9":"5","10":"6","11":"6","12":"7","13":"8","14":"9"},{"1":"Europe","2":"Sweden","3":"7","4":"7","5":"8","6":"8","7":"8","8":"8","9":"8","10":"8","11":"9","12":"9","13":"9","14":"9"},{"1":"Africa","2":"Benin","3":"2","4":"2","5":"2","6":"2","7":"3","8":"3","9":"4","10":"4","11":"5","12":"6","13":"7","14":"8"},{"1":"Africa","2":"Burundi","3":"2","4":"3","5":"3","6":"3","7":"4","8":"4","9":"5","10":"5","11":"6","12":"6","13":"7","14":"8"},{"1":"Europe","2":"Austria","3":"7","4":"7","5":"7","6":"7","7":"8","8":"8","9":"8","10":"8","11":"8","12":"8","13":"8","14":"8"},{"1":"Europe","2":"Switzerland","3":"5","4":"5","5":"6","6":"6","7":"6","8":"6","9":"6","10":"7","11":"7","12":"7","13":"7","14":"8"},{"1":"Americas","2":"El Salvador","3":"2","4":"2","5":"3","6":"3","7":"4","8":"4","9":"4","10":"5","11":"5","12":"6","13":"6","14":"7"},{"1":"Americas","2":"Honduras","3":"2","4":"2","5":"2","6":"3","7":"3","8":"3","9":"4","10":"4","11":"5","12":"6","13":"7","14":"7"},{"1":"Americas","2":"Paraguay","3":"2","4":"2","5":"2","6":"2","7":"3","8":"3","9":"3","10":"4","11":"4","12":"5","13":"6","14":"7"},{"1":"Asia-Pacific","2":"Hong Kong, China","3":"2","4":"3","5":"3","6":"4","7":"4","8":"5","9":"5","10":"6","11":"6","12":"6","13":"7","14":"7"},{"1":"Europe","2":"Bulgaria","3":"7","4":"8","5":"8","6":"8","7":"9","8":"9","9":"9","10":"9","11":"9","12":"8","13":"8","14":"7"},{"1":"Africa","2":"Libya","3":"1","4":"1","5":"1","6":"2","7":"2","8":"3","9":"3","10":"4","11":"4","12":"5","13":"5","14":"6"},{"1":"Africa","2":"Sierra Leone","3":"2","4":"2","5":"2","6":"3","7":"3","8":"3","9":"3","10":"4","11":"4","12":"5","13":"5","14":"6"},{"1":"Africa","2":"Togo","3":"1","4":"1","5":"2","6":"2","7":"2","8":"2","9":"3","10":"3","11":"4","12":"4","13":"5","14":"6"},{"1":"Americas","2":"Nicaragua","3":"1","4":"1","5":"2","6":"2","7":"2","8":"3","9":"3","10":"3","11":"4","12":"5","13":"5","14":"6"},{"1":"Asia-Pacific","2":"Israel","3":"2","4":"2","5":"2","6":"3","7":"3","8":"3","9":"4","10":"4","11":"5","12":"6","13":"6","14":"6"},{"1":"Asia-Pacific","2":"Jordan","3":"1","4":"1","5":"1","6":"1","7":"2","8":"2","9":"2","10":"3","11":"4","12":"5","13":"5","14":"6"},{"1":"Africa","2":"Eritrea","3":"1","4":"2","5":"2","6":"2","7":"2","8":"3","9":"3","10":"3","11":"4","12":"4","13":"4","14":"5"},{"1":"Asia-Pacific","2":"Singapore","3":"1","4":"1","5":"2","6":"2","7":"2","8":"2","9":"3","10":"3","11":"3","12":"4","13":"4","14":"5"},{"1":"Europe","2":"Bosnia and Herzegovina","3":"3","4":"3","5":"3","6":"4","7":"4","8":"4","9":"4","10":"4","11":"4","12":"4","13":"4","14":"5"},{"1":"Europe","2":"Denmark","3":"4","4":"4","5":"5","6":"5","7":"5","8":"5","9":"5","10":"5","11":"5","12":"5","13":"5","14":"5"},{"1":"Europe","2":"Finland","3":"4","4":"4","5":"4","6":"5","7":"5","8":"5","9":"5","10":"5","11":"5","12":"5","13":"5","14":"5"},{"1":"Europe","2":"Norway","3":"3","4":"3","5":"4","6":"4","7":"4","8":"4","9":"4","10":"4","11":"4","12":"4","13":"5","14":"5"},{"1":"Europe","2":"Slovak Republic","3":"4","4":"4","5":"4","6":"4","7":"5","8":"5","9":"5","10":"5","11":"5","12":"5","13":"5","14":"5"},{"1":"Africa","2":"Central African Republic","3":"1","4":"1","5":"2","6":"2","7":"2","8":"2","9":"2","10":"3","11":"3","12":"4","13":"4","14":"4"},{"1":"Africa","2":"Congo, Rep.","3":"1","4":"1","5":"1","6":"1","7":"1","8":"2","9":"2","10":"2","11":"2","12":"3","13":"3","14":"4"},{"1":"Americas","2":"Costa Rica","3":"1","4":"1","5":"1","6":"2","7":"2","8":"2","9":"2","10":"3","11":"3","12":"4","13":"4","14":"4"},{"1":"Americas","2":"Puerto Rico","3":"2","4":"2","5":"2","6":"3","7":"3","8":"3","9":"3","10":"3","11":"4","12":"4","13":"4","14":"4"},{"1":"Asia-Pacific","2":"Lebanon","3":"1","4":"2","5":"2","6":"2","7":"3","8":"3","9":"3","10":"3","11":"3","12":"3","13":"4","14":"4"},{"1":"Asia-Pacific","2":"New Zealand","3":"2","4":"2","5":"2","6":"3","7":"3","8":"3","9":"3","10":"3","11":"3","12":"4","13":"4","14":"4"},{"1":"Asia-Pacific","2":"West Bank and Gaza","3":"1","4":"1","5":"1","6":"1","7":"1","8":"1","9":"1","10":"2","11":"2","12":"3","13":"3","14":"4"},{"1":"Europe","2":"Albania","3":"1","4":"1","5":"2","6":"2","7":"2","8":"3","9":"3","10":"3","11":"3","12":"3","13":"4","14":"4"},{"1":"Europe","2":"Croatia","3":"4","4":"4","5":"4","6":"4","7":"4","8":"4","9":"4","10":"4","11":"4","12":"4","13":"4","14":"4"},{"1":"Europe","2":"Ireland","3":"3","4":"3","5":"3","6":"3","7":"3","8":"3","9":"3","10":"4","11":"4","12":"4","13":"4","14":"4"},{"1":"Africa","2":"Liberia","3":"1","4":"1","5":"1","6":"1","7":"1","8":"2","9":"2","10":"2","11":"2","12":"2","13":"3","14":"3"},{"1":"Africa","2":"Mauritania","3":"1","4":"1","5":"1","6":"1","7":"1","8":"1","9":"2","10":"2","11":"2","12":"2","13":"3","14":"3"},{"1":"Americas","2":"Jamaica","3":"1","4":"2","5":"2","6":"2","7":"2","8":"2","9":"2","10":"2","11":"2","12":"3","13":"3","14":"3"},{"1":"Americas","2":"Panama","3":"1","4":"1","5":"1","6":"1","7":"2","8":"2","9":"2","10":"2","11":"2","12":"3","13":"3","14":"3"},{"1":"Americas","2":"Uruguay","3":"2","4":"2","5":"3","6":"3","7":"3","8":"3","9":"3","10":"3","11":"3","12":"3","13":"3","14":"3"},{"1":"Asia-Pacific","2":"Kuwait","3":"0","4":"0","5":"0","6":"1","7":"1","8":"1","9":"1","10":"2","11":"1","12":"2","13":"2","14":"3"},{"1":"Asia-Pacific","2":"Mongolia","3":"1","4":"1","5":"1","6":"1","7":"1","8":"2","9":"2","10":"2","11":"2","12":"2","13":"3","14":"3"},{"1":"Asia-Pacific","2":"Oman","3":"1","4":"1","5":"1","6":"1","7":"1","8":"1","9":"1","10":"2","11":"2","12":"2","13":"3","14":"3"},{"1":"Africa","2":"Botswana","3":"0","4":"0","5":"1","6":"1","7":"1","8":"1","9":"1","10":"1","11":"1","12":"2","13":"2","14":"2"},{"1":"Africa","2":"Gambia","3":"0","4":"0","5":"0","6":"0","7":"1","8":"1","9":"1","10":"1","11":"1","12":"1","13":"1","14":"2"},{"1":"Africa","2":"Lesotho","3":"1","4":"1","5":"1","6":"1","7":"1","8":"1","9":"1","10":"2","11":"2","12":"2","13":"2","14":"2"},{"1":"Africa","2":"Namibia","3":"0","4":"1","5":"1","6":"1","7":"1","8":"1","9":"1","10":"1","11":"2","12":"2","13":"2","14":"2"},{"1":"Europe","2":"Slovenia","3":"1","4":"2","5":"2","6":"2","7":"2","8":"2","9":"2","10":"2","11":"2","12":"2","13":"2","14":"2"},{"1":"Africa","2":"Comoros","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"1","13":"1","14":"1"},{"1":"Africa","2":"Equatorial Guinea","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0","13":"0","14":"1"},{"1":"Africa","2":"Gabon","3":"0","4":"0","5":"0","6":"0","7":"1","8":"1","9":"1","10":"1","11":"1","12":"1","13":"1","14":"1"},{"1":"Africa","2":"Guinea-Bissau","3":"1","4":"1","5":"1","6":"1","7":"1","8":"1","9":"1","10":"1","11":"1","12":"1","13":"1","14":"1"},{"1":"Africa","2":"Mauritius","3":"1","4":"1","5":"1","6":"1","7":"1","8":"1","9":"1","10":"1","11":"1","12":"1","13":"1","14":"1"},{"1":"Africa","2":"Reunion","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"1","10":"1","11":"1","12":"1","13":"1","14":"1"},{"1":"Africa","2":"Swaziland","3":"0","4":"0","5":"0","6":"0","7":"0","8":"1","9":"1","10":"1","11":"1","12":"1","13":"1","14":"1"},{"1":"Americas","2":"Trinidad and Tobago","3":"1","4":"1","5":"1","6":"1","7":"1","8":"1","9":"1","10":"1","11":"1","12":"1","13":"1","14":"1"},{"1":"Asia-Pacific","2":"Bahrain","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"1","12":"1","13":"1","14":"1"},{"1":"Europe","2":"Montenegro","3":"0","4":"0","5":"0","6":"1","7":"1","8":"1","9":"1","10":"1","11":"1","12":"1","13":"1","14":"1"},{"1":"Africa","2":"Djibouti","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0"},{"1":"Africa","2":"Sao Tome and Principe","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0"},{"1":"Europe","2":"Iceland","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0","13":"0","14":"0"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Now that we have the required columns, let’s plan the row order. We notice that, for each region, we have the top 4 countries (as per 2007) , followed by ‘Others’. Let’s create the <code>top-4</code> dataset.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb5-1">top4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pop_trend <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;">group_by</span>(region) <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;">slice_max</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2007</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n =</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;">with_ties =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <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;">ungroup</span>()</span>
<span id="cb5-5"></span>
<span id="cb5-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(top4)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["country"],"name":[2],"type":["chr"],"align":["left"]},{"label":["1952"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["1957"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["1962"],"name":[5],"type":["dbl"],"align":["right"]},{"label":["1967"],"name":[6],"type":["dbl"],"align":["right"]},{"label":["1972"],"name":[7],"type":["dbl"],"align":["right"]},{"label":["1977"],"name":[8],"type":["dbl"],"align":["right"]},{"label":["1982"],"name":[9],"type":["dbl"],"align":["right"]},{"label":["1987"],"name":[10],"type":["dbl"],"align":["right"]},{"label":["1992"],"name":[11],"type":["dbl"],"align":["right"]},{"label":["1997"],"name":[12],"type":["dbl"],"align":["right"]},{"label":["2002"],"name":[13],"type":["dbl"],"align":["right"]},{"label":["2007"],"name":[14],"type":["dbl"],"align":["right"]}],"data":[{"1":"Africa","2":"Nigeria","3":"33","4":"37","5":"42","6":"47","7":"54","8":"62","9":"73","10":"82","11":"93","12":"106","13":"120","14":"135"},{"1":"Africa","2":"Egypt","3":"22","4":"25","5":"28","6":"32","7":"35","8":"39","9":"46","10":"53","11":"59","12":"66","13":"73","14":"80"},{"1":"Africa","2":"Ethiopia","3":"21","4":"23","5":"25","6":"28","7":"31","8":"35","9":"38","10":"43","11":"52","12":"60","13":"68","14":"77"},{"1":"Africa","2":"Congo, Dem. Rep.","3":"14","4":"16","5":"17","6":"20","7":"23","8":"26","9":"31","10":"35","11":"42","12":"48","13":"55","14":"65"},{"1":"Americas","2":"United States","3":"158","4":"172","5":"187","6":"199","7":"210","8":"220","9":"232","10":"243","11":"257","12":"273","13":"288","14":"301"},{"1":"Americas","2":"Brazil","3":"57","4":"66","5":"76","6":"88","7":"101","8":"114","9":"129","10":"143","11":"156","12":"169","13":"180","14":"190"},{"1":"Americas","2":"Mexico","3":"30","4":"35","5":"41","6":"48","7":"56","8":"64","9":"72","10":"80","11":"88","12":"96","13":"102","14":"109"},{"1":"Americas","2":"Colombia","3":"12","4":"14","5":"17","6":"20","7":"23","8":"25","9":"28","10":"31","11":"34","12":"38","13":"41","14":"44"},{"1":"Asia-Pacific","2":"China","3":"556","4":"637","5":"666","6":"755","7":"862","8":"943","9":"1000","10":"1084","11":"1165","12":"1230","13":"1280","14":"1319"},{"1":"Asia-Pacific","2":"India","3":"372","4":"409","5":"454","6":"506","7":"567","8":"634","9":"708","10":"788","11":"872","12":"959","13":"1034","14":"1110"},{"1":"Asia-Pacific","2":"Indonesia","3":"82","4":"90","5":"99","6":"109","7":"121","8":"137","9":"153","10":"169","11":"185","12":"199","13":"211","14":"224"},{"1":"Asia-Pacific","2":"Pakistan","3":"41","4":"47","5":"53","6":"61","7":"69","8":"78","9":"91","10":"105","11":"120","12":"136","13":"153","14":"169"},{"1":"Europe","2":"Germany","3":"69","4":"71","5":"74","6":"76","7":"79","8":"78","9":"78","10":"78","11":"81","12":"82","13":"82","14":"82"},{"1":"Europe","2":"Turkey","3":"22","4":"26","5":"30","6":"33","7":"37","8":"42","9":"47","10":"53","11":"58","12":"63","13":"67","14":"71"},{"1":"Europe","2":"France","3":"42","4":"44","5":"47","6":"50","7":"52","8":"53","9":"54","10":"56","11":"57","12":"59","13":"60","14":"61"},{"1":"Europe","2":"United Kingdom","3":"50","4":"51","5":"53","6":"55","7":"56","8":"56","9":"56","10":"57","11":"58","12":"59","13":"60","14":"61"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>To create the <code>others</code> dataset, we exclude all countries that are present in the <code>top-4</code>.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb6-1">others <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pop_trend <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;">filter</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!</span>country <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%in%</span> top4<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>country) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(region) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">across</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.cols =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>country, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fns =</span> sum),</span>
<span id="cb6-5">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-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;">country =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Others"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb6-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(region, country, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">everything</span>())</span>
<span id="cb6-8"></span>
<span id="cb6-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(others)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["country"],"name":[2],"type":["chr"],"align":["left"]},{"label":["1952"],"name":[3],"type":["dbl"],"align":["right"]},{"label":["1957"],"name":[4],"type":["dbl"],"align":["right"]},{"label":["1962"],"name":[5],"type":["dbl"],"align":["right"]},{"label":["1967"],"name":[6],"type":["dbl"],"align":["right"]},{"label":["1972"],"name":[7],"type":["dbl"],"align":["right"]},{"label":["1977"],"name":[8],"type":["dbl"],"align":["right"]},{"label":["1982"],"name":[9],"type":["dbl"],"align":["right"]},{"label":["1987"],"name":[10],"type":["dbl"],"align":["right"]},{"label":["1992"],"name":[11],"type":["dbl"],"align":["right"]},{"label":["1997"],"name":[12],"type":["dbl"],"align":["right"]},{"label":["2002"],"name":[13],"type":["dbl"],"align":["right"]},{"label":["2007"],"name":[14],"type":["dbl"],"align":["right"]}],"data":[{"1":"Africa","2":"Others","3":"146","4":"162","5":"183","6":"206","7":"239","8":"270","9":"314","10":"362","11":"409","12":"463","13":"515","14":"570"},{"1":"Americas","2":"Others","3":"88","4":"101","5":"113","6":"127","7":"143","8":"156","9":"167","10":"185","11":"202","12":"224","13":"238","14":"254"},{"1":"Asia-Pacific","2":"Others","3":"354","4":"391","5":"436","6":"493","7":"546","8":"608","9":"672","10":"745","11":"811","12":"882","13":"949","14":"1012"},{"1":"Europe","2":"Others","3":"235","4":"244","5":"257","6":"269","7":"278","8":"289","9":"294","10":"301","11":"303","12":"305","13":"307","14":"310"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>While we create the <code>top-4</code> and <code>others</code> datasets separately, we will combine them later at the very last moment before writing them to the Excel template.</p>
<p>Now that we have the datasets needed for <code>02_chart</code>, let’s proceed to the create <code>02_table</code> . This table gives you the count of countries that fall under various population ranges.</p>
<div style="text-align:center;">
<div id="fig-slide22" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-slide22-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/uf3flVN.png" title="02_table" class="img-fluid figure-img" style="width:60.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-slide22-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: 02_table on Slide 2
</figcaption></figure>
</div>
</div>
<p>Let’s create <code>02_table</code>. To create this table, we first create a new variable called <code>pop_range</code>.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb7-1">pop_levels <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;">'Less than 500K'</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'500K - 1 Million'</span>,</span>
<span id="cb7-2">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'1M - 10 Million'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'10M - 100 Million'</span>,</span>
<span id="cb7-3">                <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'100M - 1 Billion'</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'More than 1 Billion'</span>)</span>
<span id="cb7-4"></span>
<span id="cb7-5">gp2007 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <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;">2007</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-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;">pop_range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">case_when</span>(pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">5E5</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pop_levels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>],</span>
<span id="cb7-8">                               pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E6</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pop_levels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>],</span>
<span id="cb7-9">                               pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E7</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pop_levels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>],</span>
<span id="cb7-10">                               pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E8</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pop_levels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>],</span>
<span id="cb7-11">                               pop <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&lt;</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E9</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pop_levels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>],</span>
<span id="cb7-12">                               <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>      <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> pop_levels[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">6</span>]),</span>
<span id="cb7-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop_range =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(pop_range, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">levels =</span> pop_levels))</span>
<span id="cb7-14"></span>
<span id="cb7-15">pop_groups <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gp2007 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(region, pop_range, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.drop =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"># of Countries</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">n</span>(),</span>
<span id="cb7-18">            <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-19">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(region, pop_range) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-20">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Population Category</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> pop_range)</span>
<span id="cb7-21"></span>
<span id="cb7-22"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(pop_groups)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["Population Category"],"name":[2],"type":["fct"],"align":["left"]},{"label":["# of Countries"],"name":[3],"type":["int"],"align":["right"]}],"data":[{"1":"Africa","2":"Less than 500K","3":"2"},{"1":"Africa","2":"500K - 1 Million","3":"3"},{"1":"Africa","2":"1M - 10 Million","3":"21"},{"1":"Africa","2":"10M - 100 Million","3":"25"},{"1":"Africa","2":"100M - 1 Billion","3":"1"},{"1":"Africa","2":"More than 1 Billion","3":"0"},{"1":"Americas","2":"Less than 500K","3":"0"},{"1":"Americas","2":"500K - 1 Million","3":"0"},{"1":"Americas","2":"1M - 10 Million","3":"13"},{"1":"Americas","2":"10M - 100 Million","3":"9"},{"1":"Americas","2":"100M - 1 Billion","3":"3"},{"1":"Americas","2":"More than 1 Billion","3":"0"},{"1":"Asia-Pacific","2":"Less than 500K","3":"0"},{"1":"Asia-Pacific","2":"500K - 1 Million","3":"1"},{"1":"Asia-Pacific","2":"1M - 10 Million","3":"10"},{"1":"Asia-Pacific","2":"10M - 100 Million","3":"18"},{"1":"Asia-Pacific","2":"100M - 1 Billion","3":"4"},{"1":"Asia-Pacific","2":"More than 1 Billion","3":"2"},{"1":"Europe","2":"Less than 500K","3":"1"},{"1":"Europe","2":"500K - 1 Million","3":"1"},{"1":"Europe","2":"1M - 10 Million","3":"14"},{"1":"Europe","2":"10M - 100 Million","3":"14"},{"1":"Europe","2":"100M - 1 Billion","3":"0"},{"1":"Europe","2":"More than 1 Billion","3":"0"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section><section id="slide-3" class="level3"><h3 class="anchored" data-anchor-id="slide-3">Slide 3</h3>
<p>Slide 3 contains 2 strings and one chart. The data for the chart looks as shown below.</p>
<div style="text-align:center;">
<div id="fig-slide3" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-slide3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/kDAbook.png" title="Slide 3" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-slide3-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: 03_chart table for Slide 3
</figcaption></figure>
</div>
</div>
<p>The data for <code>03_chart</code> is the list of top 10 countries in each region as per latest record i.e.&nbsp;2007. Let’s create the <code>top10</code> table.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb8-1">top10 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gp <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(year <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;">2007</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(region) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">slice_max</span>(pop, <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;">10</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 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;">ungroup</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;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>year) <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;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pop =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(pop<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span><span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E6</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;">%&gt;%</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># population in millions</span></span>
<span id="cb8-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_names</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;">"region"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"country"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"population"</span>))</span>
<span id="cb8-9"></span>
<span id="cb8-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(top10)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["region"],"name":[1],"type":["chr"],"align":["left"]},{"label":["country"],"name":[2],"type":["chr"],"align":["left"]},{"label":["population"],"name":[3],"type":["dbl"],"align":["right"]}],"data":[{"1":"Africa","2":"Nigeria","3":"135.0312"},{"1":"Africa","2":"Egypt","3":"80.2645"},{"1":"Africa","2":"Ethiopia","3":"76.5119"},{"1":"Africa","2":"Congo, Dem. Rep.","3":"64.6068"},{"1":"Africa","2":"South Africa","3":"43.9978"},{"1":"Africa","2":"Sudan","3":"42.2929"},{"1":"Africa","2":"Tanzania","3":"38.1396"},{"1":"Africa","2":"Kenya","3":"35.6102"},{"1":"Africa","2":"Morocco","3":"33.7572"},{"1":"Africa","2":"Algeria","3":"33.3332"},{"1":"Americas","2":"United States","3":"301.1399"},{"1":"Americas","2":"Brazil","3":"190.0106"},{"1":"Americas","2":"Mexico","3":"108.7009"},{"1":"Americas","2":"Colombia","3":"44.2276"},{"1":"Americas","2":"Argentina","3":"40.3019"},{"1":"Americas","2":"Canada","3":"33.3901"},{"1":"Americas","2":"Peru","3":"28.6748"},{"1":"Americas","2":"Venezuela","3":"26.0847"},{"1":"Americas","2":"Chile","3":"16.2847"},{"1":"Americas","2":"Ecuador","3":"13.7557"},{"1":"Asia-Pacific","2":"China","3":"1318.6831"},{"1":"Asia-Pacific","2":"India","3":"1110.3963"},{"1":"Asia-Pacific","2":"Indonesia","3":"223.5470"},{"1":"Asia-Pacific","2":"Pakistan","3":"169.2706"},{"1":"Asia-Pacific","2":"Bangladesh","3":"150.4483"},{"1":"Asia-Pacific","2":"Japan","3":"127.4680"},{"1":"Asia-Pacific","2":"Philippines","3":"91.0773"},{"1":"Asia-Pacific","2":"Vietnam","3":"85.2624"},{"1":"Asia-Pacific","2":"Iran","3":"69.4536"},{"1":"Asia-Pacific","2":"Thailand","3":"65.0681"},{"1":"Europe","2":"Germany","3":"82.4010"},{"1":"Europe","2":"Turkey","3":"71.1586"},{"1":"Europe","2":"France","3":"61.0839"},{"1":"Europe","2":"United Kingdom","3":"60.7762"},{"1":"Europe","2":"Italy","3":"58.1477"},{"1":"Europe","2":"Spain","3":"40.4482"},{"1":"Europe","2":"Poland","3":"38.5182"},{"1":"Europe","2":"Romania","3":"22.2761"},{"1":"Europe","2":"Netherlands","3":"16.5706"},{"1":"Europe","2":"Greece","3":"10.7063"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section></section><section id="the-for-loop" class="level2 page-columns page-full"><h2 class="anchored" data-anchor-id="the-for-loop">The <code>for</code> loop!</h2>
<p>We now have to load the Excel template with the data at appropriate cell locations for one region at a time. Since we have about 4 regions, we will create a vector of unique regions to iterate over.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb9-1">unique_regions <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> gp <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;">distinct</span>(region) <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>
<span id="cb9-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">cat</span>(unique_regions, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</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></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>Asia-Pacific
Europe
Africa
Americas</code></pre>
</div>
</div>
<p>As our last step, we will create the <code>for</code> loop that will iterate over <code>unique_regions</code> , filter the datasets for each region, write them to the Excel Template, save the template with temporary name. We save the file with different name to prevent unintentionally corrupting the Excel macro template. Finally, we run the macro in the renamed file.</p>
<p>The code will look something like this</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (region <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> unique_regions) {</span>
<span id="cb11-2">  </span>
<span id="cb11-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: filter the data sets</span></span>
<span id="cb11-4">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: write the data sets</span></span>
<span id="cb11-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: save the excel template with different name</span></span>
<span id="cb11-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: load the renamed Excel file</span></span>
<span id="cb11-7">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 5: run macro</span></span>
<span id="cb11-8">}</span></code></pre></div>
</details>
</div>
<p>Let’s populate the above <code>for</code> loop with the code we need.</p>
<div class="cell column-body-outset-right">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb12-1"><span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">for</span> (curr_region <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">in</span> unique_regions) {</span>
<span id="cb12-2">  </span>
<span id="cb12-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 1: filter the data sets</span></span>
<span id="cb12-4">  </span>
<span id="cb12-5">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slide 1</span></span>
<span id="cb12-6">  S1_title <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>(curr_region, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population"</span>)</span>
<span id="cb12-7">  S1_subtitle <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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Vishal Katti"</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;">format</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.Date</span>(),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %d, %Y"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sep =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"   "</span>)</span>
<span id="cb12-8">  </span>
<span id="cb12-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slide 2</span></span>
<span id="cb12-10">  S2_title <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>(curr_region, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Population since 1952"</span>)</span>
<span id="cb12-11">  </span>
<span id="cb12-12">  S2_top4 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> top4        <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>(region <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;">all_of</span>(curr_region)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>region) <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;">arrange</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">2007</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">`</span>))</span>
<span id="cb12-13">  S2_others <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> others    <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>(region <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;">all_of</span>(curr_region)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>region)</span>
<span id="cb12-14">  S2_top5 <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>(S2_top4, S2_others)</span>
<span id="cb12-15">  </span>
<span id="cb12-16">  S2_table <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> pop_groups <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>(region <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;">all_of</span>(curr_region)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>region)</span>
<span id="cb12-17">  </span>
<span id="cb12-18">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Slide 3</span></span>
<span id="cb12-19">  S3_title <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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Top 10 most populated countries in"</span>, curr_region)</span>
<span id="cb12-20">  </span>
<span id="cb12-21">  S3_chart <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> top10      <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>(region <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;">all_of</span>(curr_region)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>region)</span>
<span id="cb12-22">  </span>
<span id="cb12-23">  S3_factoid <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>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"The population of"</span>, S3_chart<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>country[<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;">"is approx."</span>,</span>
<span id="cb12-24">                      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">round</span>(S3_chart<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>population[<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>S3_chart<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>population[<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>
<span id="cb12-25">                      <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"times that of"</span>, S3_chart<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>country[<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>])</span>
<span id="cb12-26">  </span>
<span id="cb12-27">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 2: write the data sets</span></span>
<span id="cb12-28">  </span>
<span id="cb12-29">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Load the template</span></span>
<span id="cb12-30">  wb <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;">loadWorkbook</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path/to/template/XL2PPT.xlsm"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># relative to this R script</span></span>
<span id="cb12-31">  sht <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;">"Sheet1"</span></span>
<span id="cb12-32">  </span>
<span id="cb12-33">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># write data to coordinate (col, row)</span></span>
<span id="cb12-34">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S1_title,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</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;">3</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-35">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S1_subtitle, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-36">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S2_title,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</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;">7</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-37">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S2_top5,     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</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;">9</span>),  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S2_table,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</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;">18</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;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-39">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S3_title,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</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;">18</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S3_factoid,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</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;">19</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb12-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">writeData</span>(wb, sht, S3_chart,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">xy =</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;">21</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">colNames =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-42">  </span>
<span id="cb12-43">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 3: save the excel template with different name</span></span>
<span id="cb12-44">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">saveWorkbook</span>(wb, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path/to/template/XL2PPT_edited.xlsm"</span>, <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="cb12-45">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gc</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-46">  <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;">2</span>)</span>
<span id="cb12-47">  </span>
<span id="cb12-48">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 4: load the renamed Excel file</span></span>
<span id="cb12-49">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create Excel Application</span></span>
<span id="cb12-50">  xlApp <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;">"Excel.Application"</span>)</span>
<span id="cb12-51"></span>
<span id="cb12-52">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Open the Macro Excel book</span></span>
<span id="cb12-53">  xlWbk <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> xlApp<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;">Workbooks</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;">Open</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">normalizePath</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"path/to/template/XL2PPT_edited.xlsm"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">winslash =</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;"># Change to your directory</span></span>
<span id="cb12-54">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># its ok to run macro without visible excel application</span></span>
<span id="cb12-55">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># If you want to see your workbook, please set it to TRUE</span></span>
<span id="cb12-56">  xlApp[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Visible"</span>]] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span></span>
<span id="cb12-57">  </span>
<span id="cb12-58">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Step 5: run macro</span></span>
<span id="cb12-59">  xlApp<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;">Run</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Create_Continental_Deck"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Name of Macro to run</span></span>
<span id="cb12-60"></span>
<span id="cb12-61">  xlWbk<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;">Close</span>(<span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># save and close excel book</span></span>
<span id="cb12-62">  xlApp<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;">Quit</span>()</span>
<span id="cb12-63">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">gc</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">verbose =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span>
<span id="cb12-64">  <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;">2</span>)</span>
<span id="cb12-65">}</span></code></pre></div>
</details>
</div>
<p>Once the code runs completely, you will see 4 new PowerPoint Presentations in your working folder.</p>
<div style="text-align:center;">
<div id="fig-output-files" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-output-files-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/Pe8yyxI.png" title="Output Files" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-output-files-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Output Files
</figcaption></figure>
</div>
</div>
<p>You can download the full R script from <a href="https://gist.github.com/vkatti/cc95c56e11c1b8985bbd288f353c7386" title="Navigate to Github Gist">here</a>.</p>
</section><section id="references-citations" class="level2"><h2 class="anchored" data-anchor-id="references-citations">References &amp; Citations</h2>
<ul>
<li><p>Jennifer Bryan (2017). gapminder: Data from Gapminder. R package version 0.3.0. <a href="https://CRAN.R-project.org/package=gapminder" class="uri">https://CRAN.R-project.org/package=gapminder</a></p></li>
<li><p>Hadley Wickham, Romain Francois, Lionel Henry and Kirill Muller (2021). dplyr: A Grammar of Data Manipulation. R package version 1.0.7. <a href="https://CRAN.R-project.org/package=dplyr" class="uri">https://CRAN.R-project.org/package=dplyr</a></p></li>
<li><p>Hadley Wickham (2021). tidyr: Tidy Messy Data. R package version 1.1.3. <a href="https://CRAN.R-project.org/package=tidyr" class="uri">https://CRAN.R-project.org/package=tidyr</a></p></li>
<li><p>Greg Lin (2020). reactable: Interactive Data Tables Based on ‘React Table’. R package version 0.2.3. <a href="https://CRAN.R-project.org/package=reactable" class="uri">https://CRAN.R-project.org/package=reactable</a></p></li>
<li><p>Philipp Schauberger and Alexander Walker (2021). openxlsx: Read, Write and Edit xlsx Files. R package version 4.2.4. <a href="https://CRAN.R-project.org/package=openxlsx" class="uri">https://CRAN.R-project.org/package=openxlsx</a></p></li>
<li><p>Duncan Temple Lang (NA). RDCOMClient: R-DCOM client. <a href="http://www.omegahat.net/RDCOMClient," class="uri">http://www.omegahat.net/RDCOMClient,</a> <a href="http://www.omegahat.net" class="uri">http://www.omegahat.net</a> <a href="http://www.omegahat.net/bugs." class="uri">http://www.omegahat.net/bugs.</a></p></li>
<li><p><a href="https://docs.microsoft.com/en-us/office/vba/api/overview/excel" class="uri">https://docs.microsoft.com/en-us/office/vba/api/overview/excel</a></p></li>
<li><p><a href="https://docs.microsoft.com/en-us/office/vba/api/overview/powerpoint" class="uri">https://docs.microsoft.com/en-us/office/vba/api/overview/powerpoint</a></p></li>
</ul>


</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2021,
  author = {Katti, Vishal},
  title = {Unholy {Trinity} of {R,} {Excel} {VBA} and {Powerpoint} -
    {Part} 2 of 2},
  date = {2021-12-29},
  url = {https://vishalkatti.com/posts/R2VBA2PPT2/},
  langid = {en},
  abstract = {This post demonstrates how to run VBA macros in Excel
    which in turn creates Presentations based off PowerPoint Templates.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2021" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2021. <span>“Unholy Trinity of R, Excel VBA and
Powerpoint - Part 2 of 2.”</span> December 29, 2021. <a href="https://vishalkatti.com/posts/R2VBA2PPT2/">https://vishalkatti.com/posts/R2VBA2PPT2/</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>Excel</category>
  <category>VBA</category>
  <category>PowerPoint</category>
  <category>openxlsx</category>
  <category>RDCOMClient</category>
  <guid>https://vishalkatti.com/posts/R2VBA2PPT2/</guid>
  <pubDate>Tue, 28 Dec 2021 18:30:00 GMT</pubDate>
  <media:content url="https://vishalkatti.com/posts/R2VBA2PPT2/images/R2XL2PPT_logo.png" medium="image" type="image/png" height="59" width="144"/>
</item>
<item>
  <title>Unholy Trinity of R, Excel VBA and Powerpoint - Part 1 of 2</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/R2VBA2PPT1/</link>
  <description><![CDATA[ <p>One of the most common tasks in most offices, is creating presentations and reports in Microsoft PowerPoint. While the tool is great for creating ad-hoc presentations, editing the same with new data on a periodic basis gets tedious. Now, I know that some wonderful packages like <code>officer</code> and <code>officedown</code> exist that enable us to create PowerPoint presentations with editable charts from R itself. You can read all about this in the amazing <a href="https://alison.rbind.io/blog/2021-07-officedown/">Alison Hill’s blog post “Up and running with officedown”</a>.</p>
<p>Since I discovered R while looking for a better alternative to VBA for data analysis and Excel/PowerPoint automation, the following is an alternative workflow to create multiple PowerPoint presentations using a combination of these technologies. Note that this workflow uses the <code>RDCOMClient</code> package which works in Windows environment only.</p>
<section id="tldr" class="level2"><h2 class="anchored" data-anchor-id="tldr">TL;DR</h2>
<p>In this 2-part blog, we create a PowerPoint template with named placeholders which we populate from an Excel file using VBA. The Excel file is loaded with data using R with the help of <code>openxlsx</code> package and then the macro is triggered using the <code>RDCOMClient</code> package.</p>
<p>This solution has great potential to give you the same feeling as those Jurassic Park scientists that Dr.&nbsp;Ian Malcolm remarked about!</p>
<p>Advantages of this approach over <code>officer</code> and <code>officedown</code>:</p>
<ol type="1">
<li>Slide/content/header/footer formatting control is in the PowerPoint template rather than R code.</li>
<li>All charts are native and can contain any feature (dual axis, mixed data series like bar + line, line + points). All Excel chart-types are available. Go wild!</li>
<li>You can use any PowerPoint template design (Yes, even your sad/weird/exciting corporate template!).</li>
</ol>
<p>Let’s begin.</p>
<p>Suppose we want to automate the following PowerPoint presentation. It contains 3 slides with a title slide and 2 content slides having graphs and tables created from the <code>gapminder</code> dataset. This <code>.pptx</code> file also has a custom footer.</p>
<div style="text-align:center;">
<div id="fig-gapminder" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-gapminder-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/LL6UwWH.gif" title="The Gapminder World Report" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-gapminder-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: The Gapminder World Population Report. Note the custom footer!
</figcaption></figure>
</div>
</div>
<p>We want to create the same presentation with same structure but at a continent-level. <code>gapminder</code> has data for 5 continents and we wish to create 5 presentations by the end of this.</p>
</section><section id="the-powerpoint-template" class="level2"><h2 class="anchored" data-anchor-id="the-powerpoint-template">The PowerPoint Template</h2>
<p>In this approach, we start with the PowerPoint presentation file. We will create a template with placeholders and charts with dummy data.</p>
<div style="text-align:center;">
<div id="fig-potx" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-potx-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/NvdILBA.png" title="Save As PowerPoint Template (.potx)" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-potx-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: Save As PowerPoint Template (.potx)
</figcaption></figure>
</div>
</div>
<p>Usually, you would have a copy of the <code>.pptx</code> file you want to automate. Save a copy of it as a <code>PowerPoint Template (.potx)</code>, ideally to your R Project<sup>1</sup> folder. In my case, I’ve created a new R Project folder named <code>R2XL2PPT</code> as shown in Figure&nbsp;2.</p>
<p>Now let us prep the template. If you open the template file by double-click or right-click &gt; New, it would open a fresh <code>.pptx</code> presentation using the template. Right-Click and click Open in the context menu to open the <code>.potx</code> template file for editing.</p>
<div style="text-align:center;">
<div id="fig-open" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-open-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/eQokXoX.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-open-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;3: CORRECT: Right-Click &gt; Open
</figcaption></figure>
</div>
</div>
<p>Once you have the template open, we will add names to all the text placeholders, tables and graphs we wish to update. To update the placeholder name:</p>
<ol type="1">
<li>Select the shape/text-area/table/graph.</li>
<li>From Shape Format, click Selection Pane.</li>
<li>In the Selection Pane, change the name of the selected item.</li>
</ol>
<div style="text-align:center;">
<div id="fig-add-shapes" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-add-shapes-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/xhA0D3u.png" title="Add shape names from Selection Pane" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-add-shapes-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;4: Add shape names from Selection Pane
</figcaption></figure>
</div>
</div>
<div class="callout callout-style-default callout-tip callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Advisory
</div>
</div>
<div class="callout-body-container callout-body">
<p>We use the format <code>NN_[Position]Object</code> where <code>NN</code> is the slide number, <code>[Position]</code> is the either TopLeft, TopRight, BottomLeft, BottomRight or any other position and finally, <code>Object</code> is either Table, Chart, Title, Subtitle, TextBox etc. You can use any fancy identifier here, just make sure that your future self and others can recognise them easily.</p>
</div>
</div>
<p>Once you set the names of all the items that you want to customise, save the template.</p>
<p>Download the <code>GP_template.potx</code> template <a href="content/GP_template.potx">here</a>.</p>
</section><section id="the-excel-template" class="level2"><h2 class="anchored" data-anchor-id="the-excel-template">The Excel Template</h2>
<div style="text-align:center;">
<div id="fig-mapping" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-mapping-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/A080o1Z.png" title="PowerPoint item to Excel named range mapping" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-mapping-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;5: PowerPoint item to Excel named range mapping
</figcaption></figure>
</div>
</div>
<p>To populate all the named items in the PowerPoint template, we will now create an Excel document which looks identical to the template with respect to the named items. Please see Figure&nbsp;5.</p>
<p>For every named item, depending on whether it is a textbox or chart or table, we will create a named range for that item. For example, for item <code>01_title</code> in the PowerPoint template, we create a <code>S1_title</code> named range<sup>2</sup> (which points to cell C3) as a placeholder for it.</p>
<div class="callout callout-style-default callout-warning callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Warning
</div>
</div>
<div class="callout-body-container callout-body">
<p>Excel does not allow the names of the named ranges to start with a number, hence <code>01_title</code> is mapped to <code>S1_title</code>. The S stands for Slide. Just one of those Excel quirks I guess!</p>
</div>
</div>
<p>You can set a single Excel cell as named range for each <strong>textbox</strong> in the PowerPoint template. You can copy-paste <strong>tables</strong> from Powerpoint to the Excel template directly. The entire table must be set as a named range.</p>
<div style="text-align:center;">
<div id="fig-charts" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-charts-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/rVsb1a6.png" title="Excel Chart: Right Click > Edit Data" class="img-fluid figure-img" style="width:70.0%">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-charts-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;6: Excel Chart: Right Click &gt; Edit Data
</figcaption></figure>
</div>
</div>
<p>For charts, right-click the chart in PowerPoint and select <code>Edit Data</code>. See Figure&nbsp;6. An excel worksheet is displayed with the underlying data. Copy-paste the entire data into the Excel template.</p>
<div style="text-align:center;">
<div id="fig-xlsm" class="quarto-float quarto-figure quarto-figure-center anchored">
<figure class="quarto-float quarto-float-fig figure"><div aria-describedby="fig-xlsm-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://i.imgur.com/oMdSUvp.png" title="XL2PPT design" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-xlsm-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;7: XL2PPT design
</figcaption></figure>
</div>
</div>
<p>For the <code>GP_template.potx</code>, the corresponding excel template <code>XL2PPT.xlsm</code> is shown in Figure&nbsp;7. Please note that this template does not have the VBA macro yet.</p>
</section><section id="the-vba-macro" class="level2"><h2 class="anchored" data-anchor-id="the-vba-macro">The VBA Macro</h2>
<p>We want the VBA macro to:</p>
<ol type="1">
<li>Open a new instance of PowerPoint presentation using the <code>GP_template.potx</code> file.</li>
<li>Copy text/numbers from various placeholders and replace existing text/numbers in the PowerPoint presentation.</li>
<li>Save the presentation with custom file name with <code>.pptx</code> extension.</li>
</ol>
<p>The actual mapping of the named ranges in the Excel template to the named shapes in the PowerPoint template happens in the VBA code. However, at this stage, you can actually create the PowerPoint presentation by copying the numbers into the Excel template and hitting the big <code>RUN MACRO</code> button.</p>
<p>For your reference, I am embedding the VBA macro code below. You can download the <code>XL2PPT.xlsm</code> file from <a href="content/XL2PPT.xlsm">here</a>.</p>
<div class="cell">
<div class="cell-output-display">
<details closed=""><summary><span title="Click to Expand"> VBA Macro Code </span>
</summary><script src="https://gist.github.com/vkatti/6b62c49c7dac8901b0194a758a77c0df.js"></script></details><p><br></p>
</div>
</div>
</section><section id="r" class="level2"><h2 class="anchored" data-anchor-id="r">R</h2>
<p>Suppose we want to create a 10 variations of PowerPoint presentation using the same template. While creating the presentation from Excel is now automated, how about creating the numbers for each of those 10 variations? This is where we bring in R with the <code>openxlsx</code> and <code>RDCOMClient</code> packages. We use the <code>tidyverse</code> set of packages to read in data, clean and massage the data into the various formats we need, <code>openxlsx</code> to write the data (single numbers, text or tables of numbers) to the Excel template and <code>RDCOMClient</code> to run the embedded VBA code in the Excel template.</p>
<p>Check out <a href="https://vishalkatti.com/posts/R2VBA2PPT2/">Part 2</a> of this blog to see how to run VBA code using R. Let me know if you find this useful or any corrections required in the comments below.</p>


</section><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p><strong>What is an R Project?</strong><br><em>https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects</em>↩︎</p></li>
<li id="fn2"><p><strong>What is a Named Range in Excel?</strong> <em>https://support.microsoft.com/en-us/office/define-and-use-names-in-formulas-4d0f13ac-53b7-422e-afd2-abd7ff379c64</em>↩︎</p></li>
</ol></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2021,
  author = {Katti, Vishal},
  title = {Unholy {Trinity} of {R,} {Excel} {VBA} and {Powerpoint} -
    {Part} 1 of 2},
  date = {2021-10-19},
  url = {https://vishalkatti.com/posts/R2VBA2PPT1/},
  langid = {en},
  abstract = {This post demonstrates how to create a PowerPoint template
    based off your custom/corporate Presentation/Report and VBA-enabled
    Excel file that would populate the report.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2021" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2021. <span>“Unholy Trinity of R, Excel VBA and
Powerpoint - Part 1 of 2.”</span> October 19, 2021. <a href="https://vishalkatti.com/posts/R2VBA2PPT1/">https://vishalkatti.com/posts/R2VBA2PPT1/</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>Excel</category>
  <category>VBA</category>
  <category>PowerPoint</category>
  <guid>https://vishalkatti.com/posts/R2VBA2PPT1/</guid>
  <pubDate>Mon, 18 Oct 2021 18:30:00 GMT</pubDate>
  <media:content url="https://vishalkatti.com/posts/R2VBA2PPT1/images/R2XL2PPT_logo.png" medium="image" type="image/png" height="59" width="144"/>
</item>
<item>
  <title>Interactive Drill-down Tables using {reactable}</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/drilldown-with-reactable/</link>
  <description><![CDATA[ <p>We often come across denormalized data that has 2 or more levels of information. For example, top-level info like course info with data fields like course id, course name, description, start/end date and second-level info like student info with data fields like with student id, student name, age and gender. We may also have these two groups of data as separate tables with a primary-key foreign-key design, usually from a well-designed SQL database.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>If you are reading this article from anywhere except https://vishalkatti.com, you may not see some of the interactive elements below. Go to <a href="https://www.vishalkatti.com/posts/drilldown-with-reactable/">original post here</a></p>
</div>
</div>
<p>Let us create some data.</p>
<section id="top-level-data-course" class="level2"><h2 class="anchored" data-anchor-id="top-level-data-course">Top-Level data: <strong>course</strong>
</h2>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(dplyr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quietly =</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;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(rmarkdown, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quietly =</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;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb1-3"></span>
<span id="cb1-4">course <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_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;">4</span>,</span>
<span id="cb1-5">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_name =</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;">"Course"</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;">4</span>]),</span>
<span id="cb1-6">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_date  =</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> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"2021-01-01"</span>), <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">length.out =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">4</span>),</span>
<span id="cb1-7">                 <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_date    =</span> lubridate<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;">ceiling_date</span>(start_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">unit =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</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="cb1-8"></span>
<span id="cb1-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(course)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["course_id"],"name":[1],"type":["int"],"align":["right"]},{"label":["course_name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["start_date"],"name":[3],"type":["date"],"align":["right"]},{"label":["end_date"],"name":[4],"type":["date"],"align":["right"]}],"data":[{"1":"1","2":"Course A","3":"2021-01-01","4":"2021-01-31"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28"},{"1":"3","2":"Course C","3":"2021-03-01","4":"2021-03-31"},{"1":"4","2":"Course D","3":"2021-04-01","4":"2021-04-30"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section><section id="second-level-data-student" class="level2"><h2 class="anchored" data-anchor-id="second-level-data-student">Second Level Data: <strong>student</strong>
</h2>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb2-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;">42</span>)</span>
<span id="cb2-2">student <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 class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_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;">20</span>,</span>
<span id="cb2-3">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_name    =</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;">"Student"</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;">20</span>]),</span>
<span id="cb2-4">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender    =</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;">"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 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;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb2-5">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age       =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</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;">35</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;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb2-6">                  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sample</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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">20</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replace =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>))</span>
<span id="cb2-7"></span>
<span id="cb2-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(student)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["s_id"],"name":[1],"type":["int"],"align":["right"]},{"label":["s_name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["gender"],"name":[3],"type":["chr"],"align":["left"]},{"label":["age"],"name":[4],"type":["int"],"align":["right"]},{"label":["course_id"],"name":[5],"type":["int"],"align":["right"]}],"data":[{"1":"1","2":"Student A","3":"X","4":"21","5":"3"},{"1":"2","2":"Student B","3":"X","4":"22","5":"4"},{"1":"3","2":"Student C","3":"X","4":"30","5":"3"},{"1":"4","2":"Student D","3":"X","4":"22","5":"1"},{"1":"5","2":"Student E","3":"Y","4":"19","5":"1"},{"1":"6","2":"Student F","3":"Y","4":"25","5":"2"},{"1":"7","2":"Student G","3":"Y","4":"20","5":"2"},{"1":"8","2":"Student H","3":"X","4":"18","5":"2"},{"1":"9","2":"Student I","3":"Z","4":"27","5":"2"},{"1":"10","2":"Student J","3":"Z","4":"28","5":"2"},{"1":"11","2":"Student K","3":"X","4":"32","5":"4"},{"1":"12","2":"Student L","3":"X","4":"25","5":"3"},{"1":"13","2":"Student M","3":"Y","4":"21","5":"2"},{"1":"14","2":"Student N","3":"Y","4":"21","5":"1"},{"1":"15","2":"Student O","3":"Y","4":"35","5":"2"},{"1":"16","2":"Student P","3":"Z","4":"30","5":"3"},{"1":"17","2":"Student Q","3":"Z","4":"22","5":"2"},{"1":"18","2":"Student R","3":"X","4":"21","5":"2"},{"1":"19","2":"Student S","3":"X","4":"19","5":"2"},{"1":"20","2":"Student T","3":"Z","4":"35","5":"4"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>If we are sourcing data from a database, it is probable that you would see these 2 levels of data in 2 separate tables/views, but most business users are comfortable with MS Excel and want <strong>all the data in one sheet</strong>!!</p>
<p>So the data actually looks something like this.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb3-1">combined_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;">left_join</span>(course, student, <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;">"course_id"</span>)</span>
<span id="cb3-2"></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(combined_df)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["course_id"],"name":[1],"type":["int"],"align":["right"]},{"label":["course_name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["start_date"],"name":[3],"type":["date"],"align":["right"]},{"label":["end_date"],"name":[4],"type":["date"],"align":["right"]},{"label":["s_id"],"name":[5],"type":["int"],"align":["right"]},{"label":["s_name"],"name":[6],"type":["chr"],"align":["left"]},{"label":["gender"],"name":[7],"type":["chr"],"align":["left"]},{"label":["age"],"name":[8],"type":["int"],"align":["right"]}],"data":[{"1":"1","2":"Course A","3":"2021-01-01","4":"2021-01-31","5":"4","6":"Student D","7":"X","8":"22"},{"1":"1","2":"Course A","3":"2021-01-01","4":"2021-01-31","5":"5","6":"Student E","7":"Y","8":"19"},{"1":"1","2":"Course A","3":"2021-01-01","4":"2021-01-31","5":"14","6":"Student N","7":"Y","8":"21"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"6","6":"Student F","7":"Y","8":"25"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"7","6":"Student G","7":"Y","8":"20"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"8","6":"Student H","7":"X","8":"18"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"9","6":"Student I","7":"Z","8":"27"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"10","6":"Student J","7":"Z","8":"28"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"13","6":"Student M","7":"Y","8":"21"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"15","6":"Student O","7":"Y","8":"35"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"17","6":"Student Q","7":"Z","8":"22"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"18","6":"Student R","7":"X","8":"21"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"19","6":"Student S","7":"X","8":"19"},{"1":"3","2":"Course C","3":"2021-03-01","4":"2021-03-31","5":"1","6":"Student A","7":"X","8":"21"},{"1":"3","2":"Course C","3":"2021-03-01","4":"2021-03-31","5":"3","6":"Student C","7":"X","8":"30"},{"1":"3","2":"Course C","3":"2021-03-01","4":"2021-03-31","5":"12","6":"Student L","7":"X","8":"25"},{"1":"3","2":"Course C","3":"2021-03-01","4":"2021-03-31","5":"16","6":"Student P","7":"Z","8":"30"},{"1":"4","2":"Course D","3":"2021-04-01","4":"2021-04-30","5":"2","6":"Student B","7":"X","8":"22"},{"1":"4","2":"Course D","3":"2021-04-01","4":"2021-04-30","5":"11","6":"Student K","7":"X","8":"32"},{"1":"4","2":"Course D","3":"2021-04-01","4":"2021-04-30","5":"20","6":"Student T","7":"Z","8":"35"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<p>Displaying such data in a table causes all the top-level data fields to repeat for every second-level record. You can see that <code>course_id</code>, <code>course_name</code>, <code>start_date</code> and <code>end_date</code> columns repeat for all students who enrolled in the same course. Take a moment to think about how would you display such data in an interactive table in a web page, HTML report or Shiny app.</p>
<p>It is advisable to split such denormalized data into normalized data i.e.&nbsp;create the original top-level and second level tables from the <code>combined_df</code>.</p>
</section><section id="vanilla-reactable" class="level2"><h2 class="anchored" data-anchor-id="vanilla-reactable">Vanilla <code>reactable</code>
</h2>
<p>One of my favorite R packages is <a href="https://glin.github.io/reactable/">reactable</a>. The default output creates a neat interactive table with pagination (if data has more than 10 rows) and ability to sort columns.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines 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>(reactable, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">quietly =</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;">warn.conflicts =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)</span>
<span id="cb4-2"></span>
<span id="cb4-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> combined_df)</span></code></pre></div>
</details><div class="cell-output-display">
<div id="htmlwidget-0420b066e36ce9489c4b" class="reactable html-widget" style="width:auto;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-0420b066e36ce9489c4b">{"x":{"tag":{"name":"Reactable","attribs":{"data":{"course_id":[1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4],"course_name":["Course A","Course A","Course A","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course C","Course C","Course C","Course C","Course D","Course D","Course D"],"start_date":["2021-01-01","2021-01-01","2021-01-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-03-01","2021-03-01","2021-03-01","2021-03-01","2021-04-01","2021-04-01","2021-04-01"],"end_date":["2021-01-31","2021-01-31","2021-01-31","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-03-31","2021-03-31","2021-03-31","2021-03-31","2021-04-30","2021-04-30","2021-04-30"],"s_id":[4,5,14,6,7,8,9,10,13,15,17,18,19,1,3,12,16,2,11,20],"s_name":["Student D","Student E","Student N","Student F","Student G","Student H","Student I","Student J","Student M","Student O","Student Q","Student R","Student S","Student A","Student C","Student L","Student P","Student B","Student K","Student T"],"gender":["X","Y","Y","Y","Y","X","Z","Z","Y","Y","Z","X","X","X","X","X","Z","X","X","Z"],"age":[22,19,21,25,20,18,27,28,21,35,22,21,19,21,30,25,30,22,32,35]},"columns":[{"accessor":"course_id","name":"course_id","type":"numeric"},{"accessor":"course_name","name":"course_name","type":"character"},{"accessor":"start_date","name":"start_date","type":"Date"},{"accessor":"end_date","name":"end_date","type":"Date"},{"accessor":"s_id","name":"s_id","type":"numeric"},{"accessor":"s_name","name":"s_name","type":"character"},{"accessor":"gender","name":"gender","type":"character"},{"accessor":"age","name":"age","type":"numeric"}],"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"dataKey":"413c1f3c101a174f7070712d78c69feb"},"children":[]},"class":"reactR_markup"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</section><section id="basic-formatting" class="level2"><h2 class="anchored" data-anchor-id="basic-formatting">Basic Formatting</h2>
<p>With some additional tweaks, we can make it look better.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactable</span>(</span>
<span id="cb5-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data       =</span> combined_df,</span>
<span id="cb5-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compact    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for minimum row height</span></span>
<span id="cb5-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filterable =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for individual column filters</span></span>
<span id="cb5-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">striped    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># banded rows</span></span>
<span id="cb5-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resizable  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for resizable column widths</span></span>
<span id="cb5-7">  <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;">list</span>( <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># define custom header name, width, alignment etc.</span></span>
<span id="cb5-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_id   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CID"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb5-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Course Name"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</span>),</span>
<span id="cb5-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_date  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Start Date"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb5-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_date    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"End Date"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb5-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_id        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SID"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb5-13">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_name      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student Name"</span>),</span>
<span id="cb5-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gender"</span>,      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb5-15">    <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;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb5-16">  )</span>
<span id="cb5-17">)</span></code></pre></div>
</details><div class="cell-output-display">
<div id="htmlwidget-d308ea22dc0f64dbf7ed" class="reactable html-widget" style="width:auto;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-d308ea22dc0f64dbf7ed">{"x":{"tag":{"name":"Reactable","attribs":{"data":{"course_id":[1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4],"course_name":["Course A","Course A","Course A","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course C","Course C","Course C","Course C","Course D","Course D","Course D"],"start_date":["2021-01-01","2021-01-01","2021-01-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-03-01","2021-03-01","2021-03-01","2021-03-01","2021-04-01","2021-04-01","2021-04-01"],"end_date":["2021-01-31","2021-01-31","2021-01-31","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-03-31","2021-03-31","2021-03-31","2021-03-31","2021-04-30","2021-04-30","2021-04-30"],"s_id":[4,5,14,6,7,8,9,10,13,15,17,18,19,1,3,12,16,2,11,20],"s_name":["Student D","Student E","Student N","Student F","Student G","Student H","Student I","Student J","Student M","Student O","Student Q","Student R","Student S","Student A","Student C","Student L","Student P","Student B","Student K","Student T"],"gender":["X","Y","Y","Y","Y","X","Z","Z","Y","Y","Z","X","X","X","X","X","Z","X","X","Z"],"age":[22,19,21,25,20,18,27,28,21,35,22,21,19,21,30,25,30,22,32,35]},"columns":[{"accessor":"course_id","name":"CID","type":"numeric","width":50,"align":"center"},{"accessor":"course_name","name":"Course Name","type":"character","width":140},{"accessor":"start_date","name":"Start Date","type":"Date","width":120,"align":"center"},{"accessor":"end_date","name":"End Date","type":"Date","width":120,"align":"center"},{"accessor":"s_id","name":"SID","type":"numeric","width":70,"align":"center"},{"accessor":"s_name","name":"Student Name","type":"character"},{"accessor":"gender","name":"Gender","type":"character","width":80,"align":"center"},{"accessor":"age","name":"Age","type":"numeric","width":50}],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"striped":true,"compact":true,"dataKey":"37ac57f783425a81c3f289334adfb3a2"},"children":[]},"class":"reactR_markup"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>However, the problem of repeating top-level fields still persists.</p>
</section><section id="grouping-and-aggregating" class="level2"><h2 class="anchored" data-anchor-id="grouping-and-aggregating">Grouping and Aggregating</h2>
<p><a href="https://glin.github.io/reactable/">reactable</a> has a <strong>groupBy</strong> argument which lets us combined rows with common data fields and the <strong>aggregate</strong> argument inside <code>colDef</code> lets us define what aggregation to be used for each column of the top-level data.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactable</span>(</span>
<span id="cb6-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data       =</span> combined_df,</span>
<span id="cb6-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compact    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for minimum row height</span></span>
<span id="cb6-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filterable =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for individual column filters</span></span>
<span id="cb6-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">striped    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># banded rows</span></span>
<span id="cb6-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resizable  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for resizable column widths</span></span>
<span id="cb6-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groupBy    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"course_id"</span>,</span>
<span id="cb6-8">  <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;">list</span>(</span>
<span id="cb6-9">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># show count of students in each course</span></span>
<span id="cb6-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_id   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CID"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>),  </span>
<span id="cb6-11">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># show unique course name</span></span>
<span id="cb6-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Course Name"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</span>,                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>), </span>
<span id="cb6-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># show unique start date</span></span>
<span id="cb6-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_date  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Start Date"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>), </span>
<span id="cb6-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># show unique end date</span></span>
<span id="cb6-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_date    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"End Date"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>), </span>
<span id="cb6-17">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_id        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SID"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb6-18">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_name      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student Name"</span>),</span>
<span id="cb6-19">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gender"</span>,      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb6-20">    <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;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb6-21">  )</span>
<span id="cb6-22">)</span></code></pre></div>
</details><div class="cell-output-display">
<div id="htmlwidget-44db758151977091446d" class="reactable html-widget" style="width:auto;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-44db758151977091446d">{"x":{"tag":{"name":"Reactable","attribs":{"data":{"course_id":[1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4],"course_name":["Course A","Course A","Course A","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course C","Course C","Course C","Course C","Course D","Course D","Course D"],"start_date":["2021-01-01","2021-01-01","2021-01-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-03-01","2021-03-01","2021-03-01","2021-03-01","2021-04-01","2021-04-01","2021-04-01"],"end_date":["2021-01-31","2021-01-31","2021-01-31","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-03-31","2021-03-31","2021-03-31","2021-03-31","2021-04-30","2021-04-30","2021-04-30"],"s_id":[4,5,14,6,7,8,9,10,13,15,17,18,19,1,3,12,16,2,11,20],"s_name":["Student D","Student E","Student N","Student F","Student G","Student H","Student I","Student J","Student M","Student O","Student Q","Student R","Student S","Student A","Student C","Student L","Student P","Student B","Student K","Student T"],"gender":["X","Y","Y","Y","Y","X","Z","Z","Y","Y","Z","X","X","X","X","X","Z","X","X","Z"],"age":[22,19,21,25,20,18,27,28,21,35,22,21,19,21,30,25,30,22,32,35]},"columns":[{"accessor":"course_id","name":"CID","type":"numeric","aggregate":"count","width":100,"align":"left"},{"accessor":"course_name","name":"Course Name","type":"character","aggregate":"unique","width":140},{"accessor":"start_date","name":"Start Date","type":"Date","aggregate":"unique","width":120,"align":"center"},{"accessor":"end_date","name":"End Date","type":"Date","aggregate":"unique","width":120,"align":"center"},{"accessor":"s_id","name":"SID","type":"numeric","width":70,"align":"center"},{"accessor":"s_name","name":"Student Name","type":"character"},{"accessor":"gender","name":"Gender","type":"character","width":80,"align":"center"},{"accessor":"age","name":"Age","type":"numeric","width":50}],"pivotBy":["course_id"],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"striped":true,"compact":true,"dataKey":"e9fc15f3a53a06ad3569f7b3f70a31cd"},"children":[]},"class":"reactR_markup"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>In this case, all the columns which are not <code>aggregate</code>d remain hidden. Clicking the little triangle in the <code>CID</code> column displays the hidden rows. Looks better, but again, the issue of duplicated data remains.</p>
<p>You can aggregate the second-level columns too, but this distorts the table and frankly, looks ugly. Here I aggregate the <code>SID</code> column in addition to all the other top-level columns.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactable</span>(</span>
<span id="cb7-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data       =</span> combined_df,</span>
<span id="cb7-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compact    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for minimum row height</span></span>
<span id="cb7-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filterable =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for individual column filters</span></span>
<span id="cb7-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">striped    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># banded rows</span></span>
<span id="cb7-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resizable  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for resizable column widths</span></span>
<span id="cb7-7">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">groupBy    =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"course_id"</span>,</span>
<span id="cb7-8">  <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;">list</span>(</span>
<span id="cb7-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_id   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CID"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">100</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"count"</span>),</span>
<span id="cb7-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Course Name"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">140</span>,                     <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>),</span>
<span id="cb7-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_date  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Start Date"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>),</span>
<span id="cb7-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_date    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"End Date"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>),</span>
<span id="cb7-13">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># YIKES!! Aggregating Student ID to show unique ids in each course.</span></span>
<span id="cb7-14">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_id        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SID"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">aggregate =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"unique"</span>), </span>
<span id="cb7-15">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_name      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student Name"</span>),</span>
<span id="cb7-16">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gender"</span>,      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">80</span>,   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb7-17">    <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;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age"</span>,         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)</span>
<span id="cb7-18">  )</span>
<span id="cb7-19">)</span></code></pre></div>
</details><div class="cell-output-display">
<div id="htmlwidget-67b62794e33863e62991" class="reactable html-widget" style="width:auto;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-67b62794e33863e62991">{"x":{"tag":{"name":"Reactable","attribs":{"data":{"course_id":[1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,4,4,4],"course_name":["Course A","Course A","Course A","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course B","Course C","Course C","Course C","Course C","Course D","Course D","Course D"],"start_date":["2021-01-01","2021-01-01","2021-01-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-02-01","2021-03-01","2021-03-01","2021-03-01","2021-03-01","2021-04-01","2021-04-01","2021-04-01"],"end_date":["2021-01-31","2021-01-31","2021-01-31","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-02-28","2021-03-31","2021-03-31","2021-03-31","2021-03-31","2021-04-30","2021-04-30","2021-04-30"],"s_id":[4,5,14,6,7,8,9,10,13,15,17,18,19,1,3,12,16,2,11,20],"s_name":["Student D","Student E","Student N","Student F","Student G","Student H","Student I","Student J","Student M","Student O","Student Q","Student R","Student S","Student A","Student C","Student L","Student P","Student B","Student K","Student T"],"gender":["X","Y","Y","Y","Y","X","Z","Z","Y","Y","Z","X","X","X","X","X","Z","X","X","Z"],"age":[22,19,21,25,20,18,27,28,21,35,22,21,19,21,30,25,30,22,32,35]},"columns":[{"accessor":"course_id","name":"CID","type":"numeric","aggregate":"count","width":100,"align":"left"},{"accessor":"course_name","name":"Course Name","type":"character","aggregate":"unique","width":140},{"accessor":"start_date","name":"Start Date","type":"Date","aggregate":"unique","width":120,"align":"center"},{"accessor":"end_date","name":"End Date","type":"Date","aggregate":"unique","width":120,"align":"center"},{"accessor":"s_id","name":"SID","type":"numeric","aggregate":"unique","width":70,"align":"center"},{"accessor":"s_name","name":"Student Name","type":"character"},{"accessor":"gender","name":"Gender","type":"character","width":80,"align":"center"},{"accessor":"age","name":"Age","type":"numeric","width":50}],"pivotBy":["course_id"],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"striped":true,"compact":true,"dataKey":"51a6dfecd993aea3dbb11e027155fa81"},"children":[]},"class":"reactR_markup"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>Wouldn’t it be nice if we could display only the top-level columns by default and on clicking the small triangle for a row, show all the second-level columns corresponding to that row only, like a drill-down table?</p>
<p>To do this we need 2 separate tables. Earlier in this post, I said it is advisable to split such denormalized data into normalized data i.e.&nbsp;create the original top-level and second level tables from the <code>combined_df</code>. Let’s recreate the 2 tables.</p>
<p>I want to demonstrate how we go from the combined data to the 2 tables. Hence I will not use the <code>course</code> and <code>student</code> tables created earlier.</p>
<p>Creating the <code>top_level</code> table using just the columns in <code>course</code>. Let’s also create a new column <code>n_students</code> depicting count of students in each course.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb8-1">top_level <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb8-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only course info columns</span></span>
<span id="cb8-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">count</span>(course_id, course_name, start_date, end_date, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"n_students"</span>) </span>
<span id="cb8-4"></span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(top_level)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["course_id"],"name":[1],"type":["int"],"align":["right"]},{"label":["course_name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["start_date"],"name":[3],"type":["date"],"align":["right"]},{"label":["end_date"],"name":[4],"type":["date"],"align":["right"]},{"label":["n_students"],"name":[5],"type":["int"],"align":["right"]}],"data":[{"1":"1","2":"Course A","3":"2021-01-01","4":"2021-01-31","5":"3"},{"1":"2","2":"Course B","3":"2021-02-01","4":"2021-02-28","5":"10"},{"1":"3","2":"Course C","3":"2021-03-01","4":"2021-03-31","5":"4"},{"1":"4","2":"Course D","3":"2021-04-01","4":"2021-04-30","5":"3"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb9-1">second_level <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> combined_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-2">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Only Student info columns with unique identifier for Course</span></span>
<span id="cb9-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(course_id, s_id, s_name, gender, age) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(s_id)</span>
<span id="cb9-5"></span>
<span id="cb9-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paged_table</span>(second_level)</span></code></pre></div>
</details><div class="cell-output-display">
<div data-pagedtable="false">
  <script data-pagedtable-source="" type="application/json">
{"columns":[{"label":["course_id"],"name":[1],"type":["int"],"align":["right"]},{"label":["s_id"],"name":[2],"type":["int"],"align":["right"]},{"label":["s_name"],"name":[3],"type":["chr"],"align":["left"]},{"label":["gender"],"name":[4],"type":["chr"],"align":["left"]},{"label":["age"],"name":[5],"type":["int"],"align":["right"]}],"data":[{"1":"3","2":"1","3":"Student A","4":"X","5":"21"},{"1":"4","2":"2","3":"Student B","4":"X","5":"22"},{"1":"3","2":"3","3":"Student C","4":"X","5":"30"},{"1":"1","2":"4","3":"Student D","4":"X","5":"22"},{"1":"1","2":"5","3":"Student E","4":"Y","5":"19"},{"1":"2","2":"6","3":"Student F","4":"Y","5":"25"},{"1":"2","2":"7","3":"Student G","4":"Y","5":"20"},{"1":"2","2":"8","3":"Student H","4":"X","5":"18"},{"1":"2","2":"9","3":"Student I","4":"Z","5":"27"},{"1":"2","2":"10","3":"Student J","4":"Z","5":"28"},{"1":"4","2":"11","3":"Student K","4":"X","5":"32"},{"1":"3","2":"12","3":"Student L","4":"X","5":"25"},{"1":"2","2":"13","3":"Student M","4":"Y","5":"21"},{"1":"1","2":"14","3":"Student N","4":"Y","5":"21"},{"1":"2","2":"15","3":"Student O","4":"Y","5":"35"},{"1":"3","2":"16","3":"Student P","4":"Z","5":"30"},{"1":"2","2":"17","3":"Student Q","4":"Z","5":"22"},{"1":"2","2":"18","3":"Student R","4":"X","5":"21"},{"1":"2","2":"19","3":"Student S","4":"X","5":"19"},{"1":"4","2":"20","3":"Student T","4":"Z","5":"35"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
  </script>
</div>
</div>
</div>
</section><section id="the-final-drill-down-table" class="level2"><h2 class="anchored" data-anchor-id="the-final-drill-down-table">The final Drill-down Table</h2>
<p>Now that we have the 2 tables ready, let us now create the final <a href="https://glin.github.io/reactable/">reactable</a>. The trick here is to use the <strong>details</strong> argument to which we pass another <a href="https://glin.github.io/reactable/">reactable</a> of just the rows with students data corresponding to given course.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactable</span>(</span>
<span id="cb10-2">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data       =</span> top_level,</span>
<span id="cb10-3">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compact    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for minimum row height</span></span>
<span id="cb10-4">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filterable =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for individual column filters</span></span>
<span id="cb10-5">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">striped    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># banded rows</span></span>
<span id="cb10-6">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resizable  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># for resizable column widths</span></span>
<span id="cb10-7">  <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;">list</span>(</span>
<span id="cb10-8">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_id   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"CID"</span>,             <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>,  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb10-9">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_name =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Course Name"</span>), </span>
<span id="cb10-10">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">start_date  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Start Date"</span>,      <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb10-11">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">end_date    =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"End Date"</span>,        <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">120</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb10-12">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">n_students  =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No. of Students"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">130</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb10-13">  ),</span>
<span id="cb10-14">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">details =</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">function</span>(index) { <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># index is the row number of current row.</span></span>
<span id="cb10-15">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># sub-table of only those students for current row.</span></span>
<span id="cb10-16">    sec_lvl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> second_level[second_level<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>course_id <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> top_level<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>course_id[index], ] </span>
<span id="cb10-17">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">reactable</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data       =</span> sec_lvl,</span>
<span id="cb10-18">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">compact    =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb10-19">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">filterable =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb10-20">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bordered   =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, </span>
<span id="cb10-21">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">resizable  =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>,</span>
<span id="cb10-22">              <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;">list</span>(</span>
<span id="cb10-23">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">course_id   =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">show =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># hide the course id column</span></span>
<span id="cb10-24">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_id        =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"SID"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">70</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb10-25">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">s_name      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Student Name"</span>),</span>
<span id="cb10-26">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">gender      =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Gender"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">90</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>),</span>
<span id="cb10-27">                <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">age         =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">colDef</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">name =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Age"</span>,    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">width =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">align =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"center"</span>)</span>
<span id="cb10-28">              )</span>
<span id="cb10-29">              )</span>
<span id="cb10-30">  }</span>
<span id="cb10-31">)</span></code></pre></div>
</details><div class="cell-output-display">
<div id="htmlwidget-5a0cb435521fcff53b1c" class="reactable html-widget" style="width:auto;height:auto;"></div>
<script type="application/json" data-for="htmlwidget-5a0cb435521fcff53b1c">{"x":{"tag":{"name":"Reactable","attribs":{"data":{"course_id":[1,2,3,4],"course_name":["Course A","Course B","Course C","Course D"],"start_date":["2021-01-01","2021-02-01","2021-03-01","2021-04-01"],"end_date":["2021-01-31","2021-02-28","2021-03-31","2021-04-30"],"n_students":[3,10,4,3]},"columns":[{"accessor":".details","name":"","type":"NULL","sortable":false,"resizable":false,"filterable":false,"searchable":false,"width":45,"align":"center","details":[{"name":"Reactable","attribs":{"data":{"course_id":[1,1,1],"s_id":[4,5,14],"s_name":["Student D","Student E","Student N"],"gender":["X","Y","Y"],"age":[22,19,21]},"columns":[{"accessor":"course_id","name":"course_id","type":"numeric","show":false},{"accessor":"s_id","name":"SID","type":"numeric","width":70,"align":"center"},{"accessor":"s_name","name":"Student Name","type":"character"},{"accessor":"gender","name":"Gender","type":"character","width":90,"align":"center"},{"accessor":"age","name":"Age","type":"numeric","width":50,"align":"center"}],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"bordered":true,"compact":true,"dataKey":"12036566002706ab50f66831a92eae32","nested":true},"children":[]},{"name":"Reactable","attribs":{"data":{"course_id":[2,2,2,2,2,2,2,2,2,2],"s_id":[6,7,8,9,10,13,15,17,18,19],"s_name":["Student F","Student G","Student H","Student I","Student J","Student M","Student O","Student Q","Student R","Student S"],"gender":["Y","Y","X","Z","Z","Y","Y","Z","X","X"],"age":[25,20,18,27,28,21,35,22,21,19]},"columns":[{"accessor":"course_id","name":"course_id","type":"numeric","show":false},{"accessor":"s_id","name":"SID","type":"numeric","width":70,"align":"center"},{"accessor":"s_name","name":"Student Name","type":"character"},{"accessor":"gender","name":"Gender","type":"character","width":90,"align":"center"},{"accessor":"age","name":"Age","type":"numeric","width":50,"align":"center"}],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"bordered":true,"compact":true,"dataKey":"6587f2ad3dcb0fcc5216bf4473c986a0","nested":true},"children":[]},{"name":"Reactable","attribs":{"data":{"course_id":[3,3,3,3],"s_id":[1,3,12,16],"s_name":["Student A","Student C","Student L","Student P"],"gender":["X","X","X","Z"],"age":[21,30,25,30]},"columns":[{"accessor":"course_id","name":"course_id","type":"numeric","show":false},{"accessor":"s_id","name":"SID","type":"numeric","width":70,"align":"center"},{"accessor":"s_name","name":"Student Name","type":"character"},{"accessor":"gender","name":"Gender","type":"character","width":90,"align":"center"},{"accessor":"age","name":"Age","type":"numeric","width":50,"align":"center"}],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"bordered":true,"compact":true,"dataKey":"e3963514a17cdadfff8af46fde214632","nested":true},"children":[]},{"name":"Reactable","attribs":{"data":{"course_id":[4,4,4],"s_id":[2,11,20],"s_name":["Student B","Student K","Student T"],"gender":["X","X","Z"],"age":[22,32,35]},"columns":[{"accessor":"course_id","name":"course_id","type":"numeric","show":false},{"accessor":"s_id","name":"SID","type":"numeric","width":70,"align":"center"},{"accessor":"s_name","name":"Student Name","type":"character"},{"accessor":"gender","name":"Gender","type":"character","width":90,"align":"center"},{"accessor":"age","name":"Age","type":"numeric","width":50,"align":"center"}],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"bordered":true,"compact":true,"dataKey":"5a3a2a276df6d52409d68b95331871bb","nested":true},"children":[]}]},{"accessor":"course_id","name":"CID","type":"numeric","width":50,"align":"center"},{"accessor":"course_name","name":"Course Name","type":"character"},{"accessor":"start_date","name":"Start Date","type":"Date","width":120,"align":"center"},{"accessor":"end_date","name":"End Date","type":"Date","width":120,"align":"center"},{"accessor":"n_students","name":"No. of Students","type":"numeric","width":130,"align":"center"}],"resizable":true,"filterable":true,"defaultPageSize":10,"paginationType":"numbers","showPageInfo":true,"minRows":1,"striped":true,"compact":true,"dataKey":"4234130aace77d06280744fa5876f205"},"children":[]},"class":"reactR_markup"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<p>Since the sub-table is also a <a href="https://glin.github.io/reactable/">reactable</a>, you can go another level down… and another, but please do consider the usability aspect of this feature before taking that decision. I haven’t tried going beyond 2 levels of data myself. Maybe a part 2 to this post??</p>
</section><section id="conclusion" class="level2"><h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>Drill-down tables let you pack a lot of data in a compact manner and allow use by multiple audiences interested in varying degrees/levels of information. <a href="https://glin.github.io/reactable/">reactable</a> can help create an interactive data table from tabular data with sorting and pagination by default. The data table is an HTML widget that can be used in R Markdown documents and Shiny applications, or viewed from an R console. A lot of features can be enabled/disabled using the basic arguments of the <code>reactable()</code> function and much more using custom JavaScript.</p>
</section><section id="references-citations" class="level2"><h2 class="anchored" data-anchor-id="references-citations">References &amp; Citations</h2>
<ul>
<li>Greg Lin (2020). reactable: Interactive Data Tables Based on ‘React Table’. R package version 0.2.3. https://CRAN.R-project.org/package=reactable</li>
</ul>


</section><div id="quarto-appendix" class="default"><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2021,
  author = {Katti, Vishal},
  title = {Interactive {Drill-down} {Tables} Using \{Reactable\}},
  date = {2021-07-27},
  url = {https://vishalkatti.com/posts/drilldown-with-reactable/},
  langid = {en},
  abstract = {This post demonstrates how to use the \{reactable\}
    package to create multi-level drill-down tables having hidden rows}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2021" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2021. <span>“Interactive Drill-down Tables Using
{Reactable}.”</span> July 27, 2021. <a href="https://vishalkatti.com/posts/drilldown-with-reactable/">https://vishalkatti.com/posts/drilldown-with-reactable/</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>reactable</category>
  <category>drill-down</category>
  <guid>https://vishalkatti.com/posts/drilldown-with-reactable/</guid>
  <pubDate>Mon, 26 Jul 2021 18:30:00 GMT</pubDate>
  <media:content url="https://i.imgur.com/Zzk5SWf.gif" medium="image" type="image/gif"/>
</item>
<item>
  <title>Programming with R {dplyr} - As I Understand It!!</title>
  <dc:creator>Vishal Katti</dc:creator>
  <link>https://vishalkatti.com/posts/programming-with-dplyr/</link>
  <description><![CDATA[ <section id="introduction" class="level1"><h1>Introduction</h1>
<p>The purpose of this article is to act as a quick guide for myself and others to understand how to use <code>dplyr</code> effectively to create dynamic functions. The general assumption is that the reader is familiar with the <a href="https://dplyr.tidyverse.org">dplyr</a> package and how to use it for data wrangling.</p>
<section id="inspiration" class="level3"><h3 class="anchored" data-anchor-id="inspiration">Inspiration</h3>
<p>I regularly deal with event-related information with event date and few other columns like event type, root cause etc. Most reports usually involve calculating number of events that took place on a monthly, quarterly or annual basis, sometimes split by event type, root cause and other columns. After a few reports I realized that I am basically writing the same code over and over again to calculate these KPIs. Keeping the <code>DRY</code><sup>1</sup> principle in mind, I managed to write a few functions to calculate these KPIs with a few dynamic variables. Following is an attempt to articulate what I learnt while creating those functions.</p>
</section><section id="data" class="level3"><h3 class="anchored" data-anchor-id="data">Data</h3>
<p>We shall use the Texas Housing Sales data, available as a tibble in the popular <code>ggplot2</code> package as reference data. It contains monthly information about the housing market in Texas provided by the TAMU real estate center, <a href="https://www.recenter.tamu.edu/" class="uri">https://www.recenter.tamu.edu/</a>. It has 8602 observations and 9 variables.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb1-1">txhousing <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> ggplot2<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>txhousing</span>
<span id="cb1-2">dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(txhousing)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>Rows: 8,602
Columns: 9
$ city      &lt;chr&gt; "Abilene", "Abilene", "Abilene", "Abilene", "Abilene", "Abil~
$ year      &lt;int&gt; 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, ~
$ month     &lt;int&gt; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, ~
$ sales     &lt;dbl&gt; 72, 98, 130, 98, 141, 156, 152, 131, 104, 101, 100, 92, 75, ~
$ volume    &lt;dbl&gt; 5380000, 6505000, 9285000, 9730000, 10590000, 13910000, 1263~
$ median    &lt;dbl&gt; 71400, 58700, 58100, 68600, 67300, 66900, 73500, 75000, 6450~
$ listings  &lt;dbl&gt; 701, 746, 784, 785, 794, 780, 742, 765, 771, 764, 721, 658, ~
$ inventory &lt;dbl&gt; 6.3, 6.6, 6.8, 6.9, 6.8, 6.6, 6.2, 6.4, 6.5, 6.6, 6.2, 5.7, ~
$ date      &lt;dbl&gt; 2000.000, 2000.083, 2000.167, 2000.250, 2000.333, 2000.417, ~</code></pre>
</div>
</div>
<p>We shall refer the above data in all the following sections.</p>
</section></section><section id="select" class="level1"><h1><code>select()</code></h1>
<p>When using <code>dplyr</code> functions, the two most popular ways to pass column names is either as bare names i.e.&nbsp;column names without enclosing them in quotes like <code>sales</code> or <code>volume</code> OR pass them as a character string like double-quote “sales” or single-quote ‘volume’. You could also pass a character vector like <code>c("sales", "volume")</code>. In this section we will explore the 3 ways to dynamically select the columns we want.</p>
<section id="passing-raw-column-names" class="level3"><h3 class="anchored" data-anchor-id="passing-raw-column-names">Passing raw column names</h3>
<p>In this method, we pass the raw name of the column we want to select and use the <code>embrace</code> of curly-curly brackets to pass the raw name. For multiple columns, we can pass the raw names as a single vector.</p>
<div class="cell" data-label="select_raw">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb3-1">select_raw <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>(df, var) {</span>
<span id="cb3-2">  </span>
<span id="cb3-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># embrace of curly-curly {{}} brackets</span></span>
<span id="cb3-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, {{var}}) <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;">head</span>()</span>
<span id="cb3-6">}</span>
<span id="cb3-7"></span>
<span id="cb3-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass single raw name</span></span>
<span id="cb3-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select_raw</span>(txhousing, sales)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 1
  sales
  &lt;dbl&gt;
1    72
2    98
3   130
4    98
5   141
6   156</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass a vector of raw names for multiple columns</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select_raw</span>(txhousing, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sales, volume))      </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 2
  sales   volume
  &lt;dbl&gt;    &lt;dbl&gt;
1    72  5380000
2    98  6505000
3   130  9285000
4    98  9730000
5   141 10590000
6   156 13910000</code></pre>
</div>
</div>
<p>If passing multiple raw names as vector as in the <code>select_raw()</code> feels like an unnecessary complication, try the next method.</p>
</section><section id="passing-multiple-raw-column-names-using-...-argument" class="level3"><h3 class="anchored" data-anchor-id="passing-multiple-raw-column-names-using-...-argument">Passing multiple raw column names using <code>...</code> argument</h3>
<p>In this method, we use the <code>...</code> argument to pass the raw names of the columns we want to select.</p>
<div class="cell" data-label="my_select">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb7-1">my_select <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>(df, ...) {</span>
<span id="cb7-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, ...) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb7-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb7-4">}</span>
<span id="cb7-5"></span>
<span id="cb7-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass multiple raw names directly</span></span>
<span id="cb7-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_select</span>(txhousing, sales, volume)          </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 2
  sales   volume
  &lt;dbl&gt;    &lt;dbl&gt;
1    72  5380000
2    98  6505000
3   130  9285000
4    98  9730000
5   141 10590000
6   156 13910000</code></pre>
</div>
</div>
</section><section id="passing-a-character-vector-of-column-names" class="level3"><h3 class="anchored" data-anchor-id="passing-a-character-vector-of-column-names">Passing a character vector of column names</h3>
<p>If we have the column names as a character vector, we use the <code>all_of</code> function to pass the character vector to the internal <code>select</code> function.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb9-1">my_select_char <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>(df, cols) {</span>
<span id="cb9-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">all_of</span>(cols)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb9-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb9-4">}</span>
<span id="cb9-5"></span>
<span id="cb9-6">my_cols <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;">"sales"</span>,<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"volume"</span>)</span>
<span id="cb9-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_select_char</span>(txhousing, my_cols)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 2
  sales   volume
  &lt;dbl&gt;    &lt;dbl&gt;
1    72  5380000
2    98  6505000
3   130  9285000
4    98  9730000
5   141 10590000
6   156 13910000</code></pre>
</div>
</div>
</section></section><section id="filter" class="level1"><h1><code>filter()</code></h1>
<p>In the previous section, we passed column names either as bare names or character strings. <code><a href="https://rdrr.io/r/stats/filter.html">filter()</a></code> takes one or more expressions/conditions that result in a logical vector, with same length as number of rows in the data.frame/tibble and returns only those rows for which the expression/condition returns <code>TRUE</code>. Following are 2 ways to pass these logical expressions/conditions. I’m using expression and condition interchangeably here. In this context, a condition is an expression that results in a boolean <code>TRUE/FALSE</code> result.</p>
<section id="passing-single-raw-criteria" class="level3"><h3 class="anchored" data-anchor-id="passing-single-raw-criteria">Passing single raw criteria</h3>
<p>In this method, we pass the condition <code>sales &gt; 8000</code> as a raw/bare expression.</p>
<div class="cell" data-label="filter_raw">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb11-1">filter_raw <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>(df, cond) {</span>
<span id="cb11-2">  </span>
<span id="cb11-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># embrace of curly-curly {{}} brackets</span></span>
<span id="cb11-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, {{cond}})        </span>
<span id="cb11-5">}</span>
<span id="cb11-6"></span>
<span id="cb11-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Pass a single raw criterion</span></span>
<span id="cb11-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter_raw</span>(txhousing, sales <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;">8000</span>)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 x 9
   city     year month sales     volume median listings inventory  date
   &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;      &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
 1 Houston  2006     5  8040 1602621368 151200    35398       5.5 2006.
 2 Houston  2006     6  8628 1795898108 155200    36281       5.6 2006.
 3 Houston  2013     5  8439 2121508529 186100    20526       3.3 2013.
 4 Houston  2013     7  8468 2168720825 187800    21497       3.3 2014.
 5 Houston  2013     8  8155 2083377894 186700    21366       3.3 2014.
 6 Houston  2014     6  8391 2342443127 211200    19725       2.9 2014.
 7 Houston  2014     7  8391 2278932511 199700    20214       3   2014.
 8 Houston  2014     8  8167 2195184825 202400    20007       2.9 2015.
 9 Houston  2015     6  8449 2490238594 222400    22311       3.2 2015.
10 Houston  2015     7  8945 2568156780 217600    23875       3.4 2016.</code></pre>
</div>
</div>
<p>Do you think we can pass multiple bare conditions as a vector, like we did for <code>select_raw()</code> in the previous section? Let us try passing multiple raw criteria as a vector.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter_raw</span>(txhousing, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sales <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;">8000</span>, year <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;">2010</span>))</span></code></pre></div>
</details><pre style="color: red;"><code>Error in `dplyr::filter()`:
! Problem while computing `..1 = c(sales &gt; 8000, year &gt; 2010)`.
x Input `..1` must be of size 8602 or 1, not size 17204.
</code></pre>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Vector Concatenation
</div>
</div>
<div class="callout-body-container callout-body">
<p>Passing multiple raw criteria as a vector doesn’t work like it works for <code>select_raw()</code> function. Let us understand why. Consider the following code:</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb14-1">A <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)      <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># boolean vector of length = 2</span></span>
<span id="cb14-2">B <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="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>)    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># boolean vector of length = 2</span></span>
<span id="cb14-3">X <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(A, B)</span>
<span id="cb14-4">X</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>[1]  TRUE  TRUE FALSE FALSE</code></pre>
</div>
</div>
<p>Notice that length of X is 4. Similarly, <code>sales &gt; 8000</code> evaluates to a TRUE/FALSE boolean vector of length 8602 (equal to number of rows in <code>txhousing</code>) and so does <code>year &gt; 2010</code>. So the vector <code>c(sales &gt; 8000, year &gt; 2010)</code> becomes a TRUE/FALSE boolean vector of length 17204, which results in an error.</p>
</div>
</div>
</section><section id="passing-multiple-raw-criteria-using-argument" class="level3"><h3 class="anchored" data-anchor-id="passing-multiple-raw-criteria-using-argument">Passing multiple raw criteria using … argument</h3>
<p>To pass multiple raw criteria, we can use the <code>...</code> argument.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb16" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb16-1">my_filter <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>(df, ...) { </span>
<span id="cb16-2">  </span>
<span id="cb16-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass the dots argument</span></span>
<span id="cb16-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, ...)</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;"># pass multiple raw criteria</span></span>
<span id="cb16-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_filter</span>(txhousing, sales <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;">8000</span>, year <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;">2010</span>) </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 8 x 9
  city     year month sales     volume median listings inventory  date
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;      &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Houston  2013     5  8439 2121508529 186100    20526       3.3 2013.
2 Houston  2013     7  8468 2168720825 187800    21497       3.3 2014.
3 Houston  2013     8  8155 2083377894 186700    21366       3.3 2014.
4 Houston  2014     6  8391 2342443127 211200    19725       2.9 2014.
5 Houston  2014     7  8391 2278932511 199700    20214       3   2014.
6 Houston  2014     8  8167 2195184825 202400    20007       2.9 2015.
7 Houston  2015     6  8449 2490238594 222400    22311       3.2 2015.
8 Houston  2015     7  8945 2568156780 217600    23875       3.4 2016.</code></pre>
</div>
</div>
</section><section id="passing-single-criteria-as-a-character-string" class="level3"><h3 class="anchored" data-anchor-id="passing-single-criteria-as-a-character-string">Passing single criteria as a character string</h3>
<p>By default, <code><a href="https://dplyr.tidyverse.org/reference/filter.html">dplyr::filter()</a></code> does not accept conditions as character strings. Following is an example which results in error</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb18" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb18-1">dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(txhousing, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales &gt; 8000"</span>)</span></code></pre></div>
</details><pre style="color: red;"><code>Error in `dplyr::filter()`:
! Problem while computing `..1 = "sales &gt; 8000"`.
x Input `..1` must be a logical vector, not a character.
</code></pre>
</div>
<p>We need to convert the character condition into a raw expression.</p>
<div class="cell" data-label="my_filter_string">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb19-1">my_filter_string <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>(df, cond) {</span>
<span id="cb19-2">  </span>
<span id="cb19-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert text to raw criterion</span></span>
<span id="cb19-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> cond))) </span>
<span id="cb19-5">}</span>
<span id="cb19-6"></span>
<span id="cb19-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass single text string as criteria</span></span>
<span id="cb19-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_filter_string</span>(txhousing, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales &gt; 8000"</span>)  </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 10 x 9
   city     year month sales     volume median listings inventory  date
   &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;      &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
 1 Houston  2006     5  8040 1602621368 151200    35398       5.5 2006.
 2 Houston  2006     6  8628 1795898108 155200    36281       5.6 2006.
 3 Houston  2013     5  8439 2121508529 186100    20526       3.3 2013.
 4 Houston  2013     7  8468 2168720825 187800    21497       3.3 2014.
 5 Houston  2013     8  8155 2083377894 186700    21366       3.3 2014.
 6 Houston  2014     6  8391 2342443127 211200    19725       2.9 2014.
 7 Houston  2014     7  8391 2278932511 199700    20214       3   2014.
 8 Houston  2014     8  8167 2195184825 202400    20007       2.9 2015.
 9 Houston  2015     6  8449 2490238594 222400    22311       3.2 2015.
10 Houston  2015     7  8945 2568156780 217600    23875       3.4 2016.</code></pre>
</div>
</div>
<p>The special sauce here is the <code>eval(parse(text = ...))</code> combo that converts the long text criteria into a single raw criteria and passes it to the internal <code><a href="https://rdrr.io/r/stats/filter.html">filter()</a></code> function.</p>
</section><section id="passing-multiple-criteria-as-character-vector" class="level3"><h3 class="anchored" data-anchor-id="passing-multiple-criteria-as-character-vector">Passing multiple criteria as character vector</h3>
<p>What if want to pass multiple criteria as a string vector? In such a situation, we must combine all the string conditions into a single long string condition using <code>paste0(..., collapse = " &amp; ")</code>. The <code>paste0("(", cond, ")", collapse = " &amp; ")</code> combines all the criteria into a single long criteria, but still a text string.</p>
<div class="cell" data-label="my_filter_strings">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb21-1">my_filter_strings <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>(df, cond) { </span>
<span id="cb21-2">  </span>
<span id="cb21-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># combine all criteria</span></span>
<span id="cb21-4">  filter_text <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;">paste0</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"("</span>, cond, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">")"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">collapse =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">" &amp; "</span>)</span>
<span id="cb21-5">  </span>
<span id="cb21-6">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (OPTIONAL) show the combined filter string</span></span>
<span id="cb21-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">message</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Filter Condition: "</span>, filter_text)</span>
<span id="cb21-8">  </span>
<span id="cb21-9">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># convert text to raw criterion</span></span>
<span id="cb21-10">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">filter</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">eval</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">parse</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">text =</span> filter_text)))</span>
<span id="cb21-11">  }</span>
<span id="cb21-12"></span>
<span id="cb21-13">my_filter_criteria <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;">"sales &gt; 8000"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year &gt; 2010"</span>)</span>
<span id="cb21-14"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_filter_strings</span>(txhousing, my_filter_criteria)</span></code></pre></div>
</details><div class="cell-output cell-output-stderr">
<pre><code>Filter Condition: (sales &gt; 8000) &amp; (year &gt; 2010)</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 8 x 9
  city     year month sales     volume median listings inventory  date
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;      &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Houston  2013     5  8439 2121508529 186100    20526       3.3 2013.
2 Houston  2013     7  8468 2168720825 187800    21497       3.3 2014.
3 Houston  2013     8  8155 2083377894 186700    21366       3.3 2014.
4 Houston  2014     6  8391 2342443127 211200    19725       2.9 2014.
5 Houston  2014     7  8391 2278932511 199700    20214       3   2014.
6 Houston  2014     8  8167 2195184825 202400    20007       2.9 2015.
7 Houston  2015     6  8449 2490238594 222400    22311       3.2 2015.
8 Houston  2015     7  8945 2568156780 217600    23875       3.4 2016.</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-caution callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
OR Condition
</div>
</div>
<div class="callout-body-container callout-body">
<p>To create an OR condition, the expression must be a single string separated by pipe ‘|’ as in example below.</p>
</div>
</div>
<div class="cell" data-label="filter_OR">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb24" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb24-1">my_filter_criteria_with_OR <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;">"sales &gt; 8000 | sales &lt; 50"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year &gt; 2010"</span>)</span>
<span id="cb24-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">my_filter_strings</span>(txhousing, my_filter_criteria_with_OR)</span></code></pre></div>
</details><div class="cell-output cell-output-stderr">
<pre><code>Filter Condition: (sales &gt; 8000 | sales &lt; 50) &amp; (year &gt; 2010)</code></pre>
</div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 315 x 9
   city         year month sales  volume median listings inventory  date
   &lt;chr&gt;       &lt;int&gt; &lt;int&gt; &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
 1 Brownsville  2011     1    48 4974408  83300      784      12.6 2011 
 2 Brownsville  2011     2    47 5558575 101400      776      12.7 2011.
 3 Brownsville  2011     7    47 4807019  91200      749      13.1 2012.
 4 Brownsville  2011    12    39 4203440  86800      726      12.4 2012.
 5 Brownsville  2012     1    43 3892348  85000      791      13.6 2012 
 6 Brownsville  2012     3    27 2976148  93800      734      13.3 2012.
 7 Brownsville  2012    11    41 5115393  99000      807      14   2013.
 8 Brownsville  2013    11    38 4824930 108000      859      13.4 2014.
 9 Brownsville  2015     1    41 5400796  97000      733      10.7 2015 
10 Galveston    2011     1    43 8882961 170000     1015      13.7 2011 
# ... with 305 more rows</code></pre>
</div>
</div>
</section></section><section id="mutate" class="level1"><h1><code>mutate()</code></h1>
<p><code>mutate()</code> allows you to add new columns or modify existing columns. In the example below, we will create a new column <code>volume_in_millions</code> from the existing column <code>volume</code>. The names of both the columns can be passed to the function either as raw names or character strings.</p>
<section id="passing-the-column-name-as-raw-name" class="level3"><h3 class="anchored" data-anchor-id="passing-the-column-name-as-raw-name">Passing the column name as raw name</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb27" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb27-1">mutate_raw <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>(df, new_col_raw, old_col_raw, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">num =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) { </span>
<span id="cb27-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, {{new_col_raw}} <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> {{old_col_raw}}<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>num) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb27-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb27-4">}</span>
<span id="cb27-5"></span>
<span id="cb27-6">txhousing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb27-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(city, year, month, volume) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb27-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass raw column names w/o quotes</span></span>
<span id="cb27-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate_raw</span>(vol_in_millions, volume, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E6</span>) </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 5
  city     year month   volume vol_in_millions
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt;    &lt;dbl&gt;           &lt;dbl&gt;
1 Abilene  2000     1  5380000            5.38
2 Abilene  2000     2  6505000            6.50
3 Abilene  2000     3  9285000            9.28
4 Abilene  2000     4  9730000            9.73
5 Abilene  2000     5 10590000           10.6 
6 Abilene  2000     6 13910000           13.9 </code></pre>
</div>
</div>
</section><section id="passing-the-new-variable-name-as-character-string-direct" class="level3"><h3 class="anchored" data-anchor-id="passing-the-new-variable-name-as-character-string-direct">Passing the new variable name as character string (direct)</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb29" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb29-1">mutate_text <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>(df, new_col_str, old_col_str, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">num =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) { </span>
<span id="cb29-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, {{new_col_str}} <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> df[[old_col_str]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>num) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb29-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb29-4">}</span>
<span id="cb29-5"></span>
<span id="cb29-6">txhousing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb29-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(city, year, month, volume) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb29-8">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass column names as strings</span></span>
<span id="cb29-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate_text</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"vol_in_millions"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"volume"</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E6</span>) </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 5
  city     year month   volume vol_in_millions
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt;    &lt;dbl&gt;           &lt;dbl&gt;
1 Abilene  2000     1  5380000            5.38
2 Abilene  2000     2  6505000            6.50
3 Abilene  2000     3  9285000            9.28
4 Abilene  2000     4  9730000            9.73
5 Abilene  2000     5 10590000           10.6 
6 Abilene  2000     6 13910000           13.9 </code></pre>
</div>
</div>
</section><section id="passing-the-new-variable-name-as-character-string-indirect" class="level3"><h3 class="anchored" data-anchor-id="passing-the-new-variable-name-as-character-string-indirect">Passing the new variable name as character string (indirect)</h3>
<p>Instead of passing the name of the variable as a character string as an argument, we can pass a variable containing the name of the variable. In the below example, the name of the new variable is stored in <code>new_var</code>. Using the new <a href="https://glue.tidyverse.org/">glue</a> syntax, enabled by the walrus operator <code>:=</code>, we substitute the <code>new_var</code> variable with its value.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb31" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb31-1">mutate_var <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>(df, new_col_var, old_col_var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">num =</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>) {</span>
<span id="cb31-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{new_col_var}"</span> <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> df[[old_col_var]]<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">/</span>num) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb31-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb31-4">}</span>
<span id="cb31-5"></span>
<span id="cb31-6">new_var <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;">"vol_in_millions"</span></span>
<span id="cb31-7">old_var <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;">"volume"</span></span>
<span id="cb31-8"></span>
<span id="cb31-9">txhousing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb31-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(city, year, month, volume) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb31-11">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># pass column names as variables</span></span>
<span id="cb31-12">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate_var</span>(new_var, old_var, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1E6</span>)  </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 5
  city     year month   volume vol_in_millions
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt;    &lt;dbl&gt;           &lt;dbl&gt;
1 Abilene  2000     1  5380000            5.38
2 Abilene  2000     2  6505000            6.50
3 Abilene  2000     3  9285000            9.28
4 Abilene  2000     4  9730000            9.73
5 Abilene  2000     5 10590000           10.6 
6 Abilene  2000     6 13910000           13.9 </code></pre>
</div>
</div>
</section></section><section id="arrange" class="level1"><h1><code>arrange()</code></h1>
<p><code>arrange()</code> sorts the rows of a data frame by the values of selected columns. By default, it sorts in Ascending order. To force a column to sort in Descending order, we must use the <code>desc()</code> function.</p>
<section id="passing-single-raw-name" class="level3"><h3 class="anchored" data-anchor-id="passing-single-raw-name">Passing single raw name</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb33" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb33-1">arrange_raw <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>(df, var) {</span>
<span id="cb33-2">  </span>
<span id="cb33-3">  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># embrace of curly-curly {{}} brackets</span></span>
<span id="cb33-4">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, {{var}}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span>    </span>
<span id="cb33-5">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb33-6">}</span>
<span id="cb33-7"></span>
<span id="cb33-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_raw</span>(txhousing, sales)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city                year month sales  volume median listings inventory  date
  &lt;chr&gt;              &lt;int&gt; &lt;int&gt; &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 San Marcos          2011    10     6 1156999 180000      163       8.3 2012.
2 Harlingen           2000     7     9 1110000  87500      719      30.8 2000.
3 South Padre Island  2011     1     9 2088500 225000     1258      55.7 2011 
4 San Marcos          2011     1    10 1482310 140000      165       7.5 2011 
5 San Marcos          2011    12    10 1561250 140000      148       8   2012.
6 San Marcos          2014    11    10 1506878 146700       96       4   2015.</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb35" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb35-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_raw</span>(txhousing, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(sales))</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city     year month sales     volume median listings inventory  date
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;      &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Houston  2015     7  8945 2568156780 217600    23875       3.4 2016.
2 Houston  2006     6  8628 1795898108 155200    36281       5.6 2006.
3 Houston  2013     7  8468 2168720825 187800    21497       3.3 2014.
4 Houston  2015     6  8449 2490238594 222400    22311       3.2 2015.
5 Houston  2013     5  8439 2121508529 186100    20526       3.3 2013.
6 Houston  2014     6  8391 2342443127 211200    19725       2.9 2014.</code></pre>
</div>
</div>
<p><code>arrange_raw()</code> fails when we pass multiple raw names as a vector.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb37" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb37-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_raw</span>(txhousing, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(sales, volume))</span></code></pre></div>
</details><pre style="color: red;"><code>Error in `dplyr::arrange()`:
! Problem with the implicit `transmute()` step.
x Problem while computing `..1 = c(sales, volume)`.
x `..1` must be size 8602 or 1, not 17204.
</code></pre>
</div>
</section><section id="passing-multiple-raw-names-using-...-argument" class="level3"><h3 class="anchored" data-anchor-id="passing-multiple-raw-names-using-...-argument">Passing multiple raw names using <code>...</code> argument</h3>
<p>To pass multiple raw names, we must use the <code>...</code> argument.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb38" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb38-1">arrange_raw_multiple <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>(df, ...) {</span>
<span id="cb38-2">  dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, ...) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb38-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>()</span>
<span id="cb38-4">}</span>
<span id="cb38-5"></span>
<span id="cb38-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_raw_multiple</span>(txhousing, city, sales)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city     year month sales  volume median listings inventory  date
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Abilene  2003     1    68 5385000  70000      668       5.4  2003
2 Abilene  2011     1    68 8834493 123300      809       6.1  2011
3 Abilene  2009     1    70 8414801  92900      861       6.3  2009
4 Abilene  2000     1    72 5380000  71400      701       6.3  2000
5 Abilene  2010     1    73 9130783 112200      868       6.4  2010
6 Abilene  2001     1    75 5730000  64500      779       6.8  2001</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb40" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb40-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_raw_multiple</span>(txhousing, city, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(sales))</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city     year month sales   volume median listings inventory  date
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;    &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Abilene  2015     7   268 45845730 148700      986       5   2016.
2 Abilene  2015     6   260 41396230 141500      965       5   2015.
3 Abilene  2007     7   239 29315000 114300      940       5.2 2008.
4 Abilene  2013     8   236 30777727 120000      976       5.4 2014.
5 Abilene  2014     7   231 35861350 145800     1033       5.8 2014.
6 Abilene  2005     6   230 24050000  92500      664       4.1 2005.</code></pre>
</div>
</div>
</section><section id="pass-single-column-name-as-string" class="level3"><h3 class="anchored" data-anchor-id="pass-single-column-name-as-string">Pass single column name as string</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb42" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb42-1">arrange_str <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>(df, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.desc =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) {</span>
<span id="cb42-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (.desc) {</span>
<span id="cb42-3">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(df[[var]])) <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;">head</span>()</span>
<span id="cb42-4">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb42-5">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, df[[var]]) <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;">head</span>()</span>
<span id="cb42-6">  }</span>
<span id="cb42-7">}</span>
<span id="cb42-8"></span>
<span id="cb42-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_str</span>(txhousing, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales"</span>)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city                year month sales  volume median listings inventory  date
  &lt;chr&gt;              &lt;int&gt; &lt;int&gt; &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 San Marcos          2011    10     6 1156999 180000      163       8.3 2012.
2 Harlingen           2000     7     9 1110000  87500      719      30.8 2000.
3 South Padre Island  2011     1     9 2088500 225000     1258      55.7 2011 
4 San Marcos          2011     1    10 1482310 140000      165       7.5 2011 
5 San Marcos          2011    12    10 1561250 140000      148       8   2012.
6 San Marcos          2014    11    10 1506878 146700       96       4   2015.</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb44" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb44-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_str</span>(txhousing, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.desc =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city     year month sales     volume median listings inventory  date
  &lt;chr&gt;   &lt;int&gt; &lt;int&gt; &lt;dbl&gt;      &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Houston  2015     7  8945 2568156780 217600    23875       3.4 2016.
2 Houston  2006     6  8628 1795898108 155200    36281       5.6 2006.
3 Houston  2013     7  8468 2168720825 187800    21497       3.3 2014.
4 Houston  2015     6  8449 2490238594 222400    22311       3.2 2015.
5 Houston  2013     5  8439 2121508529 186100    20526       3.3 2013.
6 Houston  2014     6  8391 2342443127 211200    19725       2.9 2014.</code></pre>
</div>
</div>
</section><section id="pass-multiple-column-name-as-string" class="level3"><h3 class="anchored" data-anchor-id="pass-multiple-column-name-as-string">Pass multiple column name as string</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb46" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb46-1">arrange_str_multiple <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>(df, var, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">desc =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>) {</span>
<span id="cb46-2">  <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> (desc) {</span>
<span id="cb46-3">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">desc</span>(df[var])) <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;">head</span>()</span>
<span id="cb46-4">  } <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">else</span> {</span>
<span id="cb46-5">    dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.data =</span> df, df[var]) <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;">head</span>()</span>
<span id="cb46-6">  }</span>
<span id="cb46-7">}</span>
<span id="cb46-8"></span>
<span id="cb46-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># This function arranges the dataframe either all ascending</span></span>
<span id="cb46-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># or all descending. Definitely need a better example.</span></span>
<span id="cb46-11"></span>
<span id="cb46-12"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_str_multiple</span>(txhousing, <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;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales"</span>))</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city         year month sales  volume median listings inventory  date
  &lt;chr&gt;       &lt;int&gt; &lt;int&gt; &lt;dbl&gt;   &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Paris        2000     1    19 1440000  71700      286       7.5  2000
2 San Marcos   2000     1    22 2380000 106700      190       6.3  2000
3 Lufkin       2000     1    28 2280000  68000       NA      NA    2000
4 Harlingen    2000     1    31 3910000  87500      644      24.9  2000
5 Galveston    2000     1    37 4555000  95000      636       9.1  2000
6 Port Arthur  2000     1    40 3090000  68300      314       5.6  2000</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb48" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb48-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">arrange_str_multiple</span>(txhousing, <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;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sales"</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">desc =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 x 9
  city           year month sales     volume median listings inventory  date
  &lt;chr&gt;         &lt;int&gt; &lt;int&gt; &lt;dbl&gt;      &lt;dbl&gt;  &lt;dbl&gt;    &lt;dbl&gt;     &lt;dbl&gt; &lt;dbl&gt;
1 Houston        2015     7  8945 2568156780 217600    23875       3.4 2016.
2 Dallas         2015     7  7038 2021907410 233000    12292       2.4 2016.
3 Austin         2015     7  3466 1150381553 264600     7913       3   2016.
4 San Antonio    2015     7  2962  704891602 198100     9462       4.1 2016.
5 Collin County  2015     7  1861  613669702 292600     2809       2.1 2016.
6 Fort Bend      2015     7  1372  431875327 280400     3328       3.1 2016.</code></pre>
</div>
</div>
</section></section><section id="group_by" class="level1"><h1><code>group_by()</code></h1>
<p>In <code>group_by()</code>, we <em>select</em> which columns to, well, group by! (Damn these well-named functions!). So one can use the same techniques as <code>select()</code> to choose the columns.</p>
<p>In the following examples, we will create only one summarised value <code>total_sales</code> for simplicity.</p>
<section id="passing-single-raw-name-1" class="level3"><h3 class="anchored" data-anchor-id="passing-single-raw-name-1">Passing single raw name</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb50" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb50-1">group_raw <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>(df, grp) {</span>
<span id="cb50-2">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb50-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>({{grp}}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb50-4">    <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;">total_sales =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(sales, <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="cb50-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb50-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<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;">5</span>)</span>
<span id="cb50-7">}</span>
<span id="cb50-8"></span>
<span id="cb50-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of sales per year</span></span>
<span id="cb50-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_raw</span>(txhousing, year)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 2
   year total_sales
  &lt;int&gt;       &lt;dbl&gt;
1  2000      222483
2  2001      231453
3  2002      234600
4  2003      253909
5  2004      283999</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb52" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb52-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of sales per month</span></span>
<span id="cb52-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_raw</span>(txhousing, month)       </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 2
  month total_sales
  &lt;int&gt;       &lt;dbl&gt;
1     1      245924
2     2      296410
3     3      386909
4     4      397332
5     5      448968</code></pre>
</div>
</div>
</section><section id="passing-multiple-raw-names-using-the-...-operator" class="level3"><h3 class="anchored" data-anchor-id="passing-multiple-raw-names-using-the-...-operator">Passing multiple raw names using the <code>...</code> operator</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb54" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb54-1">group_raw_multiple <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>(df, ...) {</span>
<span id="cb54-2">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb54-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(...) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb54-4">    <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;">total_sales =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(sales, <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="cb54-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb54-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<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;">5</span>)</span>
<span id="cb54-7">}</span>
<span id="cb54-8"></span>
<span id="cb54-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of sales per year</span></span>
<span id="cb54-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_raw_multiple</span>(txhousing, year)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 2
   year total_sales
  &lt;int&gt;       &lt;dbl&gt;
1  2000      222483
2  2001      231453
3  2002      234600
4  2003      253909
5  2004      283999</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb56" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb56-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of sales per month</span></span>
<span id="cb56-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_raw_multiple</span>(txhousing, year, month)     </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 3
   year month total_sales
  &lt;int&gt; &lt;int&gt;       &lt;dbl&gt;
1  2000     1       11411
2  2000     2       15674
3  2000     3       20202
4  2000     4       18658
5  2000     5       22388</code></pre>
</div>
</div>
</section><section id="passing-single-or-multiple-column-names-as-character-string" class="level3"><h3 class="anchored" data-anchor-id="passing-single-or-multiple-column-names-as-character-string">Passing single or multiple column names as character string</h3>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb58" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb58-1">group_str <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>(df, grp) {</span>
<span id="cb58-2">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb58-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(df[grp]) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb58-4">    <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;">total_sales =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>(sales, <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="cb58-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb58-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<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;">5</span>)</span>
<span id="cb58-7">}</span>
<span id="cb58-8"></span>
<span id="cb58-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of sales per year</span></span>
<span id="cb58-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_str</span>(txhousing, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"year"</span>)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 2
   year total_sales
  &lt;int&gt;       &lt;dbl&gt;
1  2000      222483
2  2001      231453
3  2002      234600
4  2003      253909
5  2004      283999</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb60" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb60-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of sales per month</span></span>
<span id="cb60-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_str</span>(txhousing, <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;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</span>))       </span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 3
   year month total_sales
  &lt;int&gt; &lt;int&gt;       &lt;dbl&gt;
1  2000     1       11411
2  2000     2       15674
3  2000     3       20202
4  2000     4       18658
5  2000     5       22388</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb62" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb62-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The same column names can be passed as variables containing the character names</span></span>
<span id="cb62-2">yr <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;">"year"</span></span>
<span id="cb62-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_str</span>(txhousing, yr)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 2
   year total_sales
  &lt;int&gt;       &lt;dbl&gt;
1  2000      222483
2  2001      231453
3  2002      234600
4  2003      253909
5  2004      283999</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb64" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb64-1">yrmon <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;">"year"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"month"</span>)</span>
<span id="cb64-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_str</span>(txhousing, yrmon)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 3
   year month total_sales
  &lt;int&gt; &lt;int&gt;       &lt;dbl&gt;
1  2000     1       11411
2  2000     2       15674
3  2000     3       20202
4  2000     4       18658
5  2000     5       22388</code></pre>
</div>
</div>
<p>If you want the summarised column to have a custom name like <code>total_&lt;sumvar&gt;</code>, then you can wrap the value in quotes as below. This method uses the <code>glue</code> syntax enabled by the <code>:=</code> walrus operator. The walrus operator takes either a raw name or a character string on its LHS.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb66" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb66-1">group_raw2 <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>(df, grp, sumvar) {</span>
<span id="cb66-2">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb66-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>({{grp}}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb66-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total_{{sumvar}}"</span> <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>({{sumvar}}, <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="cb66-5">              <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>)  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb66-6">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">head</span>(<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;">5</span>)</span>
<span id="cb66-7">}</span>
<span id="cb66-8"></span>
<span id="cb66-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of sales per year</span></span>
<span id="cb66-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_raw2</span>(txhousing, year, sales)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 2
   year total_sales
  &lt;int&gt;       &lt;dbl&gt;
1  2000      222483
2  2001      231453
3  2002      234600
4  2003      253909
5  2004      283999</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb68" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb68-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Sum of listings per month</span></span>
<span id="cb68-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_raw2</span>(txhousing, month, listings)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 5 x 2
  month total_listings
  &lt;int&gt;          &lt;dbl&gt;
1     1        1854661
2     2        1888104
3     3        1949187
4     4        1991278
5     5        2038932</code></pre>
</div>
</div>
<p>After writing so many examples, I see a pattern. <code>group_by()</code> works with techniques similar to <code>select()</code> while <code>summarise()</code> works with techniques similar to <code>mutate()</code>.</p>
</section></section><section id="slightly-better-examples" class="level1"><h1>(Slightly Better) Examples</h1>
<p>The <code>txhousing</code> is a city-wise monthly sales and volume dataset. It has a <code>year</code> and <code>month</code> column. Let us create a <code>date</code> column and keep only those columns relevant for our custom <code>tx_summary()</code> function.</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb70" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb70-1">small_df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> txhousing <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb70-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;">date =</span> lubridate<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as_date</span>(glue<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;">glue</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"{year}-{month}-01"</span>))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb70-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(city, date, sales, volume)</span></code></pre></div>
</details>
</div>
<section id="mutate-example" class="level3"><h3 class="anchored" data-anchor-id="mutate-example">
<code>mutate()</code> example</h3>
<p>Now let us create the <code>create_ymq()</code> function. This function would take 2 arguments, a data frame <code>df</code> and a raw name of a date column.</p>
<div class="cell" data-label="create_ymq">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb71" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb71-1">create_ymq <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>(df, date_col) {</span>
<span id="cb71-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">inherits</span>(df, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data.frame"</span>))</span>
<span id="cb71-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">stopifnot</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> dplyr<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pull</span>({{date_col}})) <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;">'Date'</span>)</span>
<span id="cb71-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(df,</span>
<span id="cb71-5">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Year =</span> lubridate<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;">year</span>({{date_col}}),</span>
<span id="cb71-6">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nHalf =</span> lubridate<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;">semester</span>({{date_col}}),</span>
<span id="cb71-7">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yHalf =</span> lubridate<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;">semester</span>({{date_col}}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">with_year =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb71-8">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dHalf =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(lubridate<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;">semester</span>({{date_col}}), <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"H"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>({{date_col}},<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%y"</span>)),</span>
<span id="cb71-9">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">nQtr =</span> lubridate<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;">quarter</span>({{date_col}}),</span>
<span id="cb71-10">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yQtr =</span> lubridate<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;">quarter</span>({{date_col}}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">with_year =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>),</span>
<span id="cb71-11">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dQtr =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">paste0</span>(lubridate<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;">quarter</span>({{date_col}}),<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Q"</span>, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>({{date_col}},<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%y"</span>)),</span>
<span id="cb71-12">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">Month =</span> lubridate<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;">month</span>({{date_col}}),</span>
<span id="cb71-13">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">yMonth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">as.numeric</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>({{date_col}}, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%Y.%m"</span>)),</span>
<span id="cb71-14">         <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">dMonth =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span>({{date_col}}, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"%b %Y"</span>)</span>
<span id="cb71-15">         )</span>
<span id="cb71-16">}</span>
<span id="cb71-17"></span>
<span id="cb71-18"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_ymq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">df =</span> small_df, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_col =</span> date) <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;">glimpse</span>()</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code>Rows: 8,602
Columns: 14
$ city   &lt;chr&gt; "Abilene", "Abilene", "Abilene", "Abilene", "Abilene", "Abilene~
$ date   &lt;date&gt; 2000-01-01, 2000-02-01, 2000-03-01, 2000-04-01, 2000-05-01, 20~
$ sales  &lt;dbl&gt; 72, 98, 130, 98, 141, 156, 152, 131, 104, 101, 100, 92, 75, 112~
$ volume &lt;dbl&gt; 5380000, 6505000, 9285000, 9730000, 10590000, 13910000, 1263500~
$ Year   &lt;dbl&gt; 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 200~
$ nHalf  &lt;int&gt; 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, ~
$ yHalf  &lt;dbl&gt; 2000.1, 2000.1, 2000.1, 2000.1, 2000.1, 2000.1, 2000.2, 2000.2,~
$ dHalf  &lt;chr&gt; "1H00", "1H00", "1H00", "1H00", "1H00", "1H00", "2H00", "2H00",~
$ nQtr   &lt;int&gt; 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 1, 1, 1, 2, 2, 2, 3, 3, 3, ~
$ yQtr   &lt;dbl&gt; 2000.1, 2000.1, 2000.1, 2000.2, 2000.2, 2000.2, 2000.3, 2000.3,~
$ dQtr   &lt;chr&gt; "1Q00", "1Q00", "1Q00", "2Q00", "2Q00", "2Q00", "3Q00", "3Q00",~
$ Month  &lt;dbl&gt; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, ~
$ yMonth &lt;dbl&gt; 2000.01, 2000.02, 2000.03, 2000.04, 2000.05, 2000.06, 2000.07, ~
$ dMonth &lt;chr&gt; "Jan 2000", "Feb 2000", "Mar 2000", "Apr 2000", "May 2000", "Ju~</code></pre>
</div>
</div>
</section><section id="group_by-example" class="level3"><h3 class="anchored" data-anchor-id="group_by-example">
<code>group_by()</code> example</h3>
<p>Now that we have a function that creates various date-related columns, let us create a function that let’s you create summary tables like annual sales per city, quarterly volumes per city etc.</p>
<div class="cell" data-label="tx_summary">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb73" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb73-1">tx_summary <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>(df, grp_col, sum_col) {</span>
<span id="cb73-2">  df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb73-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">group_by</span>(city, {{grp_col}}) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb73-4">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summarise</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"total_{{sum_col}}"</span> <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> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">sum</span>({{sum_col}}, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">na.rm =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.groups =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'drop'</span>)</span>
<span id="cb73-5">}</span></code></pre></div>
</details>
</div>
<p>Using these 2 functions, we can now create multiple summary tables</p>
<div class="cell">
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb74" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb74-1">small_df_with_date_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> small_df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb74-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">create_ymq</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">date_col =</span> date)</span>
<span id="cb74-3"></span>
<span id="cb74-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Annual Sales per city</span></span>
<span id="cb74-5">small_df_with_date_cols <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb74-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tx_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grp_col =</span> Year, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sum_col =</span> sales)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 736 x 3
   city     Year total_sales
   &lt;chr&gt;   &lt;dbl&gt;       &lt;dbl&gt;
 1 Abilene  2000        1375
 2 Abilene  2001        1431
 3 Abilene  2002        1516
 4 Abilene  2003        1632
 5 Abilene  2004        1830
 6 Abilene  2005        1977
 7 Abilene  2006        1997
 8 Abilene  2007        2003
 9 Abilene  2008        1651
10 Abilene  2009        1634
# ... with 726 more rows</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb76" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb76-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Half Yearly volumes per city</span></span>
<span id="cb76-2">small_df_with_date_cols <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb76-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tx_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grp_col =</span> yHalf, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sum_col =</span> volume)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1,472 x 3
   city    yHalf total_volume
   &lt;chr&gt;   &lt;dbl&gt;        &lt;dbl&gt;
 1 Abilene 2000.     55400000
 2 Abilene 2000.     53175000
 3 Abilene 2001.     55795000
 4 Abilene 2001.     58570000
 5 Abilene 2002.     55305000
 6 Abilene 2002.     63370000
 7 Abilene 2003.     58175000
 8 Abilene 2003.     77500000
 9 Abilene 2004.     74205000
10 Abilene 2004.     85465000
# ... with 1,462 more rows</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb78" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb78-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Quarterly Sales per city</span></span>
<span id="cb78-2">small_df_with_date_cols <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb78-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tx_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grp_col =</span> yQtr, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sum_col =</span> sales)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2,898 x 3
   city     yQtr total_sales
   &lt;chr&gt;   &lt;dbl&gt;       &lt;dbl&gt;
 1 Abilene 2000.         300
 2 Abilene 2000.         395
 3 Abilene 2000.         387
 4 Abilene 2000.         293
 5 Abilene 2001.         305
 6 Abilene 2001.         394
 7 Abilene 2001.         401
 8 Abilene 2001.         331
 9 Abilene 2002.         295
10 Abilene 2002.         425
# ... with 2,888 more rows</code></pre>
</div>
<details open="" class="code-fold"><summary>Code</summary><div class="sourceCode cell-code" id="cb80" style="background: #f1f3f5;"><pre class="sourceCode numberSource r number-lines code-with-copy"><code class="sourceCode r"><span id="cb80-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Monthly Volumes per city</span></span>
<span id="cb80-2">small_df_with_date_cols <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb80-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tx_summary</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">grp_col =</span> yMonth, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">sum_col =</span> volume)</span></code></pre></div>
</details><div class="cell-output cell-output-stdout">
<pre><code># A tibble: 8,602 x 3
   city    yMonth total_volume
   &lt;chr&gt;    &lt;dbl&gt;        &lt;dbl&gt;
 1 Abilene  2000.      5380000
 2 Abilene  2000.      6505000
 3 Abilene  2000.      9285000
 4 Abilene  2000.      9730000
 5 Abilene  2000.     10590000
 6 Abilene  2000.     13910000
 7 Abilene  2000.     12635000
 8 Abilene  2000.     10710000
 9 Abilene  2000.      7615000
10 Abilene  2000.      7040000
# ... with 8,592 more rows</code></pre>
</div>
</div>
</section><section id="more-ideas" class="level3"><h3 class="anchored" data-anchor-id="more-ideas">More ideas</h3>
<p>You could further extend this by creating a custom filtering function that gives you, say, the rows with the highest or lowest <code>total_sales</code> or <code>total_volume</code>.</p>
</section></section><section id="conclusion" class="level1"><h1>Conclusion</h1>
<p>The ability to create such dynamic functions, enabled by the wonderful {dplyr} package, allows us to level-up in terms of programming with R and helps make our code neat and tidy.</p>
<div style="text-align:center;">
<div class="quarto-figure quarto-figure-center">
<figure class="figure"><p><img src="https://i.imgur.com/H9jo8OB.gif" class="img-fluid figure-img"></p>
<figcaption>How I feel while creating custom functions with {dplyr}! I can almost hear the music! Source: imgur.com</figcaption></figure>
</div>
</div>
</section><section id="references" class="level1"><h1>References</h1>
<ul>
<li>Hadley Wickham, Romain François, Lionel Henry and Kirill Müller (2022). dplyr: A Grammar of Data Manipulation. R package version 1.0.9. https://CRAN.R-project.org/package=dplyr</li>
<li>https://dplyr.tidyverse.org/articles/programming.html</li>
<li>H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016. https://ggplot2.tidyverse.org</li>
<li>Jim Hester and Jennifer Bryan (2022). glue: Interpreted String Literals. R package version 1.6.2. https://CRAN.R-project.org/package=glue</li>
</ul>


</section><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>
<ol>
<li id="fn1"><p><strong>D</strong>on’t <strong>R</strong>epeat <strong>Y</strong>ourself↩︎</p></li>
</ol></section><section class="quarto-appendix-contents" id="quarto-reuse"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a></div></div></section><section class="quarto-appendix-contents" id="quarto-citation"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{katti2021,
  author = {Katti, Vishal},
  title = {Programming with {R} \{Dplyr\} - {As} {I} {Understand}
    {It!!}},
  date = {2021-07-17},
  url = {https://vishalkatti.com/posts/programming-with-dplyr/},
  langid = {en},
  abstract = {This post demonstrates how to write your own dynamic
    functions using popular `dplyr` verbs like `select()`, `filter()`,
    `mutate()`, `arrange()` and `group\_by()` with `summarise()`.}
}
</code></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-katti2021" class="csl-entry quarto-appendix-citeas">
Katti, Vishal. 2021. <span>“Programming with R {Dplyr} - As I Understand
It!!”</span> July 17, 2021. <a href="https://vishalkatti.com/posts/programming-with-dplyr/">https://vishalkatti.com/posts/programming-with-dplyr/</a>.
</div></div></section></div> ]]></description>
  <category>R</category>
  <category>dplyr</category>
  <category>functions</category>
  <guid>https://vishalkatti.com/posts/programming-with-dplyr/</guid>
  <pubDate>Fri, 16 Jul 2021 18:30:00 GMT</pubDate>
  <media:content url="https://i.imgur.com/H9jo8OB.gif" medium="image" type="image/gif"/>
</item>
</channel>
</rss>
