Skip to content

Commit

Permalink
wrap-up mutate_at update
Browse files Browse the repository at this point in the history
  • Loading branch information
mgimond committed Feb 18, 2024
1 parent c04ca3a commit 17e7e63
Show file tree
Hide file tree
Showing 18 changed files with 253 additions and 20 deletions.
26 changes: 12 additions & 14 deletions docs/dplyr.html
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./robustness.html" class="sidebar-item-text sidebar-link">
<a href="./case_study.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">23</span>&nbsp; <span class="chapter-title">A working example: t-tests and re-expression</span></span></a>
</div>
</li>
Expand Down Expand Up @@ -439,7 +439,7 @@ <h2 id="toc-title">Table of contents</h2>
<li><a href="#arrange-sort-rows-by-column-value" id="toc-arrange-sort-rows-by-column-value" class="nav-link" data-scroll-target="#arrange-sort-rows-by-column-value"><span class="header-section-number">9.1.2</span> <code>arrange</code>: Sort rows by column value</a></li>
<li><a href="#select-subset-by-column" id="toc-select-subset-by-column" class="nav-link" data-scroll-target="#select-subset-by-column"><span class="header-section-number">9.1.3</span> <code>select</code>: Subset by column</a></li>
<li><a href="#mutate-creating-andor-calculating-column-values" id="toc-mutate-creating-andor-calculating-column-values" class="nav-link" data-scroll-target="#mutate-creating-andor-calculating-column-values"><span class="header-section-number">9.1.4</span> <code>mutate</code>: Creating and/or calculating column values</a></li>
<li><a href="#mutate_at-creating-andor-calculating-values-across-muliple-columns" id="toc-mutate_at-creating-andor-calculating-values-across-muliple-columns" class="nav-link" data-scroll-target="#mutate_at-creating-andor-calculating-values-across-muliple-columns"><span class="header-section-number">9.1.5</span> <code>mutate_at</code>: Creating and/or calculating values across muliple columns</a></li>
<li><a href="#mutate-across-muliple-columns" id="toc-mutate-across-muliple-columns" class="nav-link" data-scroll-target="#mutate-across-muliple-columns"><span class="header-section-number">9.1.5</span> <code>mutate</code> across muliple columns</a></li>
<li><a href="#summarise-summarize-columns" id="toc-summarise-summarize-columns" class="nav-link" data-scroll-target="#summarise-summarize-columns"><span class="header-section-number">9.1.6</span> <code>summarise</code>: Summarize columns</a></li>
</ul></li>
<li><a href="#sec-pipe" id="toc-sec-pipe" class="nav-link" data-scroll-target="#sec-pipe"><span class="header-section-number">9.2</span> Combining data manipulation functions using the pipe <code>%&gt;%</code></a>
Expand Down Expand Up @@ -807,8 +807,8 @@ <h3 data-number="9.1.4" class="anchored" data-anchor-id="mutate-creating-andor-c
</div>
</div>
</section>
<section id="mutate_at-creating-andor-calculating-values-across-muliple-columns" class="level3" data-number="9.1.5">
<h3 data-number="9.1.5" class="anchored" data-anchor-id="mutate_at-creating-andor-calculating-values-across-muliple-columns"><span class="header-section-number">9.1.5</span> <code>mutate_at</code>: Creating and/or calculating values across muliple columns</h3>
<section id="mutate-across-muliple-columns" class="level3" data-number="9.1.5">
<h3 data-number="9.1.5" class="anchored" data-anchor-id="mutate-across-muliple-columns"><span class="header-section-number">9.1.5</span> <code>mutate</code> across muliple columns</h3>
<p>You might find yourself wanting to apply a same set of <em>mutate</em> operations across multiple variables. For example, given the following sample dataset,</p>
<div class="cell" data-hash="dplyr_cache/html/unnamed-chunk-20_a640c66831780824f1c78c149201b315">
<div class="sourceCode cell-code" id="cb41"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>met <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(<span class="at">Wind =</span> <span class="fu">c</span>(<span class="fl">3.4</span>, <span class="fl">5.0</span>, <span class="dv">99</span>, <span class="fl">4.1</span>, <span class="fl">1.5</span>),</span>
Expand All @@ -825,10 +825,9 @@ <h3 data-number="9.1.5" class="anchored" data-anchor-id="mutate_at-creating-ando
</div>
</div>
<p>where the value <code>99</code> is a placeholder for a missing value for the variables <code>Wind</code> and <code>Prec</code> but a valid value for <code>Dir</code>, we want to replace all missing values with <code>NA</code>. We could either create two mutate operations as in,</p>
<div class="cell" data-hash="dplyr_cache/html/unnamed-chunk-21_307caa79e7eae042f48abd93a38fd8aa">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>met2 <span class="ot">&lt;-</span> <span class="fu">mutate</span>( met, <span class="at">Wind =</span> <span class="fu">ifelse</span>(Wind <span class="sc">==</span> <span class="dv">99</span>, <span class="cn">NA</span>, Wind),</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> <span class="at">Prec =</span> <span class="fu">ifelse</span>(Prec <span class="sc">==</span> <span class="dv">99</span>, <span class="cn">NA</span>, Prec))</span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a>met2</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell" data-hash="dplyr_cache/html/unnamed-chunk-21_587f6e2b9213bd613386f649e1f1f64e">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>met <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="at">Wind =</span> <span class="fu">ifelse</span>(Wind <span class="sc">==</span> <span class="dv">99</span>, <span class="cn">NA</span>, Wind),</span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> <span class="at">Prec =</span> <span class="fu">ifelse</span>(Prec <span class="sc">==</span> <span class="dv">99</span>, <span class="cn">NA</span>, Prec))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Wind Dir Prec
1 3.4 181 NA
Expand All @@ -838,11 +837,10 @@ <h3 data-number="9.1.5" class="anchored" data-anchor-id="mutate_at-creating-ando
5 1.5 99 NA</code></pre>
</div>
</div>
<p>or, we could reduce the separate <code>mutate</code> operations into a single <code>mutate_at</code> operation,</p>
<div class="cell" data-hash="dplyr_cache/html/unnamed-chunk-22_bd443bc613cdb7aaf9378ff8d34303f7">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>met2 <span class="ot">&lt;-</span> <span class="fu">mutate_at</span>( met, <span class="fu">vars</span>(Wind, Prec),</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">list</span>(<span class="sc">~</span> <span class="fu">ifelse</span>( . <span class="sc">==</span> <span class="dv">99</span>, <span class="cn">NA</span>, .)))</span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a>met2</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<p>or, we could reduce the separate <code>mutate</code> operations into a single operation by adding an <code>across()</code> function to its argument.</p>
<div class="cell" data-hash="dplyr_cache/html/unnamed-chunk-22_0a56e1f86e345910c15658f659696060">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>met <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="fu">across</span>( <span class="fu">c</span>(Wind, Prec),</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a> <span class="sc">~</span> <span class="fu">ifelse</span>( . <span class="sc">==</span> <span class="dv">99</span>, <span class="cn">NA</span>, .)))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> Wind Dir Prec
1 3.4 181 NA
Expand All @@ -852,7 +850,7 @@ <h3 data-number="9.1.5" class="anchored" data-anchor-id="mutate_at-creating-ando
5 1.5 99 NA</code></pre>
</div>
</div>
<p>The dot <code>.</code> is a placeholder for each column listed in the <code>vars()</code> function.</p>
<p><code>across</code> takes two arguments: the columns the mutate operation is to operate on, and the mutate operation to perform on these columns. Here, the tilde <code>~</code> can be interpreted as <em>“to the listed columns, apply the following function …”</em>. The dot <code>.</code> in the <code>ifelse</code> function is a placeholder for each column listed in <code>across</code>’s first argument.</p>
</section>
<section id="summarise-summarize-columns" class="level3" data-number="9.1.6">
<h3 data-number="9.1.6" class="anchored" data-anchor-id="summarise-summarize-columns"><span class="header-section-number">9.1.6</span> <code>summarise</code>: Summarize columns</h3>
Expand Down
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">

<meta name="author" content="Manny Gimond">
<meta name="dcterms.date" content="2024-02-05">
<meta name="dcterms.date" content="2024-02-18">

<title>Exploratory Data Analysis in R</title>
<style>
Expand Down Expand Up @@ -457,7 +457,7 @@ <h1 class="title">Exploratory Data Analysis in R</h1>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">February 5, 2024</p>
<p class="date">February 18, 2024</p>
</div>
</div>

Expand Down
Loading

0 comments on commit 17e7e63

Please sign in to comment.