<?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>Catherine Blatter</title>
<link>https://cathblatter.netlify.app/listings.html</link>
<atom:link href="https://cathblatter.netlify.app/listings.xml" rel="self" type="application/rss+xml"/>
<description></description>
<generator>quarto-1.3.450</generator>
<lastBuildDate>Thu, 24 Feb 2022 00:00:00 GMT</lastBuildDate>
<item>
  <title>Different approaches to rename variables</title>
  <dc:creator>Cath Blatter</dc:creator>
  <link>https://cathblatter.netlify.app/posts/2022/2022-02-24_renaming-variables.html</link>
  <description><![CDATA[ 




<section id="renaming-individual-columns" class="level2">
<h2 class="anchored" data-anchor-id="renaming-individual-columns">Renaming individual columns</h2>
<p>This was usually a rather small post with snippets but turned out to be much more interesting…</p>
<p><strong>Loading the necessary package</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span></code></pre></div>
</div>
<p><strong>Sample dataframe</strong></p>
<p>Creating a sample dataframe: the function <code>tribble()</code> lets you construct rowwise-tibbles (thus the R in <code>tribble()</code>). This is closer to the idea of data in a spreadsheet than the mirrored version with <code>tibble()</code> or <code>data.frame()</code>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb2-2">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>id, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>pesnwi_leadership, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>pesnwi_sra, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>bernca_adl, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>bernca_docu,</span>
<span id="cb2-3">  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">1.4</span>,</span>
<span id="cb2-4">  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.0</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>,</span>
<span id="cb2-5">  <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span></span>
<span id="cb2-6">)</span></code></pre></div>
</div>
<p>This is the equivalent code for the ‘mirrored’ version:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1">df <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb3-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">data.frame</span>(</span>
<span id="cb3-3">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">id =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>),</span>
<span id="cb3-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pesnwi_leadership =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.3</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.1</span>),</span>
<span id="cb3-5">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pesnwi_sra =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.5</span>),</span>
<span id="cb3-6">           <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bernca_adl =</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;">1.8</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">2.1</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">3.9</span>),</span>
<span id="cb3-7">          <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">bernca_docu =</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;">1.4</span>, <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.9</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>)</span>
<span id="cb3-8">  )</span></code></pre></div>
</div>
<p>Finally, this is how the dataframe looks like:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1">df</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 5
     id pesnwi_leadership pesnwi_sra bernca_adl bernca_docu
  &lt;dbl&gt;             &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;       &lt;dbl&gt;
1     1               2.3        2.1        1.8         1.4
2     2               2          3          2.1         0.9
3     3               3.1        3.5        3.9         3  </code></pre>
</div>
</div>
<section id="manual-renaming-with-dplyrrename" class="level3">
<h3 class="anchored" data-anchor-id="manual-renaming-with-dplyrrename">Manual renaming with <code>dplyr::rename()</code></h3>
<p>Manual renaming following the idea of newname = oldname. This is the approach you need to take for manual replacement of column-names (see an equivalent base-R code below). You can rename more than three variables like this, just add further combinations of newname = oldname…</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1">df <span class="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;">rename</span>(</span>
<span id="cb6-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pesnwi_ls =</span> pesnwi_leadership,</span>
<span id="cb6-4">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ration_adl =</span> bernca_adl,</span>
<span id="cb6-5">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">ration_docu =</span> bernca_docu</span>
<span id="cb6-6">  )</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 5
     id pesnwi_ls pesnwi_sra ration_adl ration_docu
  &lt;dbl&gt;     &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;       &lt;dbl&gt;
1     1       2.3        2.1        1.8         1.4
2     2       2          3          2.1         0.9
3     3       3.1        3.5        3.9         3  </code></pre>
</div>
</div>
</section>
<section id="manual-renaming-with-base-r" class="level3">
<h3 class="anchored" data-anchor-id="manual-renaming-with-base-r">Manual renaming with <code>base-R</code></h3>
<p>Of course its also possible to rename the columns with other approaches, e.g.&nbsp;:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(df)[<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">names</span>(df)<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;">"pesnwi_leadership"</span>] <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pesnwi_ls"</span></span></code></pre></div>
</div>
</section>
</section>
<section id="renaming-multiple-columns-at-once" class="level2">
<h2 class="anchored" data-anchor-id="renaming-multiple-columns-at-once">Renaming multiple columns at once</h2>
<section id="renaming-based-on-text-patterns-with-dplyrrename_with" class="level3">
<h3 class="anchored" data-anchor-id="renaming-based-on-text-patterns-with-dplyrrename_with">Renaming based on text-patterns with <code>dplyr::rename_with()</code></h3>
<p>It’s possible to rename multiple column-names based on matching text-patterns (regular expression). This is great if you need to rename multiple variables with the same pattern, but it only works if you can find such a pattern. If you’re in doubt which option to choose its probably safer to do the manual method above…</p>
<p>For the example dataframe, say we want to replace the prefix “bernca” (the name of an instrument to measure rationed nursing care <sup>1</sup>) by the more general term “ration”. This is possible with the following code:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1">df <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb9-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename_with</span>(</span>
<span id="cb9-3">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">.fn =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> stringr<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;">str_replace</span>(., <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bernca"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">replacement =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ration"</span>),</span>
<span id="cb9-4">    <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;">everything</span>()</span>
<span id="cb9-5">  )</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 5
     id pesnwi_leadership pesnwi_sra ration_adl ration_docu
  &lt;dbl&gt;             &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;       &lt;dbl&gt;
1     1               2.3        2.1        1.8         1.4
2     2               2          3          2.1         0.9
3     3               3.1        3.5        3.9         3  </code></pre>
</div>
</div>
<p>By default, all columns get considered, but we could limit this or manually define the columns with <code>c(bernca_adl, bernca_docu)</code>. Note that I used the {stringr} package for replacing the pattern ‘bernca’ with ‘ration’.</p>
</section>
</section>
<section id="renaming-based-on-names-defined-in-an-external-document-e.g.-xlsx" class="level2">
<h2 class="anchored" data-anchor-id="renaming-based-on-names-defined-in-an-external-document-e.g.-xlsx">Renaming based on names defined in an external document (e.g.&nbsp;xlsx)</h2>
<p>Imagine the following situation: a survey study with three timepoints was conducted over a timespan of 2 years: T0 as baseline, T1 = T0+12months and T2 = T0+24months.<br>
Some of the questions were only asked at baseline, some across all timepoints and selected variables only in T1 and T2. For several reasons (a.k.a. real-life data collection) this led to situations, where a variable named X12 in T0, did in fact not correspond to the variable named X12 in T1 and so on.</p>
<p>In total there were &gt; 250 variables to rename and the person defining the new variables did not work with R. Thus, the solution was to store the information in an external spreadsheet and then write code to import this spreadsheet and rename based on it. But how?</p>
<p>A spreadsheet was created in excel that essentially held information on the variable names of a specific questions across all time points. Additionally, the first column ‘global’ defines the new variable name that will be used across all datasets once the renaming took place. Below is a small example of how the excel-file looked like:</p>
<div class="cell">
<div class="cell-output-display">
<table data-quarto-postprocess="true" class="table table-sm table-striped small">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">global</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">T0</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">T1</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">T2</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">ID</td>
<td style="text-align: left;">ID</td>
<td style="text-align: left;">ID</td>
<td style="text-align: left;">ID</td>
</tr>
<tr class="even">
<td style="text-align: left;">X1</td>
<td style="text-align: left;">X1</td>
<td style="text-align: left;">X3</td>
<td style="text-align: left;">X2</td>
</tr>
<tr class="odd">
<td style="text-align: left;">X2</td>
<td style="text-align: left;">X4</td>
<td style="text-align: left;">X10</td>
<td style="text-align: left;">X11</td>
</tr>
<tr class="even">
<td style="text-align: left;">X3</td>
<td style="text-align: left;"></td>
<td style="text-align: left;">X2</td>
<td style="text-align: left;">X12</td>
</tr>
</tbody>
</table>


</div>
</div>
<section id="creating-sample-datasets" class="level3">
<h3 class="anchored" data-anchor-id="creating-sample-datasets">Creating sample datasets</h3>
<p>Below I created some sample datasets to mimic T0, T1 and T2 and the information from the spreadsheet:</p>
<p><strong>For the data from the surveys T0, T1, T2</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1">orig_t0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb11-2">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>ID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X1, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X4,</span>
<span id="cb11-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"A"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"m"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>,</span>
<span id="cb11-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"B"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span></span>
<span id="cb11-5">)</span>
<span id="cb11-6"></span>
<span id="cb11-7">orig_t1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb11-8">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>ID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X3, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X10, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X2,</span>
<span id="cb11-9">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"AAA"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f"</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;">44</span>,</span>
<span id="cb11-10">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BBB"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"x"</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;">56</span>,</span>
<span id="cb11-11">)</span>
<span id="cb11-12"></span>
<span id="cb11-13">orig_t2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span>  tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb11-14">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>ID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X2, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X11, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>X12,</span>
<span id="cb11-15">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"FFF"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f"</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;">53</span>,</span>
<span id="cb11-16">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"BGG"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"f"</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;">23</span>,</span>
<span id="cb11-17">)</span></code></pre></div>
</div>
<p><strong>Information from the spreadsheet</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1">name_tbl <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(</span>
<span id="cb12-2">  <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>global, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>T0, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>T1, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>T2,</span>
<span id="cb12-3">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ID"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ID"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ID"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ID"</span>,</span>
<span id="cb12-4">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X1"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X3"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X2"</span>,</span>
<span id="cb12-5">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X4"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X10"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X11"</span>,</span>
<span id="cb12-6">  <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X3"</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X2"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"X12"</span></span>
<span id="cb12-7">)</span></code></pre></div>
</div>
</section>
<section id="using-a-named-vector-to-rename-dataframes" class="level3">
<h3 class="anchored" data-anchor-id="using-a-named-vector-to-rename-dataframes">Using a named vector to rename dataframes</h3>
<p><em>check also this tweet by <span class="citation" data-cites="PipingHotData">@PipingHotData</span>: https://twitter.com/PipingHotData/status/1497014703473704965?s=20&amp;t=TA5bW8K-wxczoaW2Q6UWCQ</em></p>
<p>As noted above <code>rename()</code> takes its inputs following the usual tidyverse-style, meaning <code>rename(newname = oldname)</code>. This can be translated to the situation at hand by using the corresponding columns from the spreadsheet to create a named vector that has the information newname as ‘name’ and oldname as value. <code>tibble::deframe()</code> is suitable for this situation as described in the help-page: &gt; deframe() converts two-column data frames to a named vector or list, using the first column as name and the second column as value.</p>
<p><strong>For T0</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a named vector</span></span>
<span id="cb13-2">helper_rename_t0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> name_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(global, T0) <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;"># take the two variables needed</span></span>
<span id="cb13-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</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;"># drop_na() is important here </span></span>
<span id="cb13-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deframe</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># deframe creates the named vector</span></span>
<span id="cb13-6"></span>
<span id="cb13-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename with !!! </span></span>
<span id="cb13-8">(global_t0 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb13-9">  orig_t0 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb13-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!!</span>helper_rename_t0))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 3
  ID    X1       X2
  &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
1 A     m         1
2 B     x         2</code></pre>
</div>
</div>
<p><strong>For T1</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a named vector</span></span>
<span id="cb15-2">helper_rename_t1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> name_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(global, T1) <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;"># take the two variables needed</span></span>
<span id="cb15-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</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;"># drop_na() is important here </span></span>
<span id="cb15-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deframe</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># deframe creates the named vector</span></span>
<span id="cb15-6"></span>
<span id="cb15-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename with !!! </span></span>
<span id="cb15-8">(global_t1 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb15-9">  orig_t1 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb15-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!!</span>helper_rename_t1))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 4
  ID    X1       X2    X3
  &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
1 AAA   f         1    44
2 BBB   x         2    56</code></pre>
</div>
</div>
<p><strong>For T2</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a named vector</span></span>
<span id="cb17-2">helper_rename_t2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> name_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(global, T2) <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;"># take the two variables needed</span></span>
<span id="cb17-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</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;"># drop_na() is important here </span></span>
<span id="cb17-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">deframe</span>() <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># deframe creates the named vector</span></span>
<span id="cb17-6"></span>
<span id="cb17-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename with !!! </span></span>
<span id="cb17-8">(global_t2 <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb17-9">  orig_t2 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb17-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!!</span>helper_rename_t2))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 4
  ID    X1       X2    X3
  &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
1 FFF   f         2    53
2 BGG   f         1    23</code></pre>
</div>
</div>
<p><strong>Finally, using <code>bind_rows()</code> to combine all datasets</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1">(version1 <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>(global_t0, global_t1, global_t2))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 4
  ID    X1       X2    X3
  &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
1 A     m         1    NA
2 B     x         2    NA
3 AAA   f         1    44
4 BBB   x         2    56
5 FFF   f         2    53
6 BGG   f         1    23</code></pre>
</div>
</div>
<p>This approach worked very well, however some elements are repeated for every dataset (creating the helper vector, renaming), thus maybe a more functional programming approach might be suitable.</p>
</section>
<section id="using-functional-programming-to-rename-all-dataframes-at-once" class="level3">
<h3 class="anchored" data-anchor-id="using-functional-programming-to-rename-all-dataframes-at-once">Using functional programming to rename all dataframes at once</h3>
<p>The approach above works very well, yet sometimes its nice to have the output checked to the original content as well (see if the renaming was really correct).</p>
<p>The code presented here war written with help from the R4DS-online community, specifically <a href="https://github.com/TylerGrantSmith%5D">Tyler Smith</a> for the first option and <a href="https://twitter.com/jonthegeek"><span class="citation" data-cites="jonthegeek">@jonthegeek</span></a> for the 2nd one.</p>
<p><strong>First option</strong></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a long df with matching variable names</span></span>
<span id="cb21-2">var_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb21-3">  name_tbl <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> global) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pivot_longer</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>to, <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;">"source"</span>, <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;">"from"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-6">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(source, from, to) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">drop_na</span>()</span>
<span id="cb21-8"></span>
<span id="cb21-9"></span>
<span id="cb21-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># helper function to rename the cols</span></span>
<span id="cb21-11">rename_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(.data, from, to) {</span>
<span id="cb21-12">  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;">rename</span>(.data, </span>
<span id="cb21-13">                <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!!</span>rlang<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;">syms</span>(</span>
<span id="cb21-14">                  purrr<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;">discard</span>(</span>
<span id="cb21-15">                    rlang<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;">set_names</span>(from, to), <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">is.na</span>(from))))</span>
<span id="cb21-16">}</span>
<span id="cb21-17"></span>
<span id="cb21-18"></span>
<span id="cb21-19"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># helper function to table the cols</span></span>
<span id="cb21-20"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># to compare their content</span></span>
<span id="cb21-21">table_cols <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(.data, cols, ...) {</span>
<span id="cb21-22">  purrr<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;">map</span>(cols, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> base<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;">table</span>(.data[[.x]]), ...)</span>
<span id="cb21-23">}</span>
<span id="cb21-24"></span>
<span id="cb21-25"></span>
<span id="cb21-26"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># creating a list of all dataframes that need to be renamed</span></span>
<span id="cb21-27">data_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(</span>
<span id="cb21-28">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T0 =</span> orig_t0,</span>
<span id="cb21-29">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T1 =</span> orig_t1,</span>
<span id="cb21-30">  <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">T2 =</span> orig_t2</span>
<span id="cb21-31">)</span>
<span id="cb21-32"></span>
<span id="cb21-33"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Create a nested tibble, define the variables to </span></span>
<span id="cb21-34"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># be rename for each row, apply the renaming, finbally validate the</span></span>
<span id="cb21-35"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># data_table (original dataframe) versus the output_table (renamed dataframe)</span></span>
<span id="cb21-36"></span>
<span id="cb21-37">mapped_object <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb21-38">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">enframe</span>(data_list, <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;">"source"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">value =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"data"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-39">  <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;">mapping =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map</span>(source, <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>(var_names, source <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">==</span> .))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-40">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">hoist</span>(mapping, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">from =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"from"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">to =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"to"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb21-41">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">mutate</span>(</span>
<span id="cb21-42">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">pmap</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(data, from, to), rename_cols), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># renaming</span></span>
<span id="cb21-43">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data_table =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2</span>(data, from, table_cols, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">useNA =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"always"</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># validation</span></span>
<span id="cb21-44">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">output_table =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2</span>(output, to, table_cols, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">useNA =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"always"</span>), <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># validation</span></span>
<span id="cb21-45">    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">validate =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2_lgl</span>(data_table, output_table, identical) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># validation</span></span>
<span id="cb21-46">  )</span>
<span id="cb21-47"></span>
<span id="cb21-48"></span>
<span id="cb21-49"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># from the mapped_pbject, </span></span>
<span id="cb21-50"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># unnest the output - don't forget to store it into its own object</span></span>
<span id="cb21-51">mapped_object <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-52">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">select</span>(output) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb21-53">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">unnest</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">cols =</span> output)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 4
  ID    X1       X2    X3
  &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
1 A     m         1    NA
2 B     x         2    NA
3 AAA   f         1    44
4 BBB   x         2    56
5 FFF   f         2    53
6 BGG   f         1    23</code></pre>
</div>
</div>
<p><strong>Second option</strong></p>
<p>The approach is very similar: use <code>purrr::map2_dfr()</code> to indicate the use of two lists (.x and .y) whereas .x is the list of the dataframes and .y is the list of the vectors with the names. The suffix <code>_dfr()</code> does <code>bind_rows()</code> to combine the results into one dataframe</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># rename </span></span>
<span id="cb23-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">map2_dfr</span>(</span>
<span id="cb23-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(orig_t0, orig_t1, orig_t2),</span>
<span id="cb23-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(name_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T0, name_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T1, name_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>T2), <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(tdf, names) {</span>
<span id="cb23-5">    real_names <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> name_tbl<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>global[<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>(names)]</span>
<span id="cb23-6">    names <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;">keep</span>(names, <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(v) <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>(v))</span>
<span id="cb23-7">    tdf <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span></span>
<span id="cb23-8">      <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">rename</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">!!!</span>rlang<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;">syms</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">set_names</span>(names, real_names)))</span>
<span id="cb23-9">  }</span>
<span id="cb23-10">)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 4
  ID    X1       X2    X3
  &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
1 A     m         1    NA
2 B     x         2    NA
3 AAA   f         1    44
4 BBB   x         2    56
5 FFF   f         2    53
6 BGG   f         1    23</code></pre>
</div>
</div>
</section>
<section id="tldr" class="level3">
<h3 class="anchored" data-anchor-id="tldr">TL;DR</h3>
<ul>
<li><p>There are many different options on to how to rename a variable</p></li>
<li><p>This post should give an overview of some of the possibilities I have worked with</p></li>
<li><p>IMHO the best solution depends on the situation at hand - choose your pick!</p></li>
</ul>


</section>
</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>Schubert et al., 2007, https://doi.org/10.1097/01.nnr.0000299853.52429.62↩︎</p></li>
</ol>
</section></div> ]]></description>
  <guid>https://cathblatter.netlify.app/posts/2022/2022-02-24_renaming-variables.html</guid>
  <pubDate>Thu, 24 Feb 2022 00:00:00 GMT</pubDate>
  <media:content url="https://cathblatter.netlify.app/posts/2022/names.png" medium="image" type="image/png" height="121" width="144"/>
</item>
<item>
  <title>An (ever growing) list of random things in R</title>
  <dc:creator>Cath Blatter</dc:creator>
  <link>https://cathblatter.netlify.app/posts/2022/2022-01-27_random-stuff.html</link>
  <description><![CDATA[ 




<section id="motivation" class="level2">
<h2 class="anchored" data-anchor-id="motivation">Motivation</h2>
<p>This post should really just be a random list of things that R can do but I often forget about….</p>
</section>
<section id="print-calendar-in-r-console" class="level2">
<h2 class="anchored" data-anchor-id="print-calendar-in-r-console">Print calendar in R console</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cal"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>   February 2023      
Su Mo Tu We Th Fr Sa  
          1  2  3  4  
 5  6  7  8  9 10 11  
12 13 14 15 16 17 18  
19 20 21 22 23 24 25  
26 27 28              
                      </code></pre>
</div>
</div>
<p>Because, why not? 😎 It even goes for a whole year:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">system</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"cal 2021"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>                            2021
      January               February               March          
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
                1  2      1  2  3  4  5  6      1  2  3  4  5  6  
 3  4  5  6  7  8  9   7  8  9 10 11 12 13   7  8  9 10 11 12 13  
10 11 12 13 14 15 16  14 15 16 17 18 19 20  14 15 16 17 18 19 20  
17 18 19 20 21 22 23  21 22 23 24 25 26 27  21 22 23 24 25 26 27  
24 25 26 27 28 29 30  28                    28 29 30 31           
31                                                                

       April                  May                   June          
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
             1  2  3                     1         1  2  3  4  5  
 4  5  6  7  8  9 10   2  3  4  5  6  7  8   6  7  8  9 10 11 12  
11 12 13 14 15 16 17   9 10 11 12 13 14 15  13 14 15 16 17 18 19  
18 19 20 21 22 23 24  16 17 18 19 20 21 22  20 21 22 23 24 25 26  
25 26 27 28 29 30     23 24 25 26 27 28 29  27 28 29 30           
                      30 31                                       

        July                 August              September        
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
             1  2  3   1  2  3  4  5  6  7            1  2  3  4  
 4  5  6  7  8  9 10   8  9 10 11 12 13 14   5  6  7  8  9 10 11  
11 12 13 14 15 16 17  15 16 17 18 19 20 21  12 13 14 15 16 17 18  
18 19 20 21 22 23 24  22 23 24 25 26 27 28  19 20 21 22 23 24 25  
25 26 27 28 29 30 31  29 30 31              26 27 28 29 30        
                                                                  

      October               November              December        
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
                1  2      1  2  3  4  5  6            1  2  3  4  
 3  4  5  6  7  8  9   7  8  9 10 11 12 13   5  6  7  8  9 10 11  
10 11 12 13 14 15 16  14 15 16 17 18 19 20  12 13 14 15 16 17 18  
17 18 19 20 21 22 23  21 22 23 24 25 26 27  19 20 21 22 23 24 25  
24 25 26 27 28 29 30  28 29 30              26 27 28 29 30 31     
31                                                                </code></pre>
</div>
</div>
<p>further reading and more elaborate stuff: https://www.garrickadenbuie.com/blog/r-console-calendar/</p>


</section>

 ]]></description>
  <guid>https://cathblatter.netlify.app/posts/2022/2022-01-27_random-stuff.html</guid>
  <pubDate>Thu, 27 Jan 2022 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Installing &amp; using multiple R versions aside</title>
  <dc:creator>Cath Blatter</dc:creator>
  <link>https://cathblatter.netlify.app/posts/2021-03-06-installing-multiple-r-versions-aside/index.html</link>
  <description><![CDATA[ 




<section id="motivation" class="level1">
<h1>Motivation</h1>
<ul>
<li><p>I started using the <a href="https://rstudio.github.io/renv/">{renv}</a>-package about a year ago and it has saved me multiple times already. What always baffled me though was the hint when opening an ‘old’ project where I used a previous version of R to set it up - how would I be able to ‘switch’ back to an older version of R?</p></li>
<li><p>I remembered Garrick Aiden-Buie mentioning <a href="https://rud.is/rswitch/">Rswitch</a> in this <a href="https://www.garrickadenbuie.com/blog/setting-up-a-new-macbook-pro/">thread-post</a>.</p></li>
<li><p>I recently took a Intro-to-Python-course and stumbled upon <a href="https://docs.python.org/3/tutorial/venv.html">{venv}</a>. I was amazed to learn that in Python all previous versions stay available, so I could easily choose the version for a new virtual environment <code>r emo::ji("bulb")</code></p></li>
</ul>
</section>
<section id="how-it-started" class="level1">
<h1>How it started</h1>
<p>Thanks to google, I read numerous blog posts but did not understand much (not a regular command-line-user)…</p>
<ul>
<li><p>I gasped that using the .<code>pkg</code>-installer (double clicking through the GUI) will remove the previous version (noted <a href="https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Multiple-versions">here</a>)</p></li>
<li><p>I briefly played with the idea that instead I could unpack the <code>.tar.gz</code>-files and build from source as noted <a href="https://rud.is/rswitch/guide/">here</a> - while I was finally able to run all steps (something alongside <code>./configure make</code> etc) the version I installed was not placed in <code>~/Library/Frameworks/R.frameworks/</code> but remained in <code>/usr/local/bin/R</code> <code>r emo::ji("weary")</code> Not confident about meddling too much there another round of google was on.</p></li>
<li><p>I finally came across this answer on SO: https://stackoverflow.com/a/66155919 - it is pointed out, that <code>pkgutil --forget</code> should not mention the name of the file I downloaded (which I did…) but the name of the pkgs inside the thing I downloaded - thanks for this very helpful clarification! With new hope I did this:</p></li>
</ul>
</section>
<section id="how-its-going" class="level1">
<h1>How it’s going</h1>
<ol type="1">
<li><p>Download the package installer from the official page, eg. https://stat.ethz.ch/CRAN/bin/macosx/</p></li>
<li><p>Open the terminal and run the following line:</p></li>
</ol>
<p><code>pkgutil --pkgs | grep -i org.r-project | xargs -I {} sudo pkgutil --forget {}</code></p>
<p>Entering <code>sudo</code>-mode I got prompted for the admin password.</p>
<ol start="3" type="1">
<li><p>Open the package installer and follow the GUI-prompts as usual</p></li>
<li><p>Check with RSwitch - the new (and the old versions) should be there <code>r emo::ji("party")</code></p></li>
</ol>
<section id="note" class="level2">
<h2 class="anchored" data-anchor-id="note">Note</h2>
<p>I wrote this brief blogpost mainly for my future-self - having the steps outlined explicitly will hopefully save me time in the future. It took me repeated google-sessions in trying to understand the official R-docs and I was very grateful for the SO-answer - maybe another person might profit from this post as well - if so consider letting me know e.g.&nbsp;by <a href="https://twitter.com/cathblatter">tweet</a>.</p>
<p><code>r emo::ji("warning")</code> A note of caution: I do not fully understand what is really happening with the commands above under the hood and I cannot be held responsible for whatever happens on your machine if you follow these steps.<br>
They worked out on my computer(s) but for inquiries about what is really happening in the background consider following the official docs or ask on Stackoverflow!</p>
<p><code>r emo::ji("computer")</code> Apple Mac OS X High Sierra.</p>


</section>
</section>

 ]]></description>
  <guid>https://cathblatter.netlify.app/posts/2021-03-06-installing-multiple-r-versions-aside/index.html</guid>
  <pubDate>Sat, 06 Mar 2021 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Using tidyr::pivot_longer() and regex for data wrangling</title>
  <dc:creator>Cath Blatter</dc:creator>
  <link>https://cathblatter.netlify.app/posts/2020/2020-03-16-using-pivot-longer-and-regex-for-data-wrangling.html</link>
  <description><![CDATA[ 




<section id="tldr" class="level1">
<h1>TL;DR</h1>
<p>This code shows you how you can effectively wrangle your dataframe from wide to long using <code>tidyr::pivot_longer()</code> combined with regular expressions for properly naming the variables.</p>
</section>
<section id="your-data-is-not-in-the-right-format" class="level1">
<h1>Your data is… not in the right format!</h1>
<p>You have a wide dataset of patient data, each patient identified through <code>ID</code>. They have been hospitalised multiple times, each place of hospitalisation and outcome listed in its own variable. Place and Outcome of a hospitalization are linked with the same suffix number, e.g.&nbsp;<code>_2_2</code>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse))</span>
<span id="cb1-3"></span>
<span id="cb1-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create sample data</span></span>
<span id="cb1-5">patient_data <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb1-6">tibble<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">tribble</span>(<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>ID, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Hosp_Place_1, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Hosp_Outcome_1, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Hosp_Place_2_2, </span>
<span id="cb1-7">                <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Hosp_Outcome_2_2, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Hosp_Place_2_3, <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span>Hosp_Outcome_2_3, </span>
<span id="cb1-8">                <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;">"London"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Alive"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paris"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alive"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rome"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dead"</span>,</span>
<span id="cb1-9">                <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Paris"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alive"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Rome"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"alive"</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;">"alive"</span>,</span>
<span id="cb1-10">                <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">3</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Berne"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dead"</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>, <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA_character_</span>) </span>
<span id="cb1-11"></span>
<span id="cb1-12"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># look at data</span></span>
<span id="cb1-13">patient_data</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 3 × 7
     ID Hosp_Place_1 Hosp_Outcome_1 Hosp_Place_2_2 Hosp_Outcom…¹ Hosp_…² Hosp_…³
  &lt;dbl&gt; &lt;chr&gt;        &lt;chr&gt;          &lt;chr&gt;          &lt;chr&gt;         &lt;chr&gt;   &lt;chr&gt;  
1     1 London       Alive          Paris          alive         Rome    dead   
2     2 Paris        alive          Rome           alive         London  alive  
3     3 Berne        dead           &lt;NA&gt;           &lt;NA&gt;          &lt;NA&gt;    &lt;NA&gt;   
# … with abbreviated variable names ¹​Hosp_Outcome_2_2, ²​Hosp_Place_2_3,
#   ³​Hosp_Outcome_2_3</code></pre>
</div>
</div>
<p>Patient No.&nbsp;3 died in his first hospitatisation, so the following variables are listed <code>NA</code>.</p>
<p>For some analysis, this data structure would work ok, but for your specific questions you need to switch the unit of analysis to “hospitalisation” instead of “patient”. In other words, you’d like to switch the data from wide to long.</p>
<p>Thanks to the tidyverse’s initiative of making clear function names, it might be obvious, that <code>tidyr::pivot_longer()</code> should do what you want.</p>
</section>
<section id="familiarize-yourself-with-the-function---what-to-do-with-tidyrpivot_longer" class="level1">
<h1>Familiarize yourself with the function - What to do with tidyr::pivot_longer()?</h1>
<p>The first step is to initially think about, what the outcome should look like and what input-arguments the function takes, so let’s do this:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># tidyr::pivot_long() and its arguments</span></span>
<span id="cb3-2">tidyr<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;">pivot_longer</span>(data, </span>
<span id="cb3-3">                    cols, </span>
<span id="cb3-4">                    <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;">"name"</span>, </span>
<span id="cb3-5">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_prefix =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, </span>
<span id="cb3-6">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_sep =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>, </span>
<span id="cb3-7">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_pattern =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NULL</span>,</span>
<span id="cb3-8">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_ptypes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(),</span>
<span id="cb3-9">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_repair =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"check_unique"</span>,</span>
<span id="cb3-10">                    <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;">"value"</span>,</span>
<span id="cb3-11">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_drop_na =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">FALSE</span>,</span>
<span id="cb3-12">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_ptypes =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>())</span></code></pre></div>
</div>
<p>As you can see, only <code>data</code> and <code>cols</code> are effectively needed, so let’s try that. As I want to exclude the Patient-ID from pivoting, I remove this line from pivoting:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># as with all the tidyverse functions you can easily pipe-in the data as </span></span>
<span id="cb4-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the first argument</span></span>
<span id="cb4-3"></span>
<span id="cb4-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># '-ID' means, that all variables are used other than ID</span></span>
<span id="cb4-5">patient_data <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb4-6">  <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;">cols =</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-</span>ID)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 18 × 3
      ID name             value 
   &lt;dbl&gt; &lt;chr&gt;            &lt;chr&gt; 
 1     1 Hosp_Place_1     London
 2     1 Hosp_Outcome_1   Alive 
 3     1 Hosp_Place_2_2   Paris 
 4     1 Hosp_Outcome_2_2 alive 
 5     1 Hosp_Place_2_3   Rome  
 6     1 Hosp_Outcome_2_3 dead  
 7     2 Hosp_Place_1     Paris 
 8     2 Hosp_Outcome_1   alive 
 9     2 Hosp_Place_2_2   Rome  
10     2 Hosp_Outcome_2_2 alive 
11     2 Hosp_Place_2_3   London
12     2 Hosp_Outcome_2_3 alive 
13     3 Hosp_Place_1     Berne 
14     3 Hosp_Outcome_1   dead  
15     3 Hosp_Place_2_2   &lt;NA&gt;  
16     3 Hosp_Outcome_2_2 &lt;NA&gt;  
17     3 Hosp_Place_2_3   &lt;NA&gt;  
18     3 Hosp_Outcome_2_3 &lt;NA&gt;  </code></pre>
</div>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># I could have put the following instead, meaning selecting the variables I want</span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># but it was generally shorter to drop just the ID</span></span>
<span id="cb6-3"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># patient_data %&gt;%</span></span>
<span id="cb6-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#   pivot_longer(cols = Hosp_Place_1:Hosp_Outcome_2_3)</span></span></code></pre></div>
</div>
<p>Something clearly happened, but <code>name</code> and <code>value</code> are not exactly what we want here.<br>
The colnames “name” and “value” are actually coming from the default arguments.<br>
What is now the next step?</p>
</section>
<section id="mental-image-of-desired-outcome---how-should-my-dataframe-look-like" class="level1">
<h1>Mental image of desired outcome - How should my dataframe look like?</h1>
<p>My desired output is a dataframe with the colum names <code>ID</code>, <code>Hosp_Place</code> and <code>Hosp_Outcome</code>. Additionally, I want a variable - lets call it <code>hosp_sequence</code> - that captures the number of the hospitalisation (you remember the suffix of the original variable names).</p>
<p><code>tidyr::pivot_longer()</code>’s <code>names_to =</code>-arguments states in the help-page:<br>
<em>Can be a character vector, creating multiple columns, if names_sep or names_pattern is provided.</em></p>
<p>If you can detect any patterns in the column names, its possible to use them for the column naming. If we look at Hosp_Place_1 and Hosp_Outcome_1 we can clearly see a pattern: The information I want as name is <code>Hosp_Place</code> and <code>Hosp_Outcome</code> and the number followed should be put in variable called <code>hosp_sequence</code>.</p>
<p>This translates to something like <code>(Hosp_Place)_(1)</code> where the parts in brackets correspond to the inputs given in <code>names_to =</code>. With the <code>.value</code>-argument, I can easily take over the string as it is.</p>
<p>I actually found this very confusing (honestly - still do…) and I had great help for defining the regular expression from R4DS<sup>1</sup>.</p>
<p>Finally, this is the code we need:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># this code should do the trick</span></span>
<span id="cb7-2">patient_data <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;">pivot_longer</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>ID, </span>
<span id="cb7-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</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;">".value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hosp_sequence"</span>),</span>
<span id="cb7-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(^[A-z]+_[A-z]+)_([0-9].*)'</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 9 × 4
     ID hosp_sequence Hosp_Place Hosp_Outcome
  &lt;dbl&gt; &lt;chr&gt;         &lt;chr&gt;      &lt;chr&gt;       
1     1 1             London     Alive       
2     1 2_2           Paris      alive       
3     1 2_3           Rome       dead        
4     2 1             Paris      alive       
5     2 2_2           Rome       alive       
6     2 2_3           London     alive       
7     3 1             Berne      dead        
8     3 2_2           &lt;NA&gt;       &lt;NA&gt;        
9     3 2_3           &lt;NA&gt;       &lt;NA&gt;        </code></pre>
</div>
</div>
<p>Wait, what is happening with rows 8 and 9? This is my deceased Patient No.&nbsp;3 an those are not hospitalisations anymore - how do I drop those rows?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># you can either use dplyr::drop_na() or specify the built-in argument to TRUE</span></span>
<span id="cb9-2">patient_data <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;">pivot_longer</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>ID, </span>
<span id="cb9-4">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_to =</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;">".value"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hosp_sequence"</span>),</span>
<span id="cb9-5">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">names_pattern =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'(^[A-z]+_[A-z]+)_([0-9].*)'</span>, </span>
<span id="cb9-6">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values_drop_na =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span>)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 4
     ID hosp_sequence Hosp_Place Hosp_Outcome
  &lt;dbl&gt; &lt;chr&gt;         &lt;chr&gt;      &lt;chr&gt;       
1     1 1             London     Alive       
2     1 2_2           Paris      alive       
3     1 2_3           Rome       dead        
4     2 1             Paris      alive       
5     2 2_2           Rome       alive       
6     2 2_3           London     alive       
7     3 1             Berne      dead        </code></pre>
</div>
</div>
</section>
<section id="comment" class="level1">
<h1>Comment</h1>
<p>I wrote this blogpost after after solving exactly this issue with a real dataset for a colleague.<br>
My work as a research programmer allows me to dive into data wrangling problems on a regular basis. As I learned most of my R skills from other blogposts from the fantastic R community, I started to write up some of the problems I encountered for others. I also use my previous blogposts sometimes, when I have to dig up old code…</p>
<p>Any comments from your side? Let me know!</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>https://rfordatascience.slack.com/archives/C8K09CDNZ/p1584129595187200 If you are not already on this slack - sign up for it! Its just so great, low key help and great learning opportunities to just dive through the topics. ↩︎</p></li>
</ol>
</section></div> ]]></description>
  <guid>https://cathblatter.netlify.app/posts/2020/2020-03-16-using-pivot-longer-and-regex-for-data-wrangling.html</guid>
  <pubDate>Mon, 16 Mar 2020 00:00:00 GMT</pubDate>
</item>
<item>
  <title>Using purrr::map() to identify available datasets in a list</title>
  <dc:creator>Cath Blatter</dc:creator>
  <link>https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map.html</link>
  <description><![CDATA[ 




<section id="what-happened" class="level1">
<h1>What happened?</h1>
<p>Recently I wanted to explore plotting R for the first time and discovered the <a href="https://cran.r-project.org/web/packages/ggswissmaps/ggswissmaps.pdf">ggswissmaps-package</a>.</p>
<p>I’m new to the structure of geospatial data so I read the introductory vignette and followed the examples.</p>
<section id="examples-from-the-ggswissmaps-package" class="level3">
<h3 class="anchored" data-anchor-id="examples-from-the-ggswissmaps-package">Examples from the ggswissmaps-package</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#install.packages("ggswissmaps")</span></span>
<span id="cb1-2"></span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(ggswissmaps))</span>
<span id="cb1-4"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">suppressPackageStartupMessages</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse))</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Data frame with the coordinates of all swiss districts</span></span>
<span id="cb1-7">d <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> shp_df[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1b15"</span>]]</span>
<span id="cb1-8"></span>
<span id="cb1-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Look at the structure of the data frame</span></span>
<span id="cb1-10"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">glimpse</span>(d)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Rows: 19,502
Columns: 21
$ long    &lt;int&gt; 679207, 680062, 679981, 680365, 680281, 680479, 680717, 681021…
$ lat     &lt;int&gt; 245176, 244294, 244051, 243411, 241866, 241584, 240695, 240306…
$ order   &lt;int&gt; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,…
$ hole    &lt;lgl&gt; FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,…
$ piece   &lt;fct&gt; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
$ group   &lt;fct&gt; 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.…
$ id      &lt;chr&gt; "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0…
$ BZNR    &lt;int&gt; 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 10…
$ KTNR    &lt;int&gt; 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
$ GRNR    &lt;int&gt; 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,…
$ AREA_HA &lt;int&gt; 11303, 11303, 11303, 11303, 11303, 11303, 11303, 11303, 11303,…
$ X_MIN   &lt;int&gt; 671862, 671862, 671862, 671862, 671862, 671862, 671862, 671862…
$ X_MAX   &lt;int&gt; 686462, 686462, 686462, 686462, 686462, 686462, 686462, 686462…
$ Y_MIN   &lt;int&gt; 229137, 229137, 229137, 229137, 229137, 229137, 229137, 229137…
$ Y_MAX   &lt;int&gt; 245396, 245396, 245396, 245396, 245396, 245396, 245396, 245396…
$ X_CNTR  &lt;int&gt; 678300, 678300, 678300, 678300, 678300, 678300, 678300, 678300…
$ Y_CNTR  &lt;int&gt; 235900, 235900, 235900, 235900, 235900, 235900, 235900, 235900…
$ Z_MIN   &lt;int&gt; 380, 380, 380, 380, 380, 380, 380, 380, 380, 380, 380, 380, 38…
$ Z_MAX   &lt;int&gt; 914, 914, 914, 914, 914, 914, 914, 914, 914, 914, 914, 914, 91…
$ Z_AVG   &lt;int&gt; 561, 561, 561, 561, 561, 561, 561, 561, 561, 561, 561, 561, 56…
$ Z_MED   &lt;int&gt; 557, 557, 557, 557, 557, 557, 557, 557, 557, 557, 557, 557, 55…</code></pre>
</div>
<div class="sourceCode cell-code" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># The cantons are identified by the KTNR column</span></span>
<span id="cb3-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Extract from this data the districts of two cantons (18 = Graubünden, 21 = Ticino)</span></span>
<span id="cb3-3"></span>
<span id="cb3-4">two_cantons <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> d <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>(KTNR  <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">21</span>))</span>
<span id="cb3-5"></span>
<span id="cb3-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># And draw the map</span></span>
<span id="cb3-7"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">maps2_</span>(two_cantons)</span></code></pre></div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-1-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>This worked quite fine - but I was more interested in plotting by language region, so I did the following:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add an aditional variable "region"</span></span>
<span id="cb4-2">d <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;">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;">case_when</span>(KTNR <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;">21</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ticino"</span>,</span>
<span id="cb4-4">                                 KTNR  <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</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;">26</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Romandie"</span>,</span>
<span id="cb4-5">                                 <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deutschschweiz"</span>)) <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">-&gt;</span> d</span>
<span id="cb4-6"></span>
<span id="cb4-7"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># draw a ggplot </span></span>
<span id="cb4-8"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(d, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(region)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</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;">"Region"</span>,</span>
<span id="cb4-11">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ticino"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>,</span>
<span id="cb4-12">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Romandie"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#b2df8a"</span>,</span>
<span id="cb4-13">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deutschschweiz"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a6cee3"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-14">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb4-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="troubleshooting" class="level3">
<h3 class="anchored" data-anchor-id="troubleshooting">Troubleshooting</h3>
<p>What is the problem with this plot (aside from the ugly black color of the boundaries)? The boundaries correspond to district, not cantonal level (what I wanted). As the variable <code>KTNR</code> identifies the cantons, I did the following:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># draw a ggplot and changed to aes(....group = KTNR)</span></span>
<span id="cb5-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(d, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> KTNR)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(region)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"black"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</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;">"Region"</span>,</span>
<span id="cb5-5">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ticino"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>,</span>
<span id="cb5-6">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Romandie"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#b2df8a"</span>,</span>
<span id="cb5-7">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deutschschweiz"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a6cee3"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb5-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-3-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>…It got even worse…</p>
<p>I did not understand why at first. After a brief online exchange in the Rladies-Slack (sidenote: fantastic place to learn! 😎), I realized that my problem is with the data: <code>aes(x = long, y = lat)</code> in my dataframe correspond to district level not cantonal level. Unfortunately, the link on the website of the Federal Office of Statistics didn’t work either<sup>1</sup>, so what to do else?</p>
<p>Look at the data structure:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># a list containing 8 elements (dataframes)</span></span>
<span id="cb6-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># which each contain lon/lat on different levels</span></span>
<span id="cb6-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(shp_df)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "list"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># very long output, not shown here</span></span>
<span id="cb8-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#str(shp_df)</span></span></code></pre></div>
</div>
<p>I now knew that I could use the plot from above and check each element of the list - either by hand (corresponding to seven times copy/paste) or with <code>purrr::map()</code>!</p>
</section>
<section id="solution" class="level3">
<h3 class="anchored" data-anchor-id="solution">Solution</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># create a function to map over each element of the list to</span></span>
<span id="cb9-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># identify if one is on cantonal-level</span></span>
<span id="cb9-3">my_plot <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="cf" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(data){</span>
<span id="cb9-4">  </span>
<span id="cb9-5">  ggplot2<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;">ggplot</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> data, ggplot2<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;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-6">    ggplot2<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;">geom_polygon</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-7">    ggplot2<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;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb9-8">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span>
<span id="cb9-9">  </span>
<span id="cb9-10">}</span></code></pre></div>
</div>
<p>Then do the magic (1 simple line of code will give me 8 plots!):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># map it over the list</span></span>
<span id="cb10-2">purrr<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;">map</span>(ggswissmaps<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>shp_df, <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;">my_plot</span>(.x))</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>$g1b15</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$g1g15_encl</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-2.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$g1g15_li</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-3.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$g1g15</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-4.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$g1k15</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-5.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$g1l15</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-6.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$g1r15</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-7.png" class="img-fluid" width="672"></p>
</div>
<div class="cell-output cell-output-stdout">
<pre><code>
$g1s15</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-6-8.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>From the plots above I now know which dataframe to use:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># solution: the second last element of the list is what I need</span></span>
<span id="cb19-2">cant_level <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> </span>
<span id="cb19-3">  ggswissmaps<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">::</span>shp_df<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span>g1k15 <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%&gt;%</span> </span>
<span id="cb19-4">      <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;">case_when</span>(KTNR <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;">21</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ticino"</span>,</span>
<span id="cb19-5">                                KTNR  <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="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">22</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">23</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">24</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;">26</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Romandie"</span>,</span>
<span id="cb19-6">                                <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">TRUE</span> <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">~</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deutschschweiz"</span>))</span>
<span id="cb19-7"></span>
<span id="cb19-8"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># draw the prettier graph</span></span>
<span id="cb19-9"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(cant_level, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(region))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-11">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</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;">"Region"</span>,</span>
<span id="cb19-12">                   <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ticino"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>,</span>
<span id="cb19-13">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Romandie"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#b2df8a"</span>,</span>
<span id="cb19-14">                              <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deutschschweiz"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a6cee3"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-15">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-16">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb19-17">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>()</span></code></pre></div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>The output is what I wanted - altough we could improve it, as the lakes (which are left out in the district-level data) are not shown here…</p>
<p><em>Edit:</em></p>
<p>thanks to the <code>ggswissmaps</code>-maintainer <a href="https://github.com/gibonet">gibo’s</a> help, the maps look now much prettier:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(cant_level, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-2">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(region)), <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"white"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> <span class="fl" style="color: #AD0000;
background-color: null;
font-style: inherit;">0.1</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_fill_manual</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;">"Region"</span>,</span>
<span id="cb20-4">                    <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">values =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">c</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Ticino"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"grey90"</span>,</span>
<span id="cb20-5">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Romandie"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#b2df8a"</span>,</span>
<span id="cb20-6">                               <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Deutschschweiz"</span> <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#a6cee3"</span>)) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_void</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">legend.position =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"bottom"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb20-9">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">coord_equal</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span> <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># add a layer for the lakes</span></span>
<span id="cb20-10">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_polygon</span>(<span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> long, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> lat, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">group =</span> group), </span>
<span id="cb20-11">               <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">data =</span> shp_df[[<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"g1s15"</span>]], <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">fill =</span> <span class="cn" style="color: #8f5902;
background-color: null;
font-style: inherit;">NA</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">alpha =</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;">color =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"#0529B3"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">size =</span> .<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lty =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"solid"</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
</div>
</div>


</section>
</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>In the meantime, the package maintainer provided me with the working links: https://www.bfs.admin.ch/bfs/fr/home/services/geostat/geodonnees-statistique-federale.html.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>purrr</category>
  <guid>https://cathblatter.netlify.app/posts/2019/2019-11-28-Using_purrr_map.html</guid>
  <pubDate>Thu, 28 Nov 2019 00:00:00 GMT</pubDate>
  <media:content url="https://cathblatter.netlify.app/posts/2019/plot-fail.png" medium="image" type="image/png" height="144" width="144"/>
</item>
<item>
  <title>Getting PubMed metadata with RISmed directly into RStudio</title>
  <dc:creator>Catherine Blatter</dc:creator>
  <link>https://cathblatter.netlify.app/posts/2019/2019-04-29-getting-pubmed-metadata-from-within-rstudio.html</link>
  <description><![CDATA[ 




<section id="what-is-this-post-about" class="level2">
<h2 class="anchored" data-anchor-id="what-is-this-post-about">What is this post about?</h2>
<ul>
<li><p>For my master’s thesis (stay tuned 😎) I came at a point where I was interested in having a closer look at publication meta data (e.g.&nbsp;publications per year)</p></li>
<li><p>PubMed usually does provide this service when you execute your search, for some reason it failed for my query</p></li>
<li><p>Being taught basic statistics I was determined to find a way to create the image myself… and came across the amazing RISmed-📦</p></li>
</ul>
<p>➡️ This blog post highlights how I used the RISmed package and gives short code examples</p>
</section>
<section id="introduction---what-were-the-problems-i-faced" class="level2">
<h2 class="anchored" data-anchor-id="introduction---what-were-the-problems-i-faced">Introduction - what were the problems I faced?</h2>
<p>The topic of my master’s thesis - <em>unfinished / missed / rationed nursing care</em> - is an important topic in health services research with growing numbers of publications over the last years. Altough I did not have to use R extensively during my thesis, in one specific case I greatly profited from it:</p>
<p>Usually, PubMed provides a small graphical display of the yearly publication count on each query as shown in the image below:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://cathblatter.netlify.app/posts/2019/pubmed_yearcount.png" class="img-fluid figure-img"></p>
<figcaption class="figure-caption">PubMed year count</figcaption>
</figure>
</div>
<p>For some reason it failed for my exact search query. This was especially bad because I had to make a somehow valid statement that the publication numbers were increasing over the last years. If I only could get all meta data (i.e.&nbsp;years) from my query (~900) into a spreadsheet…<sup>1</sup></p>
<p>At some point I realized that the PubMed database should be accessible and I was sure that R has a solution to it - that’s how I found the <a href="https://cran.r-project.org/web/packages/RISmed/RISmed.pdf">RISmed-package</a>.</p>
</section>
<section id="rismed-package" class="level2">
<h2 class="anchored" data-anchor-id="rismed-package">RISmed package</h2>
<p>In short: the RISmed package provides functions to extract bibliographic information from<br>
the NCBI databases. In other words: you can extract the information from PubMed through R code - exactly what I needed!</p>
<section id="how-to-use-rismed" class="level3">
<h3 class="anchored" data-anchor-id="how-to-use-rismed">How to use RISmed?</h3>
<p>First you need to install <code>install.packages("RISmed")</code> or load the package:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># load necessary packages</span></span>
<span id="cb1-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(tidyverse)</span>
<span id="cb1-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">library</span>(RISmed)</span></code></pre></div>
</div>
<p>Then you specify the search_topic and search_query:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># build the search topic</span></span>
<span id="cb2-2">search_topic <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;">'missed care [TI] OR implicit rationing [TI] OR unfinished care [TI]'</span></span>
<span id="cb2-3"></span>
<span id="cb2-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># the EUtilsSummary()-function gets information on your search results</span></span>
<span id="cb2-5"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># (maximum results set as 1000, years between 1980 and 2019)</span></span>
<span id="cb2-6">search_query <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;">EUtilsSummary</span>(search_topic, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">retmax=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1000</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">mindate=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1980</span>,<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">maxdate=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">2019</span>)</span></code></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning in any(is.na(WhichArgs)) || sapply(WhichArgs, length) &gt; 1: 'length(x) =
3 &gt; 1' in coercion to 'logical(1)'</code></pre>
</div>
</div>
<p><code>summary()</code> and <code>QueryId</code> get you quick overview of your results:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Printing the summary displays the actual results that are available</span></span>
<span id="cb4-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">summary</span>(search_query)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Query:
("missed care"[Title] OR "implicit rationing"[Title] OR "unfinished care"[Title]) AND 1980/01/01:2019/12/31[Date - Entry] 

Result count:  66</code></pre>
</div>
<div class="sourceCode cell-code" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># see the PMIIDs of our returned query</span></span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">QueryId</span>(search_query)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "31883182" "31850645" "31845542" "31773131" "31680364" "31596988"
 [7] "31583822" "31529752" "31334567" "31305955" "30937945" "30866129"
[13] "30686577" "30681485" "30589706" "30475323" "30303195" "30303194"
[19] "30303193" "30145041" "30095046" "29791753" "29655017" "29569380"
[25] "29554590" "29471742" "29281908" "28978300" "28971888" "28960457"
[31] "28844649" "28599196" "27556651" "27492884" "27322941" "27292861"
[37] "27234155" "29718634" "26947419" "26259338" "26032730" "25794946"
[43] "25563066" "25430513" "24681453" "24681452" "24481345" "24397252"
[49] "24248553" "23991529" "23334645" "23084600" "22674763" "20661063"
[55] "19590471" "18055900" "16323493" "11467274" "10862990" "9694173" 
[61] "10173241" "8611380"  "7549650"  "7549649"  "7795458"  "8332926" </code></pre>
</div>
</div>
<p>If you think your query gets you a reasonable number of results you can then further information on your records (i.e.&nbsp;dowloading the information):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># get the actual data from PubMed and store them in an object called records</span></span>
<span id="cb8-2">records <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;">EUtilsGet</span>(search_query, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">type =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"efetch"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">db =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"pubmed"</span>)</span>
<span id="cb8-3"></span>
<span id="cb8-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># check what kind of object "records" is</span></span>
<span id="cb8-5"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">class</span>(records)</span></code></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Medline"
attr(,"package")
[1] "RISmed"</code></pre>
</div>
</div>
<p>For creating your final dataframe to analyse the data, you need to extract information as requested. You can choose either dataframe or list to store the results. Careful: If you extract <code>Author()</code> you need to choose list, otherwise the variable is dropped.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># store it in either list or dataframe (Author() returns a list, therefore </span></span>
<span id="cb10-2"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># list chosen here)</span></span>
<span id="cb10-3">pubmed_data_list <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">list</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Title'</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;">ArticleTitle</span>(records), </span>
<span id="cb10-4">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Authors'</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;">Author</span>(records), </span>
<span id="cb10-5">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Year'</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;">YearPubmed</span>(records), </span>
<span id="cb10-6">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Journal'</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;">Title</span>(records),</span>
<span id="cb10-7">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'PublicationType'</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;">PublicationType</span>(records),</span>
<span id="cb10-8">                         <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Language'</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;">Language</span>(records))</span>
<span id="cb10-9"></span>
<span id="cb10-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># version without Authors for dataframe</span></span>
<span id="cb10-11">pubmed_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 class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Title'</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;">ArticleTitle</span>(records),</span>
<span id="cb10-12">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Year'</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;">YearPubmed</span>(records), </span>
<span id="cb10-13">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Journal'</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;">Title</span>(records),</span>
<span id="cb10-14">                    <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">'Language'</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;">Language</span>(records))</span></code></pre></div>
</div>
<p>Finally, I could visually explore the year count:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># plot the yearcount</span></span>
<span id="cb11-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ggplot</span>(pubmed_data, <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">aes</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">factor</span>(Year))) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-3">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">geom_bar</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-4">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">scale_y_discrete</span>() <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-5">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">labs</span>(<span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">x =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Year"</span>,</span>
<span id="cb11-6">       <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">y =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"No. of publications"</span>) <span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">+</span></span>
<span id="cb11-7">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">theme_classic</span>()</span></code></pre></div>
<div class="cell-output-display">
<p><img src="https://cathblatter.netlify.app/posts/2019/2019-04-29-getting-pubmed-metadata-from-within-rstudio_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
</section>
<section id="how-could-my-thesis-profit-from-the-rismed-package" class="level2">
<h2 class="anchored" data-anchor-id="how-could-my-thesis-profit-from-the-rismed-package">How could my thesis profit from the RISmed package?</h2>
<p>The figure I created<sup>2</sup> helped me visualize meta information on the publications: I could clearly see that the topic <em>unfinished nursing care</em> has been mentioned with increasing tendency in publications. This gave me valid data for accepting my assumption.</p>
<p>Due to the nature of my thesis, I (sadly) didn’t have to use the exact numbers. Yet, I still learned two useful things 💡:</p>
<ol type="1">
<li><p>Using bibliographic information to inform your research may be helpful and should not be forgotten.</p></li>
<li><p>I felt like I reached a whole new level of R’s numerous capabilities: Using an R package to connect to a web-database should not be the end of my discoveries 😃</p></li>
</ol>
<p><em>Notes on this post</em>:</p>
<ul>
<li><p>Be careful with the search query - there can be differences in using different codes, see here: https://stackoverflow.com/questions/32994991/r-package-rismed-different-results-using-the-eutilsget-or-summary-function</p></li>
<li><p>Dowloading information from PubMed (<code>EUtilsGet()</code>) can take a while</p></li>
<li><p>I had some trouble with long search terms (no further explanation found)</p></li>
</ul>
<section id="other-resources-i-used" class="level4">
<h4 class="anchored" data-anchor-id="other-resources-i-used">Other resources I used</h4>
<ul>
<li>https://datascienceplus.com/search-pubmed-with-rismed/<br>
</li>
<li>https://www.r-bloggers.com/how-to-search-pubmed-with-rismed-package-in-r/<br>
</li>
<li>https://www.rdocumentation.org/packages/RISmed/versions/2.1.4</li>
</ul>


</section>
</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>Actually, PubMed does provide this service in a user friendly way: Execute your search query, choose the results you want, then go to <em>Send to</em> -&gt; <em>File</em> and you get a .csv-file.<br>
But hey, who does it this way when you can extract it with an R package?↩︎</p></li>
<li id="fn2"><p>(not the figure above, as the sample string I used in this post was not as comprehensive as the full search string used in my thesis)↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>RISmed</category>
  <guid>https://cathblatter.netlify.app/posts/2019/2019-04-29-getting-pubmed-metadata-from-within-rstudio.html</guid>
  <pubDate>Mon, 29 Apr 2019 00:00:00 GMT</pubDate>
  <media:content url="https://cathblatter.netlify.app/posts/2019/pubmed_yearcount.png" medium="image" type="image/png" height="82" width="144"/>
</item>
</channel>
</rss>
