<|start_filename|>docs/index.html<|end_filename|>
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Handle Data with Messy Header Rows and Broken Values • unheadr</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="apple-touch-icon-60x60.png">
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/spacelab/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="bootstrap-toc.css">
<script src="bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="pkgdown.css" rel="stylesheet">
<script src="pkgdown.js"></script><meta property="og:title" content="Handle Data with Messy Header Rows and Broken Values">
<meta property="og:description" content="Verb-like functions to work with messy data, often derived from 
             spreadsheets or parsed PDF tables. Includes functions for unwrapping 
             values broken up across rows, relocating embedded grouping values, 
             and to annotate meaningful formatting in spreadsheet files.">
<meta property="og:image" content="unheadr.liomys.mx/logo.png">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
    <div class="container template-home">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="index.html">unheadr</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.3.1</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="index.html">
    <span class="fas fa fas fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li>
      <a href="articles/unheadr-vignette.html">Introduction to unheadr</a>
    </li>
  </ul>
</li>
<li>
  <a href="news/index.html">Changelog</a>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right">
<li>
  <a href="https://github.com/luisDVA/unheadr">
    <span class="fab fa fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
</div>
<!--/.nav-collapse -->
  </div>
<!--/.container -->
</div>
<!--/.navbar -->

      

      </header><div class="row">
  <div class="contents col-md-9">
<div id="unheadr-" class="section level1">
<div class="page-header"><h1 class="hasAnchor">
<a href="#unheadr-" class="anchor"></a>unheadr <img src="reference/figures/logosmall.png" align="right">
</h1></div>
<!-- badges: start -->

<p>The goal of <code>unheadr</code> is to help wrangle data when it has embedded subheaders, or when values are wrapped across several rows. <a href="https://unheadr.liomys.mx/" class="uri">https://unheadr.liomys.mx/</a></p>
<div id="installation" class="section level2">
<h2 class="hasAnchor">
<a href="#installation" class="anchor"></a>Installation</h2>
<p>You can install the CRAN release or the development version with:</p>
<div class="sourceCode" id="cb1"><pre class="r"><span class="co"># Install unheadr from CRAN:</span>
<span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span>(<span class="st">"unheadr"</span>)

<span class="co"># Or install the development version from GitHub with:</span>
<span class="co"># install.packages("remotes")</span>
<span class="kw pkg">remotes</span><span class="kw ns">::</span><span class="fu"><a href="https://remotes.r-lib.org/reference/install_github.html">install_github</a></span>(<span class="st">"luisDVA/unheadr"</span>)</pre></div>
<p>The reasoning behind the package and some of the possible uses of <code>unheadr</code> are described in this publication:</p>
<p><NAME>., <NAME>., <NAME>. (2018). Good practices for sharing analysis-ready data in mammalogy and biodiversity research. <em>Hystrix, the Italian Journal of Mammalogy</em>, 29(2), 155-161. <a href="https://doi.org/10.4404/hystrix-00133-2018">Open Access, DOI 10.4404/hystrix-00133-2018</a></p>
</div>
<div id="usage" class="section level2">
<h2 class="hasAnchor">
<a href="#usage" class="anchor"></a>Usage</h2>
<p>Load the package first.</p>
<div class="sourceCode" id="cb2"><pre class="r"><span class="fu"><a href="https://rdrr.io/r/base/library.html">library</a></span>(<span class="no">unheadr</span>)</pre></div>
<div id="main-functions" class="section level3">
<h3 class="hasAnchor">
<a href="#main-functions" class="anchor"></a>Main functions</h3>
<p><strong><code><a href="reference/untangle2.html">untangle2()</a></code></strong></p>
<p><code><a href="reference/untangle2.html">untangle2()</a></code> puts embedded subheaders into their own variable, using regular expressions to identify them.</p>
<p>In the data below (a subset of a bundled dataset which can be loaded with <code><a href="https://rdrr.io/r/utils/data.html">data(primates2017)</a></code>), there are rows that correspond to values in grouping variables. These should be in their own column. Instead, they are embedded within the data rectangle. This is a common practice in many disciplines. This data presentation looks OK and is easy to read, but hard to work with (for example: calculating group-wise summaries).</p>
<p>In this example, values for an implicit “geographic region” variable and an implicit “taxonomic family” variable are embedded in the column that contains the observational units (the scientific names of various primates).</p>
<table class="table">
<thead><tr class="header">
<th align="left">scientific_name</th>
<th align="left">common_name</th>
<th align="left">red_list_status</th>
<th align="right">mass_kg</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="left">Asia</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="right">NA</td>
</tr>
<tr class="even">
<td align="left">CERCOPITHECIDAE</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="right">NA</td>
</tr>
<tr class="odd">
<td align="left">Trachypithecus obscurus</td>
<td align="left">Dusky Langur</td>
<td align="left">NT</td>
<td align="right">7.13</td>
</tr>
<tr class="even">
<td align="left">Presbytis sumatra</td>
<td align="left">Black Sumatran Langur</td>
<td align="left">EN</td>
<td align="right">6.00</td>
</tr>
<tr class="odd">
<td align="left">Rhinopithecus roxellana</td>
<td align="left">Golden Snub-nosed Monkey</td>
<td align="left">EN</td>
<td align="right">NA</td>
</tr>
<tr class="even">
<td align="left">HYLOBATIDAE</td>
<td align="left">NA</td>
<td align="left">NA</td>
<td align="right">NA</td>
</tr>
<tr class="odd">
<td align="left">Hylobates funereus</td>
<td align="left">East Bornean Gray Gibbon</td>
<td align="left">EN</td>
<td align="right">NA</td>
</tr>
<tr class="even">
<td align="left">Hylobates klossii</td>
<td align="left">Kloss’s Gibbon</td>
<td align="left">EN</td>
<td align="right">5.80</td>
</tr>
<tr class="odd">
<td align="left">Nomascus concolor</td>
<td align="left">Western Black Crested Gibbon</td>
<td align="left">CR</td>
<td align="right">7.71</td>
</tr>
</tbody>
</table>
<p>For a tidier structure, the subheaders embedded in the <em>scientific_name</em> column need to be plucked out and placed in their own variable. This was initially the main objective of <code>unheadr</code> and what <code><a href="reference/untangle2.html">untangle2()</a></code> was made for. The function can be used with <code>magrittr</code> pipes as a <code>dplyr</code>-type verb.</p>
<p>If these subheaders can be matched in bulk with a regular expression because they share a prefix, suffix, or anything in common, we can save a lot of time. Otherwise, they can be matched by name. For more details, see the examples and vignette.</p>
<p>The ‘untangled’ version of the data:</p>
<table class="table">
<thead><tr class="header">
<th align="left">scientific_name</th>
<th align="left">common_name</th>
<th align="left">red_list_status</th>
<th align="right">mass_kg</th>
<th align="left">family</th>
<th align="left">region</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="left">Trachypithecus obscurus</td>
<td align="left">Dusky Langur</td>
<td align="left">NT</td>
<td align="right">7.13</td>
<td align="left">CERCOPITHECIDAE</td>
<td align="left">Asia</td>
</tr>
<tr class="even">
<td align="left">Presbytis sumatra</td>
<td align="left">Black Sumatran Langur</td>
<td align="left">EN</td>
<td align="right">6.00</td>
<td align="left">CERCOPITHECIDAE</td>
<td align="left">Asia</td>
</tr>
<tr class="odd">
<td align="left">Rhinopithecus roxellana</td>
<td align="left">Golden Snub-nosed Monkey</td>
<td align="left">EN</td>
<td align="right">NA</td>
<td align="left">CERCOPITHECIDAE</td>
<td align="left">Asia</td>
</tr>
<tr class="even">
<td align="left">Hylobates funereus</td>
<td align="left">East Bornean Gray Gibbon</td>
<td align="left">EN</td>
<td align="right">NA</td>
<td align="left">HYLOBATIDAE</td>
<td align="left">Asia</td>
</tr>
<tr class="odd">
<td align="left">Hylobates klossii</td>
<td align="left">Kloss’s Gibbon</td>
<td align="left">EN</td>
<td align="right">5.80</td>
<td align="left">HYLOBATIDAE</td>
<td align="left">Asia</td>
</tr>
<tr class="even">
<td align="left">Nomascus concolor</td>
<td align="left">Western Black Crested Gibbon</td>
<td align="left">CR</td>
<td align="right">7.71</td>
<td align="left">HYLOBATIDAE</td>
<td align="left">Asia</td>
</tr>
</tbody>
</table>
<p>Now we can easily perform grouping operations and summarize the data (e.g. calculating average body mass by Family).</p>
<p><strong><code><a href="reference/unbreak_vals.html">unbreak_vals()</a></code></strong></p>
<p>This function uses regex to fix values that are broken across two rows. This usually happens when we are formatting a table and we need to fit it on a page.</p>
<div class="sourceCode" id="cb3"><pre class="r"><span class="co"># Set up a toy dataset</span>
<span class="no">dogsDesc</span> <span class="kw">&lt;-</span>
  <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
    <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
    <span class="kw">dogs</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"Retriever"</span>, <span class="st">"(Golden)"</span>,
      <span class="st">"Retriever"</span>, <span class="st">"(Labrador)"</span>, <span class="st">"Bulldog"</span>, <span class="st">"(French)"</span>
    ),
    <span class="kw">coat</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"long"</span>, <span class="fl">NA</span>, <span class="st">"short"</span>, <span class="fl">NA</span>, <span class="st">"short"</span>, <span class="fl">NA</span>)
  )

<span class="no">dogsDesc</span>
<span class="co">#&gt;         dogs  coat</span>
<span class="co">#&gt; 1  Retriever  long</span>
<span class="co">#&gt; 2   (Golden)  &lt;NA&gt;</span>
<span class="co">#&gt; 3  Retriever short</span>
<span class="co">#&gt; 4 (Labrador)  &lt;NA&gt;</span>
<span class="co">#&gt; 5    Bulldog short</span>
<span class="co">#&gt; 6   (French)  &lt;NA&gt;</span></pre></div>
<p>We can match the opening brackets with regex.</p>
<div class="sourceCode" id="cb4"><pre class="r"><span class="fu"><a href="reference/unbreak_vals.html">unbreak_vals</a></span>(<span class="kw">df</span> <span class="kw">=</span> <span class="no">dogsDesc</span>, <span class="kw">regex</span> <span class="kw">=</span> <span class="st">"^\\("</span>, <span class="kw">ogcol</span> <span class="kw">=</span> <span class="no">dogs</span>, <span class="kw">newcol</span> <span class="kw">=</span> <span class="no">dogs_unbroken</span>)
<span class="co">#&gt;          dogs_unbroken  coat</span>
<span class="co">#&gt; 1   Retriever (Golden)  long</span>
<span class="co">#&gt; 2 Retriever (Labrador) short</span>
<span class="co">#&gt; 3     Bulldog (French) short</span></pre></div>
<p><strong><code><a href="reference/unwrap_cols.html">unwrap_cols()</a></code></strong></p>
<p>Use this function to unwrap and glue values that have been wrapped across multiple rows for presentation purposes, with an inconsistent number of empty or <code>NA</code> values padding out the columns.</p>
<div class="sourceCode" id="cb5"><pre class="r"><span class="co"># Set up the data</span>
<span class="no">nyk</span> <span class="kw">&lt;-</span>
  <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
    <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
    <span class="kw">player</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"<NAME>"</span>, <span class="fl">NA</span>, <span class="fl">NA</span>,
      <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="st">"<NAME>"</span>, <span class="fl">NA</span>,
      <span class="st">"<NAME>"</span>, <span class="fl">NA</span>, <span class="fl">NA</span>
    ),
    <span class="kw">listed_height_m.</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="fl">2.11</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>,
      <span class="fl">NA</span>, <span class="fl">1.98</span>, <span class="fl">NA</span>, <span class="fl">1.96</span>, <span class="fl">NA</span>, <span class="fl">NA</span>
    ),
    <span class="kw">teams_chronological</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"Raptors"</span>, <span class="st">"Knicks"</span>,
      <span class="st">"Nuggets"</span>, <span class="st">"Clippers"</span>, <span class="st">"Trail Blazers"</span>,
      <span class="st">"Rockets"</span>, <span class="st">"Knicks"</span>, <span class="st">"Pistons"</span>,
      <span class="st">"Knicks"</span>, <span class="st">"Warriors"</span>, <span class="st">"Knicks"</span>,
      <span class="st">"Timberwolves"</span>
    ),
    <span class="kw">position</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"Power forward"</span>, <span class="st">"Center"</span>,
      <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>,
      <span class="st">"Shooting guard"</span>, <span class="fl">NA</span>, <span class="st">"Small forward"</span>, <span class="fl">NA</span>, <span class="fl">NA</span>
    )
  )
<span class="no">nyk</span>
<span class="co">#&gt;              player listed_height_m. teams_chronological       position</span>
<span class="co">#&gt; 1      <NAME>             2.11             Raptors  Power forward</span>
<span class="co">#&gt; 2              &lt;NA&gt;               NA              Knicks         Center</span>
<span class="co">#&gt; 3              &lt;NA&gt;               NA             Nuggets           &lt;NA&gt;</span>
<span class="co">#&gt; 4              &lt;NA&gt;               NA            Clippers           &lt;NA&gt;</span>
<span class="co">#&gt; 5              &lt;NA&gt;               NA       Trail Blazers           &lt;NA&gt;</span>
<span class="co">#&gt; 6              &lt;NA&gt;               NA             Rockets           &lt;NA&gt;</span>
<span class="co">#&gt; 7              &lt;NA&gt;               NA              Knicks           &lt;NA&gt;</span>
<span class="co">#&gt; 8     <NAME>             1.98             Pistons Shooting guard</span>
<span class="co">#&gt; 9              &lt;NA&gt;               NA              Knicks           &lt;NA&gt;</span>
<span class="co">#&gt; 10 Latrell Sprewell             1.96            Warriors  Small forward</span>
<span class="co">#&gt; 11             &lt;NA&gt;               NA              Knicks           &lt;NA&gt;</span>
<span class="co">#&gt; 12             &lt;NA&gt;               NA        Timberwolves           &lt;NA&gt;</span></pre></div>
<p>Unwrap the elements in the variable that defines the groups, separating with commas.</p>
<div class="sourceCode" id="cb6"><pre class="r"><span class="fu"><a href="reference/unwrap_cols.html">unwrap_cols</a></span>(<span class="no">nyk</span>, <span class="kw">groupingVar</span> <span class="kw">=</span> <span class="no">player</span>, <span class="kw">separator</span> <span class="kw">=</span> <span class="st">", "</span>)
<span class="co">#&gt; # A tibble: 3 x 4</span>
<span class="co">#&gt;   player     listed_height_m. teams_chronological                  position     </span>
<span class="co">#&gt;   &lt;chr&gt;      &lt;chr&gt;            &lt;chr&gt;                                &lt;chr&gt;        </span>
<span class="co">#&gt; 1 <NAME>… 2.11             Raptors, Knicks, Nuggets, Clippers,… Power forwar…</span>
<span class="co">#&gt; 2 <NAME>… 1.98             Pistons, Knicks                      Shooting gua…</span>
<span class="co">#&gt; 3 Latrell S… 1.96             Warriors, Knicks, Timberwolves       Small forward</span></pre></div>
<p><strong><code><a href="reference/unbreak_rows.html">unbreak_rows()</a></code></strong></p>
<p>This function merges sets of two contiguous rows upwards by pasting the values of the lagging row to the values of the leading row (identified using regular expressions).</p>
<p>The following table of basketball records has two sets of header rows with values broken across two contiguous rows.</p>
<div class="sourceCode" id="cb7"><pre class="r"><span class="no">bball</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
  <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
  <span class="kw">v1</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
    <span class="st">"Player"</span>, <span class="fl">NA</span>, <span class="st">"<NAME>"</span>, <span class="st">"<NAME>"</span>,
    <span class="st">"<NAME>"</span>, <span class="st">"Player"</span>,
    <span class="fl">NA</span>,
    <span class="st">"<NAME>"</span>,
    <span class="st">"<NAME>"</span>,
    <span class="st">"<NAME>"</span>
  ),
  <span class="kw">v2</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
    <span class="st">"Most points"</span>, <span class="st">"in a game"</span>, <span class="st">"55"</span>, <span class="st">"43"</span>, <span class="st">"41"</span>, <span class="st">"Most varsity"</span>,
    <span class="st">"games played"</span>, <span class="st">"111"</span>, <span class="st">"109"</span>,
    <span class="st">"104"</span>
  ),
  <span class="kw">v3</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
    <span class="st">"Season"</span>, <span class="st">"(year ending)"</span>, <span class="st">"2001"</span>, <span class="st">"2000"</span>, <span class="st">"2010"</span>, <span class="st">"Season"</span>,
    <span class="st">"(year ending)"</span>, <span class="st">"2005"</span>,
    <span class="st">"2004"</span>, <span class="st">"2002"</span>
  )
)</pre></div>
<p><code><a href="reference/unbreak_rows.html">unbreak_rows()</a></code> merges these rows if we can match them with a common pattern.</p>
<div class="sourceCode" id="cb8"><pre class="r"><span class="co"># Match with regex on variable v2</span>
<span class="fu"><a href="reference/unbreak_rows.html">unbreak_rows</a></span>(<span class="no">bball</span>, <span class="kw">regex</span> <span class="kw">=</span> <span class="st">"^Most"</span>, <span class="kw">ogcol</span> <span class="kw">=</span> <span class="no">v2</span>)
<span class="co">#&gt; 2 matches</span>
<span class="co">#&gt;                v1                        v2                   v3</span>
<span class="co">#&gt; 1          Player     Most points in a game Season (year ending)</span>
<span class="co">#&gt; 2 <NAME>                        55                 2001</span>
<span class="co">#&gt; 3     <NAME>                        43                 2000</span>
<span class="co">#&gt; 4  <NAME>                        41                 2010</span>
<span class="co">#&gt; 5          Player Most varsity games played Season (year ending)</span>
<span class="co">#&gt; 6  <NAME>                       111                 2005</span>
<span class="co">#&gt; 7 <NAME>                       109                 2004</span>
<span class="co">#&gt; 8     <NAME>                       104                 2002</span></pre></div>
<p><strong><code><a href="reference/mash_colnames.html">mash_colnames()</a></code></strong></p>
<p>When column names are broken up across the top <em>n</em> rows of a data frame or tibble, <code><a href="reference/mash_colnames.html">mash_colnames()</a></code> makes many header rows into column names. Existing names can be kept or ignored.</p>
<div class="sourceCode" id="cb9"><pre class="r"><span class="co"># Data with broken headers</span>
<span class="no">babies</span> <span class="kw">&lt;-</span>
  <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
    <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
    <span class="kw">Baby</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="fl">NA</span>, <span class="st">"Angie"</span>, <span class="st">"Yean"</span>, <span class="st">"Pierre"</span>),
    <span class="kw">Age</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"in"</span>, <span class="st">"months"</span>, <span class="st">"11"</span>, <span class="st">"9"</span>, <span class="st">"7"</span>),
    <span class="kw">Weight</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"kg"</span>, <span class="fl">NA</span>, <span class="st">"2"</span>, <span class="st">"3"</span>, <span class="st">"4"</span>),
    <span class="kw">Ward</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="fl">NA</span>, <span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"C"</span>)
  )

<span class="no">babies</span>
<span class="co">#&gt;     Baby    Age Weight Ward</span>
<span class="co">#&gt; 1   &lt;NA&gt;     in     kg &lt;NA&gt;</span>
<span class="co">#&gt; 2   &lt;NA&gt; months   &lt;NA&gt; &lt;NA&gt;</span>
<span class="co">#&gt; 3  Angie     11      2    A</span>
<span class="co">#&gt; 4   Yean      9      3    B</span>
<span class="co">#&gt; 5 Pierre      7      4    C</span></pre></div>
<div class="sourceCode" id="cb10"><pre class="r"><span class="co"># Mash, including the object names</span>
<span class="fu"><a href="reference/mash_colnames.html">mash_colnames</a></span>(<span class="no">babies</span>, <span class="kw">n_name_rows</span> <span class="kw">=</span> <span class="fl">2</span>, <span class="kw">keep_names</span> <span class="kw">=</span> <span class="fl">TRUE</span>)
<span class="co">#&gt;     Baby Age_in_months Weight_kg Ward</span>
<span class="co">#&gt; 3  Angie            11         2    A</span>
<span class="co">#&gt; 4   Yean             9         3    B</span>
<span class="co">#&gt; 5 Pierre             7         4    C</span></pre></div>
<p>For inputs with ragged column names (NA values in the first row), the first row can be filled row-wise before mashing.</p>
<div class="sourceCode" id="cb11"><pre class="r"><span class="co"># Data with ragged headers</span>
<span class="no">survey</span> <span class="kw">&lt;-</span>
  <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
    <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
    <span class="kw">X1</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Participant"</span>, <span class="fl">NA</span>, <span class="st">"12"</span>, <span class="st">"34"</span>, <span class="st">"45"</span>, <span class="st">"123"</span>),
    <span class="kw">X2</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"How did you hear about us?"</span>,
      <span class="st">"TV"</span>, <span class="st">"TRUE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>
    ),
    <span class="kw">X3</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="st">"Social Media"</span>, <span class="st">"FALSE"</span>, <span class="st">"TRUE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>),
    <span class="kw">X4</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="st">"Radio"</span>, <span class="st">"FALSE"</span>, <span class="st">"TRUE"</span>, <span class="st">"FALSE"</span>, <span class="st">"TRUE"</span>),
    <span class="kw">X5</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="st">"Flyer"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>),
    <span class="kw">X6</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Age"</span>, <span class="fl">NA</span>, <span class="st">"31"</span>, <span class="st">"23"</span>, <span class="st">"19"</span>, <span class="st">"24"</span>)
  )
<span class="no">survey</span>
<span class="co">#&gt;            X1                         X2           X3    X4    X5   X6</span>
<span class="co">#&gt; 1 Participant How did you hear about us?         &lt;NA&gt;  &lt;NA&gt;  &lt;NA&gt;  Age</span>
<span class="co">#&gt; 2        &lt;NA&gt;                         TV Social Media Radio Flyer &lt;NA&gt;</span>
<span class="co">#&gt; 3          12                       TRUE        FALSE FALSE FALSE   31</span>
<span class="co">#&gt; 4          34                      FALSE         TRUE  TRUE FALSE   23</span>
<span class="co">#&gt; 5          45                      FALSE        FALSE FALSE FALSE   19</span>
<span class="co">#&gt; 6         123                      FALSE        FALSE  TRUE FALSE   24</span></pre></div>
<div class="sourceCode" id="cb12"><pre class="r"><span class="co"># Ignoring names and using sliding headers</span>
<span class="fu"><a href="reference/mash_colnames.html">mash_colnames</a></span>(<span class="no">survey</span>,<span class="fl">2</span>,<span class="kw">keep_names</span> <span class="kw">=</span> <span class="fl">FALSE</span>,<span class="kw">sliding_headers</span> <span class="kw">=</span> <span class="fl">TRUE</span>, <span class="kw">sep</span> <span class="kw">=</span> <span class="st">"_"</span>)
<span class="co">#&gt;   Participant How did you hear about us?_TV</span>
<span class="co">#&gt; 3          12                          TRUE</span>
<span class="co">#&gt; 4          34                         FALSE</span>
<span class="co">#&gt; 5          45                         FALSE</span>
<span class="co">#&gt; 6         123                         FALSE</span>
<span class="co">#&gt;   How did you hear about us?_Social Media How did you hear about us?_Radio</span>
<span class="co">#&gt; 3                                   FALSE                            FALSE</span>
<span class="co">#&gt; 4                                    TRUE                             TRUE</span>
<span class="co">#&gt; 5                                   FALSE                            FALSE</span>
<span class="co">#&gt; 6                                   FALSE                             TRUE</span>
<span class="co">#&gt;   How did you hear about us?_Flyer Age</span>
<span class="co">#&gt; 3                            FALSE  31</span>
<span class="co">#&gt; 4                            FALSE  23</span>
<span class="co">#&gt; 5                            FALSE  19</span>
<span class="co">#&gt; 6                            FALSE  24</span></pre></div>
<p><strong><code><a href="reference/annotate_mf.html">annotate_mf()</a></code> and <code><a href="reference/annotate_mf_all.html">annotate_mf_all()</a></code></strong></p>
<p>Sometimes embedded subheaders can’t be matched by content or context, but they share the same formatting in a spreadsheet file.</p>
<p><code><a href="reference/annotate_mf.html">annotate_mf()</a></code> flattens four common approaches to confer meaningful formatting to cells and adds this as a character string to the target variable.</p>
<div class="sourceCode" id="cb13"><pre class="r"><span class="no">example_spreadsheet</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span>(<span class="st">"extdata/dog_test.xlsx"</span>, <span class="kw">package</span> <span class="kw">=</span> <span class="st">"unheadr"</span>)
<span class="fu"><a href="reference/annotate_mf.html">annotate_mf</a></span>(<span class="no">example_spreadsheet</span>,<span class="kw">orig</span> <span class="kw">=</span> <span class="no">Task</span>, <span class="kw">new</span><span class="kw">=</span><span class="no">Task_annotated</span>)</pre></div>
<p><code><a href="reference/annotate_mf_all.html">annotate_mf_all()</a></code> applies the same approach to all values in the dataset.</p>
<div class="sourceCode" id="cb14"><pre class="r"><span class="no">example_spreadsheet_all</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span>(<span class="st">"extdata/boutiques.xlsx"</span>, <span class="kw">package</span> <span class="kw">=</span> <span class="st">"unheadr"</span>)
<span class="fu"><a href="reference/annotate_mf.html">annotate_mf</a></span>(<span class="no">example_spreadsheet_all</span>)</pre></div>
<p>Lastly, <code><a href="reference/regex_valign.html">regex_valign()</a></code> can adjust the whitespace (padding) within a character vector with one element per line, for easier parsing with <code>readr</code>.</p>
<div class="sourceCode" id="cb15"><pre class="r"><span class="no">guests</span> <span class="kw">&lt;-</span>
  <span class="fu"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span>(<span class="fu"><a href="https://rdrr.io/r/base/strsplit.html">strsplit</a></span>(<span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"6     COAHUILA        20/03/2020
712        COAHUILA             20/03/2020"</span>),<span class="st">"\n"</span>))
<span class="no">guests</span>
<span class="co">#&gt; [1] "6     COAHUILA        20/03/2020"          </span>
<span class="co">#&gt; [2] "712        COAHUILA             20/03/2020"</span>
<span class="fu"><a href="reference/regex_valign.html">regex_valign</a></span>(<span class="no">guests</span>, <span class="st">"\\b(?=[A-Z])"</span>)
<span class="co">#&gt; [1] "6          COAHUILA        20/03/2020"     </span>
<span class="co">#&gt; [2] "712        COAHUILA             20/03/2020"</span></pre></div>
<p>The inconsistent whitespace between the elements in each line can be adjusted after matching a position of interest through regular expressions.</p>
</div>
</div>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <div class="links">
<h2>Links</h2>
<ul class="list-unstyled">
<li>Download from CRAN at <br><a href="https://cloud.r-project.org/package=unheadr">https://​cloud.r-project.org/​package=unheadr</a>
</li>
<li>Browse source code at <br><a href="https://github.com/luisDVA/unheadr/">https://​github.com/​luisDVA/​unheadr/​</a>
</li>
<li>Report a bug at <br><a href="https://github.com/luisDVA/unheadr/issues">https://​github.com/​luisDVA/​unheadr/​issues</a>
</li>
</ul>
</div>
<div class="license">
<h2>License</h2>
<ul class="list-unstyled">
<li><a href="LICENSE.html">Full license</a></li>
<li><small><a href="https://opensource.org/licenses/mit-license.php">MIT</a> + file <a href="LICENSE-text.html">LICENSE</a></small></li>
</ul>
</div>
<div class="citation">
<h2>Citation</h2>
<ul class="list-unstyled">
<li><a href="authors.html">Citing unheadr</a></li>
</ul>
</div>
<div class="developers">
<h2>Developers</h2>
<ul class="list-unstyled">
<li><NAME> <br><small class="roles"> Author, maintainer </small> <a href="https://orcid.org/0000-0001-9520-6543" target="orcid.widget" aria-label="ORCID"><span class="fab fa-orcid orcid" aria-hidden="true"></span></a> </li>
</ul>
</div>

  <div class="dev-status">
<h2>Dev status</h2>
<ul class="list-unstyled">
<li><a href="https://doi.org/10.4404/hystrix-00133-2018"><img src="https://img.shields.io/badge/doi-10.4404/hystrix--00133--2018-red.svg"></a></li>
<li><a href="https://cran.r-project.org/package=unheadr"><img src="https://www.r-pkg.org/badges/version/unheadr?color=purple"></a></li>
<li><a href="https://cran.r-project.org/package=unheadr"><img src="http://cranlogs.r-pkg.org/badges/last-month/unheadr?color=orange"></a></li>
<li><a href="https://cran.r-project.org/package=unheadr"><img src="http://cranlogs.r-pkg.org/badges/grand-total/unheadr?color=blue"></a></li>
<li><a href="https://cran.r-project.org/web/checks/check_results_unheadr.html"><img src="https://cranchecks.info/badges/summary/unheadr" alt="CRAN checks"></a></li>
<li><a href="https://travis-ci.org/luisDVA/unheadr"><img src="https://travis-ci.org/luisDVA/unheadr.svg?branch=master" alt="Travis build status"></a></li>
<li><a href="https://codecov.io/gh/luisDVA/unheadr?branch=master"><img src="https://codecov.io/gh/luisDVA/unheadr/branch/master/graph/badge.svg" alt="Codecov test coverage"></a></li>
</ul>
</div>
</div>
</div>


      <footer><div class="copyright">
  <p>Developed by <NAME>.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.5.1.</p>
</div>

      </footer>
</div>

  


  </body>
</html>


<|start_filename|>docs/reference/mash_colnames.html<|end_filename|>
<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html lang="en">
  <head>
  <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Make many header rows into column names — mash_colnames • unheadr</title>

<!-- favicons -->
<link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png" />
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png" />

<!-- jquery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/spacelab/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous" />


<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script>

<!-- bootstrap-toc -->
<link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script>

<!-- Font Awesome icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous" />

<!-- clipboard.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script>

<!-- headroom.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script>

<!-- pkgdown -->
<link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script>




<meta property="og:title" content="Make many header rows into column names — mash_colnames" />
<meta property="og:description" content="Make many header rows into column names" />
<meta property="og:image" content="unheadr.liomys.mx/logo.png" />




<!-- mathjax -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script>

<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->



  </head>

  <body data-spy="scroll" data-target="#toc">
    <div class="container template-reference-topic">
      <header>
      <div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">unheadr</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.3.1</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
        <li>
  <a href="../index.html">
    <span class="fas fa fas fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
    <li>
      <a href="../articles/unheadr-vignette.html">Introduction to unheadr</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">Changelog</a>
</li>
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li>
  <a href="https://github.com/luisDVA/unheadr">
    <span class="fab fa fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
      
    </div><!--/.nav-collapse -->
  </div><!--/.container -->
</div><!--/.navbar -->

      

      </header>

<div class="row">
  <div class="col-md-9 contents">
    <div class="page-header">
    <h1>Make many header rows into column names</h1>
    <small class="dont-index">Source: <a href='https://github.com/luisDVA/unheadr/blob/master/R/mash_colnames.R'><code>R/mash_colnames.R</code></a></small>
    <div class="hidden name"><code>mash_colnames.Rd</code></div>
    </div>

    <div class="ref-description">
    <p>Make many header rows into column names</p>
    </div>

    <pre class="usage"><span class='fu'>mash_colnames</span>(
  <span class='no'>df</span>,
  <span class='no'>n_name_rows</span>,
  <span class='kw'>keep_names</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
  <span class='kw'>sliding_headers</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>,
  <span class='kw'>sep</span> <span class='kw'>=</span> <span class='st'>"_"</span>
)</pre>

    <h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
    <table class="ref-arguments">
    <colgroup><col class="name" /><col class="desc" /></colgroup>
    <tr>
      <th>df</th>
      <td><p>A <code>data.frame</code> or <code>tibble</code> object in which the names are broken up
across the top <em>n</em> rows.</p></td>
    </tr>
    <tr>
      <th>n_name_rows</th>
      <td><p>Number of rows at the top of the data to be used to create
the new variable (column) names. Must be &gt;= 1.</p></td>
    </tr>
    <tr>
      <th>keep_names</th>
      <td><p>If TRUE, existing names will be included in building the
new variable names. Defaults to TRUE.</p></td>
    </tr>
    <tr>
      <th>sliding_headers</th>
      <td><p>If TRUE, empty values in the first (topmost) header
header row be filled column-wise. Defaults to FALSE. See details.</p></td>
    </tr>
    <tr>
      <th>sep</th>
      <td><p>Character string to separate the unified values (default is
underscore).</p></td>
    </tr>
    </table>

    <h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>

    <p>The original data frame, but with new column names and without the
top n rows that held the broken up names.</p>
    <h2 class="hasAnchor" id="details"><a class="anchor" href="#details"></a>Details</h2>

    <p>Tables are often shared with the column names broken up across the
first few rows. This function takes the number of rows at the top of a
table that hold the broken up names and whether or not to include the
names, and mashes the values column-wise into a single string for each
column. The <code>keep_names</code> argument can be helpful for tables we
imported using a <code>skip</code> argument. If <code>keep_names</code> is set to <code>FALSE</code>,
adjust the value of <code>n_name_rows</code> accordingly.</p>
<p>This function will throw a warning when possible <code>NA</code> values end up in the
variable names. <code>sliding_headers</code> can be used for tables with ragged
names in which not every column has a value in the very first row. In these
cases attribution by adjacency is assumed, and when <code>sliding_headers</code>
is set to <code>TRUE</code> the names in the topmost row are filled row-wise. This can
be useful for tables reporting survey data or experimental designs in an
untidy manner.</p>

    <h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
    <pre class="examples"><div class='input'><span class='no'>babies</span> <span class='kw'>&lt;-</span>
  <span class='fu'><a href='https://rdrr.io/r/base/data.frame.html'>data.frame</a></span>(
    <span class='kw'>stringsAsFactors</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>,
    <span class='kw'>Baby</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fl'>NA</span>, <span class='fl'>NA</span>, <span class='st'>"Angie"</span>, <span class='st'>"Yean"</span>, <span class='st'>"Pierre"</span>),
    <span class='kw'>Age</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"in"</span>, <span class='st'>"months"</span>, <span class='st'>"11"</span>, <span class='st'>"9"</span>, <span class='st'>"7"</span>),
    <span class='kw'>Weight</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"kg"</span>, <span class='fl'>NA</span>, <span class='st'>"2"</span>, <span class='st'>"3"</span>, <span class='st'>"4"</span>),
    <span class='kw'>Ward</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fl'>NA</span>, <span class='fl'>NA</span>, <span class='st'>"A"</span>, <span class='st'>"B"</span>, <span class='st'>"C"</span>)
  )
<span class='co'># Including the object names</span>
<span class='fu'>mash_colnames</span>(<span class='no'>babies</span>, <span class='kw'>n_name_rows</span> <span class='kw'>=</span> <span class='fl'>2</span>, <span class='kw'>keep_names</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>)</div><div class='output co'>#&gt;     Baby Age_in_months Weight_kg Ward
#&gt; 3  Angie            11         2    A
#&gt; 4   Yean             9         3    B
#&gt; 5 Pierre             7         4    C</div><div class='input'>
<span class='no'>babies_skip</span> <span class='kw'>&lt;-</span>
  <span class='fu'><a href='https://rdrr.io/r/base/data.frame.html'>data.frame</a></span>(
    <span class='kw'>stringsAsFactors</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>,
    <span class='kw'>X1</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"Baby"</span>, <span class='fl'>NA</span>, <span class='fl'>NA</span>, <span class='st'>"Jennie"</span>, <span class='st'>"Yean"</span>, <span class='st'>"Pierre"</span>),
    <span class='kw'>X2</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"Age"</span>, <span class='st'>"in"</span>, <span class='st'>"months"</span>, <span class='st'>"11"</span>, <span class='st'>"9"</span>, <span class='st'>"7"</span>),
    <span class='kw'>X3</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"Hospital"</span>, <span class='fl'>NA</span>, <span class='fl'>NA</span>, <span class='st'>"A"</span>, <span class='st'>"B"</span>, <span class='st'>"A"</span>)
  )
<span class='co'>#' # Discarding the automatically-generated names (X1, X2, etc...)</span>
<span class='fu'>mash_colnames</span>(<span class='no'>babies_skip</span>, <span class='kw'>n_name_rows</span> <span class='kw'>=</span> <span class='fl'>3</span>, <span class='kw'>keep_names</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>)</div><div class='output co'>#&gt;     Baby Age_in_months Hospital
#&gt; 4 Jennie            11        A
#&gt; 5   Yean             9        B
#&gt; 6 Pierre             7        A</div><div class='input'>
<span class='no'>fish_experiment</span> <span class='kw'>&lt;-</span>
  <span class='fu'><a href='https://rdrr.io/r/base/data.frame.html'>data.frame</a></span>(
    <span class='kw'>stringsAsFactors</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>,
    <span class='kw'>X1</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"Sample"</span>, <span class='fl'>NA</span>, <span class='st'>"Pacific"</span>, <span class='st'>"Atlantic"</span>, <span class='st'>"Freshwater"</span>),
    <span class='kw'>X2</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"Larvae"</span>, <span class='st'>"Control"</span>, <span class='st'>"12"</span>, <span class='st'>"11"</span>, <span class='st'>"10"</span>),
    <span class='kw'>X3</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fl'>NA</span>, <span class='st'>"Low Dose"</span>, <span class='st'>"11"</span>, <span class='st'>"12"</span>, <span class='st'>"8"</span>),
    <span class='kw'>X4</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fl'>NA</span>, <span class='st'>"High Dose"</span>, <span class='st'>"8"</span>, <span class='st'>"7"</span>, <span class='st'>"9"</span>),
    <span class='kw'>X5</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='st'>"Adult"</span>, <span class='st'>"Control"</span>, <span class='st'>"13"</span>, <span class='st'>"13"</span>, <span class='st'>"8"</span>),
    <span class='kw'>X6</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fl'>NA</span>, <span class='st'>"Low Dose"</span>, <span class='st'>"13"</span>, <span class='st'>"12"</span>, <span class='st'>"7"</span>),
    <span class='kw'>X7</span> <span class='kw'>=</span> <span class='fu'><a href='https://rdrr.io/r/base/c.html'>c</a></span>(<span class='fl'>NA</span>, <span class='st'>"High Dose"</span>, <span class='st'>"10"</span>, <span class='st'>"10"</span>, <span class='st'>"9"</span>)
  )
<span class='co'># Ragged names</span>
<span class='fu'>mash_colnames</span>(<span class='no'>fish_experiment</span>,
  <span class='kw'>n_name_rows</span> <span class='kw'>=</span> <span class='fl'>2</span>,
  <span class='kw'>keep_names</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>, <span class='kw'>sliding_headers</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>
)</div><div class='output co'>#&gt;       Sample Larvae_Control Larvae_Low Dose Larvae_High Dose Adult_Control
#&gt; 3    Pacific             12              11                8            13
#&gt; 4   Atlantic             11              12                7            13
#&gt; 5 Freshwater             10               8                9             8
#&gt;   Adult_Low Dose Adult_High Dose
#&gt; 3             13              10
#&gt; 4             12              10
#&gt; 5              7               9</div></pre>
  </div>
  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
    <nav id="toc" data-toggle="toc" class="sticky-top">
      <h2 data-toc-skip>Contents</h2>
    </nav>
  </div>
</div>


      <footer>
      <div class="copyright">
  <p>Developed by <NAME>.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.5.1.</p>
</div>

      </footer>
   </div>

  


  </body>
</html>




<|start_filename|>docs/articles/unheadr-vignette.html<|end_filename|>
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Introduction to unheadr • unheadr</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png">
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.4.0/spacelab/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><meta property="og:title" content="Introduction to unheadr">
<meta property="og:description" content="unheadr">
<meta property="og:image" content="unheadr.liomys.mx/logo.png">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
    <div class="container template-article">
      <header><div class="navbar navbar-default navbar-fixed-top" role="navigation">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <span class="navbar-brand">
        <a class="navbar-link" href="../index.html">unheadr</a>
        <span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Released version">0.3.1</span>
      </span>
    </div>

    <div id="navbar" class="navbar-collapse collapse">
      <ul class="nav navbar-nav">
<li>
  <a href="../index.html">
    <span class="fas fa fas fa-home fa-lg"></span>
     
  </a>
</li>
<li>
  <a href="../reference/index.html">Reference</a>
</li>
<li class="dropdown">
  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
    Articles
     
    <span class="caret"></span>
  </a>
  <ul class="dropdown-menu" role="menu">
<li>
      <a href="../articles/unheadr-vignette.html">Introduction to unheadr</a>
    </li>
  </ul>
</li>
<li>
  <a href="../news/index.html">Changelog</a>
</li>
      </ul>
<ul class="nav navbar-nav navbar-right">
<li>
  <a href="https://github.com/luisDVA/unheadr">
    <span class="fab fa fab fa-github fa-lg"></span>
     
  </a>
</li>
      </ul>
</div>
<!--/.nav-collapse -->
  </div>
<!--/.container -->
</div>
<!--/.navbar -->

      

      </header><script src="unheadr-vignette_files/header-attrs-2.1/header-attrs.js"></script><div class="row">
  <div class="col-md-9 contents">
    <div class="page-header toc-ignore">
      <h1 data-toc-skip>Introduction to unheadr</h1>
            
      
      <small class="dont-index">Source: <a href="https://github.com/luisDVA/unheadr/blob/master/vignettes/unheadr-vignette.Rmd"><code>vignettes/unheadr-vignette.Rmd</code></a></small>
      <div class="hidden name"><code>unheadr-vignette.Rmd</code></div>

    </div>

    
    
<p>When we work with other people’s data, we often have to struggle through multiple unexpected steps before we get to a flexible, usable structure. Popular ways of structuring and presenting data can place content beyond the reach of code-based routines to tackle repetitive tasks efficiently.</p>
<div id="package-functions" class="section level2">
<h2 class="hasAnchor">
<a href="#package-functions" class="anchor"></a>Package functions</h2>
<p>The functions in <code>unheadr</code> help us rework data shared by other people, from a human-readable structure to a tidier machine-readable structure on which we can perform common data manipulation tasks.</p>
<div id="data-frames-and-tibbles" class="section level3">
<h3 class="hasAnchor">
<a href="#data-frames-and-tibbles" class="anchor"></a>Data frames and tibbles</h3>
<p>Here is how <code>unheadr</code> works with tibble and data frame objects that suffer from common issues such as:</p>
<ul>
<li>Embedded subheaders</li>
<li>Broken values</li>
<li>Merged cells and values wrapped within columns</li>
<li>Broken rows</li>
<li>Broken headers</li>
</ul>
<p><strong>Embedded subheaders</strong> are usually grouping variables embedded into another variable, used to show hierarchical data or create small multiples of data.</p>
<p>A simple example would be a coffee shop menu:</p>
<div class="sourceCode" id="cb1"><html><body><pre class="r"><span class="no">dat</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
  <span class="kw">drink</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
    <span class="st">"Cold Drinks"</span>, <span class="st">"Soda"</span>, <span class="st">"Water"</span>, <span class="st">"Juice"</span>, <span class="st">"Lemonade"</span>,
    <span class="st">"Hot Drinks"</span>, <span class="st">"Tea"</span>, <span class="st">"Coffee"</span>
  ),
  <span class="kw">price</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="fl">2.99</span>, <span class="fl">1.99</span>, <span class="fl">3.15</span>, <span class="fl">2</span>, <span class="fl">NA</span>, <span class="fl">3.99</span>, <span class="fl">1.99</span>), <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>
)

<span class="no">dat</span>
<span class="co">#&gt;         drink price</span>
<span class="co">#&gt; 1 Cold Drinks    NA</span>
<span class="co">#&gt; 2        Soda  2.99</span>
<span class="co">#&gt; 3       Water  1.99</span>
<span class="co">#&gt; 4       Juice  3.15</span>
<span class="co">#&gt; 5    Lemonade  2.00</span>
<span class="co">#&gt; 6  Hot Drinks    NA</span>
<span class="co">#&gt; 7         Tea  3.99</span>
<span class="co">#&gt; 8      Coffee  1.99</span></pre></body></html></div>
<p>The beverage type is embedded in the ‘drinks’ variable. If we can match them with regular expressions, we can move the grouping values into their own variable using <code><a href="../reference/untangle2.html">untangle2()</a></code></p>
<div class="sourceCode" id="cb2"><html><body><pre class="r"><span class="fu"><a href="../reference/untangle2.html">untangle2</a></span>(<span class="no">dat</span>, <span class="st">"Drinks$"</span>, <span class="no">drink</span>, <span class="st">"beverage_type"</span>)
<span class="co">#&gt; 2 matches</span>
<span class="co">#&gt;      drink price beverage_type</span>
<span class="co">#&gt; 1     Soda  2.99   Cold Drinks</span>
<span class="co">#&gt; 2    Water  1.99   Cold Drinks</span>
<span class="co">#&gt; 3    Juice  3.15   Cold Drinks</span>
<span class="co">#&gt; 4 Lemonade  2.00   Cold Drinks</span>
<span class="co">#&gt; 5      Tea  3.99    Hot Drinks</span>
<span class="co">#&gt; 6   Coffee  1.99    Hot Drinks</span></pre></body></html></div>
<p><strong>Broken values</strong> usually happen when we’re pressed for space.</p>
<p>For whatever reason, the entries for the Barcelona 1992 and London 2012 Olympics are broken across two contiguous rows and NAs are used as padding in the other variables.</p>
<div class="sourceCode" id="cb3"><html><body><pre class="r"><span class="no">OGames</span> <span class="kw">&lt;-</span> <span class="fu">tibble</span>(
  <span class="kw">Games</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Los Angeles 1984"</span>, <span class="st">"Barcelona"</span>, <span class="st">"1992"</span>, <span class="st">"Atlanta 1996"</span>, <span class="st">"Sydney 2000"</span>, <span class="st">"London"</span>, <span class="st">"2012"</span>),
  <span class="kw">Country</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"USA"</span>, <span class="st">"Spain"</span>, <span class="fl">NA</span>, <span class="st">"USA"</span>, <span class="st">"Australia"</span>, <span class="st">"UK"</span>, <span class="fl">NA</span>),
  <span class="kw">Soccer_gold_medal</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"France"</span>, <span class="st">"Spain"</span>, <span class="fl">NA</span>, <span class="st">"Nigeria"</span>, <span class="st">"Cameroon"</span>, <span class="st">"Mexico"</span>, <span class="fl">NA</span>)
)
<span class="no">OGames</span>
<span class="co">#&gt; # A tibble: 7 x 3</span>
<span class="co">#&gt;   Games            Country   Soccer_gold_medal</span>
<span class="co">#&gt;   &lt;chr&gt;            &lt;chr&gt;     &lt;chr&gt;            </span>
<span class="co">#&gt; 1 Los Angeles 1984 USA       France           </span>
<span class="co">#&gt; 2 Barcelona        Spain     Spain            </span>
<span class="co">#&gt; 3 1992             &lt;NA&gt;      &lt;NA&gt;             </span>
<span class="co">#&gt; 4 Atlanta 1996     USA       Nigeria          </span>
<span class="co">#&gt; 5 Sydney 2000      Australia Cameroon         </span>
<span class="co">#&gt; 6 London           UK        Mexico           </span>
<span class="co">#&gt; 7 2012             &lt;NA&gt;      &lt;NA&gt;</span></pre></body></html></div>
<p>In this case, we can use <code><a href="../reference/unbreak_vals.html">unbreak_vals()</a></code> to ‘unbreak’ the lines in the ‘Games’ variable, matching the strings that start with numbers.</p>
<div class="sourceCode" id="cb4"><html><body><pre class="r"><span class="no">OGames</span> <span class="kw">%&gt;%</span>
  <span class="fu"><a href="../reference/unbreak_vals.html">unbreak_vals</a></span>(<span class="st">"^[0-9]"</span>, <span class="no">Games</span>, <span class="no">Games_unbroken</span>, <span class="kw">slice_groups</span> <span class="kw">=</span> <span class="fl">TRUE</span>) <span class="kw">%&gt;%</span>
  <span class="fu">select</span>(<span class="no">Games_unbroken</span>, <span class="fu">everything</span>())
<span class="co">#&gt; Warning: argument slice_groups is deprecated; extra rows and the variable with</span>
<span class="co">#&gt; broken values are now dropped by default.</span>
<span class="co">#&gt; # A tibble: 5 x 3</span>
<span class="co">#&gt;   Games_unbroken   Country   Soccer_gold_medal</span>
<span class="co">#&gt;   &lt;chr&gt;            &lt;chr&gt;     &lt;chr&gt;            </span>
<span class="co">#&gt; 1 Los Angeles 1984 USA       France           </span>
<span class="co">#&gt; 2 Barcelona 1992   Spain     Spain            </span>
<span class="co">#&gt; 3 Atlanta 1996     USA       Nigeria          </span>
<span class="co">#&gt; 4 Sydney 2000      Australia Cameroon         </span>
<span class="co">#&gt; 5 London 2012      UK        Mexico</span></pre></body></html></div>
<p><strong>Wrapped columns</strong> often happen when we merge cells in spreadsheets or use table formatting in a word processor, resulting in empty or NA values used to pad all the vertical space.</p>
<div class="sourceCode" id="cb5"><html><body><pre class="r"><span class="no">knicks</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
  <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
  <span class="kw">player</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"<NAME>"</span>, <span class="fl">NA</span>, <span class="st">"Latrell Sprewell"</span>, <span class="fl">NA</span>, <span class="fl">NA</span>),
  <span class="kw">teams</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
    <span class="st">"Pistons"</span>, <span class="st">"Knicks"</span>, <span class="st">"Warriors"</span>, <span class="st">"Knicks"</span>,
    <span class="st">"Timberwolves"</span>
  ),
  <span class="kw">position</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Shooting guard"</span>, <span class="fl">NA</span>, <span class="st">"Small forward"</span>, <span class="fl">NA</span>, <span class="fl">NA</span>)
)
<span class="no">knicks</span>
<span class="co">#&gt;             player        teams       position</span>
<span class="co">#&gt; 1    <NAME>      Pistons Shooting guard</span>
<span class="co">#&gt; 2             &lt;NA&gt;       Knicks           &lt;NA&gt;</span>
<span class="co">#&gt; 3 <NAME>     Warriors  Small forward</span>
<span class="co">#&gt; 4             &lt;NA&gt;       Knicks           &lt;NA&gt;</span>
<span class="co">#&gt; 5             &lt;NA&gt; Timberwolves           &lt;NA&gt;</span></pre></body></html></div>
<p>We can unwrap the ‘teams’ values into a single string using <code>unrwap_cols()</code>.</p>
<div class="sourceCode" id="cb6"><html><body><pre class="r"><span class="no">knicks</span> <span class="kw">%&gt;%</span> <span class="fu"><a href="../reference/unwrap_cols.html">unwrap_cols</a></span>(<span class="kw">groupingVar</span> <span class="kw">=</span> <span class="no">player</span>, <span class="kw">separator</span> <span class="kw">=</span> <span class="st">", "</span>)
<span class="co">#&gt; # A tibble: 2 x 3</span>
<span class="co">#&gt;   player           teams                          position      </span>
<span class="co">#&gt;   &lt;chr&gt;            &lt;chr&gt;                          &lt;chr&gt;         </span>
<span class="co">#&gt; 1 Allan Houston    Pistons, Knicks                Shooting guard</span>
<span class="co">#&gt; 2 Latrell Sprewell Warriors, Knicks, Timberwolves Small forward</span></pre></body></html></div>
<p>This is more or less the opposite to <code>separate_rows()</code> from <code>tidyr</code>.</p>
<p><strong>Broken rows</strong> have values of two contiguous rows broken up and padded with empty or NA values.</p>
<div class="sourceCode" id="cb7"><html><body><pre class="r"><span class="no">basketball</span> <span class="kw">&lt;-</span>
  <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
    <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
    <span class="kw">v1</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"Player"</span>, <span class="fl">NA</span>, <span class="st">"<NAME>"</span>, <span class="st">"<NAME>"</span>,
      <span class="st">"<NAME>"</span>, <span class="st">"<NAME>"</span>,
      <span class="st">"<NAME>"</span>, <span class="st">"<NAME>"</span>
    ),
    <span class="kw">v2</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"Most points"</span>, <span class="st">"in a game"</span>, <span class="st">"55"</span>, <span class="st">"43"</span>, <span class="st">"41"</span>, <span class="st">"111"</span>, <span class="st">"109"</span>, <span class="st">"104"</span>
    ),
    <span class="kw">v3</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(
      <span class="st">"Season"</span>, <span class="st">"(year ending)"</span>, <span class="st">"2001"</span>, <span class="st">"2000"</span>, <span class="st">"2002"</span>,
      <span class="st">"2000"</span>, <span class="st">"2002"</span>, <span class="st">"2001"</span>
    )
  )
<span class="no">basketball</span>
<span class="co">#&gt;                v1          v2            v3</span>
<span class="co">#&gt; 1          Player Most points        Season</span>
<span class="co">#&gt; 2            &lt;NA&gt;   in a game (year ending)</span>
<span class="co">#&gt; 3 <NAME>          55          2001</span>
<span class="co">#&gt; 4     <NAME>          43          2000</span>
<span class="co">#&gt; 5  <NAME>          41          2002</span>
<span class="co">#&gt; 6  <NAME>         111          2000</span>
<span class="co">#&gt; 7 <NAME>         109          2002</span>
<span class="co">#&gt; 8     <NAME>         104          2001</span></pre></body></html></div>
<p>In this case, we can match any value in any variable along the row that has broken values.</p>
<div class="sourceCode" id="cb8"><html><body><pre class="r"><span class="fu"><a href="../reference/unbreak_rows.html">unbreak_rows</a></span>(<span class="no">basketball</span>, <span class="st">"^Most"</span>, <span class="no">v2</span>)
<span class="co">#&gt; 1 match</span>
<span class="co">#&gt;                v1                    v2                   v3</span>
<span class="co">#&gt; 1          Player Most points in a game Season (year ending)</span>
<span class="co">#&gt; 2 <NAME>                    55                 2001</span>
<span class="co">#&gt; 3     <NAME>                    43                 2000</span>
<span class="co">#&gt; 4  <NAME>                    41                 2002</span>
<span class="co">#&gt; 5  <NAME>                   111                 2000</span>
<span class="co">#&gt; 6 <NAME>                   109                 2002</span>
<span class="co">#&gt; 7     <NAME>                   104                 2001</span></pre></body></html></div>
<p><strong>Broken headers</strong> are variable names broken up across the first few rows.</p>
<div class="sourceCode" id="cb9"><html><body><pre class="r"><span class="no">vehicles</span> <span class="kw">&lt;-</span>
<span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
  <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
           <span class="kw">Vehicle</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="st">"Truck"</span>, <span class="st">"Motorcycle"</span>, <span class="st">"Sedan"</span>, <span class="st">"Van"</span>),
             <span class="kw">Price</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"in"</span>,<span class="st">"2014"</span>,
                       <span class="st">"(local currency)"</span>,<span class="st">"50000"</span>,<span class="st">"44000"</span>,<span class="st">"33000"</span>,<span class="st">"50000"</span>),
             <span class="kw">Color</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="st">"White"</span>, <span class="st">"Black"</span>, <span class="st">"Red"</span>, <span class="st">"White"</span>),
         <span class="kw">Emissions</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Certificate"</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="st">"TRUE"</span>, <span class="st">"FALSE"</span>, <span class="st">"TRUE"</span>, <span class="st">"TRUE"</span>)
)
<span class="no">vehicles</span>
<span class="co">#&gt;      Vehicle            Price Color   Emissions</span>
<span class="co">#&gt; 1       &lt;NA&gt;               in  &lt;NA&gt; Certificate</span>
<span class="co">#&gt; 2       &lt;NA&gt;             2014  &lt;NA&gt;        &lt;NA&gt;</span>
<span class="co">#&gt; 3       &lt;NA&gt; (local currency)  &lt;NA&gt;        &lt;NA&gt;</span>
<span class="co">#&gt; 4      Truck            50000 White        TRUE</span>
<span class="co">#&gt; 5 Motorcycle            44000 Black       FALSE</span>
<span class="co">#&gt; 6      Sedan            33000   Red        TRUE</span>
<span class="co">#&gt; 7        Van            50000 White        TRUE</span></pre></body></html></div>
<p>Here, the column names are broken. The top <em>three</em> rows (in addition to the column name) contain fragments of the name and should be mashed together column-wise.</p>
<p>The <code><a href="../reference/mash_colnames.html">mash_colnames()</a></code> function makes these many header rows into column names. The names are broken up across the top three rows, which goes in to the <code>n_name_rows</code> argument. Unlike other functions in <code>unheadr</code>, we provide the number of rows directly, rather than attempt any string matching.</p>
<div class="sourceCode" id="cb10"><html><body><pre class="r"><span class="fu"><a href="../reference/mash_colnames.html">mash_colnames</a></span>(<span class="kw">df</span><span class="kw">=</span> <span class="no">vehicles</span>, <span class="kw">n_name_rows</span> <span class="kw">=</span> <span class="fl">3</span>, <span class="kw">keep_names</span> <span class="kw">=</span> <span class="fl">TRUE</span>)
<span class="co">#&gt;      Vehicle Price_in_2014_(local currency) Color Emissions_Certificate</span>
<span class="co">#&gt; 4      Truck                          50000 White                  TRUE</span>
<span class="co">#&gt; 5 Motorcycle                          44000 Black                 FALSE</span>
<span class="co">#&gt; 6      Sedan                          33000   Red                  TRUE</span>
<span class="co">#&gt; 7        Van                          50000 White                  TRUE</span></pre></body></html></div>
<p>When importing data with broken headers into R, variable names are not always assigned from the values in top row, leaving us with automatically generated names (e.g. X1, X2, X3, etc.).</p>
<div class="sourceCode" id="cb11"><html><body><pre class="r"><span class="no">vehicles_skip</span> <span class="kw">&lt;-</span>
<span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
  <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
                <span class="kw">X1</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Vehicle"</span>,<span class="fl">NA</span>,<span class="fl">NA</span>,<span class="fl">NA</span>,<span class="st">"Truck"</span>,
                       <span class="st">"Motorcycle"</span>,<span class="st">"Sedan"</span>,<span class="st">"Van"</span>),
                <span class="kw">X2</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Price"</span>,<span class="st">"in"</span>,<span class="st">"2014"</span>,
                       <span class="st">"(local currency)"</span>,<span class="st">"50000"</span>,<span class="st">"44000"</span>,<span class="st">"33000"</span>,<span class="st">"50000"</span>),
                <span class="kw">X3</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Color"</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="fl">NA</span>, <span class="st">"White"</span>, <span class="st">"Black"</span>, <span class="st">"Red"</span>, <span class="st">"White"</span>),
                <span class="kw">X4</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Emissions"</span>,<span class="st">"Certificate"</span>,<span class="fl">NA</span>,
                       <span class="fl">NA</span>,<span class="st">"TRUE"</span>,<span class="st">"FALSE"</span>,<span class="st">"TRUE"</span>,<span class="st">"TRUE"</span>)
)
<span class="no">vehicles_skip</span>
<span class="co">#&gt;           X1               X2    X3          X4</span>
<span class="co">#&gt; 1    Vehicle            Price Color   Emissions</span>
<span class="co">#&gt; 2       &lt;NA&gt;               in  &lt;NA&gt; Certificate</span>
<span class="co">#&gt; 3       &lt;NA&gt;             2014  &lt;NA&gt;        &lt;NA&gt;</span>
<span class="co">#&gt; 4       &lt;NA&gt; (local currency)  &lt;NA&gt;        &lt;NA&gt;</span>
<span class="co">#&gt; 5      Truck            50000 White        TRUE</span>
<span class="co">#&gt; 6 Motorcycle            44000 Black       FALSE</span>
<span class="co">#&gt; 7      Sedan            33000   Red        TRUE</span>
<span class="co">#&gt; 8        Van            50000 White        TRUE</span></pre></body></html></div>
<p>In this case, the <code>keep_names</code> argument in <code><a href="../reference/mash_colnames.html">mash_colnames()</a></code> lets us ignore the object names when building new names from the first four rows of the data.</p>
<div class="sourceCode" id="cb12"><html><body><pre class="r"><span class="fu"><a href="../reference/mash_colnames.html">mash_colnames</a></span>(<span class="kw">df</span><span class="kw">=</span> <span class="no">vehicles_skip</span>, <span class="kw">n_name_rows</span> <span class="kw">=</span> <span class="fl">4</span>, <span class="kw">keep_names</span> <span class="kw">=</span> <span class="fl">FALSE</span>)
<span class="co">#&gt;      Vehicle Price_in_2014_(local currency) Color Emissions_Certificate</span>
<span class="co">#&gt; 5      Truck                          50000 White                  TRUE</span>
<span class="co">#&gt; 6 Motorcycle                          44000 Black                 FALSE</span>
<span class="co">#&gt; 7      Sedan                          33000   Red                  TRUE</span>
<span class="co">#&gt; 8        Van                          50000 White                  TRUE</span></pre></body></html></div>
<p>Lastly, the <code>sliding_headers</code> argument in <code>mash_colnames</code> can be used for tables with ragged names, in which not every column has a value in the very first row. In such cases, attribution by adjacency is assumed, and when <code>sliding_headers = TRUE</code> the names are filled row-wise. This can be useful for tables reporting survey data or experimental designs in an untidy manner.</p>
<div class="sourceCode" id="cb13"><html><body><pre class="r"><span class="no">survey</span> <span class="kw">&lt;-</span>
<span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span>(
  <span class="kw">stringsAsFactors</span> <span class="kw">=</span> <span class="fl">FALSE</span>,
                <span class="kw">X1</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Participant"</span>, <span class="fl">NA</span>, <span class="st">"12"</span>, <span class="st">"34"</span>, <span class="st">"45"</span>, <span class="st">"123"</span>),
                <span class="kw">X2</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"How did you hear about us?"</span>,
                       <span class="st">"TV"</span>,<span class="st">"TRUE"</span>,<span class="st">"FALSE"</span>,<span class="st">"FALSE"</span>,<span class="st">"FALSE"</span>),
                <span class="kw">X3</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="st">"Social Media"</span>, <span class="st">"FALSE"</span>, <span class="st">"TRUE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>),
                <span class="kw">X4</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="st">"Radio"</span>, <span class="st">"FALSE"</span>, <span class="st">"TRUE"</span>, <span class="st">"FALSE"</span>, <span class="st">"TRUE"</span>),
                <span class="kw">X5</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="fl">NA</span>, <span class="st">"Flyer"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>, <span class="st">"FALSE"</span>),
                <span class="kw">X6</span> <span class="kw">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"Age"</span>, <span class="fl">NA</span>, <span class="st">"31"</span>, <span class="st">"23"</span>, <span class="st">"19"</span>, <span class="st">"24"</span>)
)

<span class="no">survey</span>
<span class="co">#&gt;            X1                         X2           X3    X4    X5   X6</span>
<span class="co">#&gt; 1 Participant How did you hear about us?         &lt;NA&gt;  &lt;NA&gt;  &lt;NA&gt;  Age</span>
<span class="co">#&gt; 2        &lt;NA&gt;                         TV Social Media Radio Flyer &lt;NA&gt;</span>
<span class="co">#&gt; 3          12                       TRUE        FALSE FALSE FALSE   31</span>
<span class="co">#&gt; 4          34                      FALSE         TRUE  TRUE FALSE   23</span>
<span class="co">#&gt; 5          45                      FALSE        FALSE FALSE FALSE   19</span>
<span class="co">#&gt; 6         123                      FALSE        FALSE  TRUE FALSE   24</span>
<span class="fu"><a href="../reference/mash_colnames.html">mash_colnames</a></span>(<span class="no">survey</span>,<span class="fl">2</span>,<span class="kw">keep_names</span> <span class="kw">=</span> <span class="fl">FALSE</span>,<span class="kw">sliding_headers</span> <span class="kw">=</span> <span class="fl">TRUE</span>, <span class="kw">sep</span> <span class="kw">=</span> <span class="st">"_"</span>)
<span class="co">#&gt;   Participant How did you hear about us?_TV</span>
<span class="co">#&gt; 3          12                          TRUE</span>
<span class="co">#&gt; 4          34                         FALSE</span>
<span class="co">#&gt; 5          45                         FALSE</span>
<span class="co">#&gt; 6         123                         FALSE</span>
<span class="co">#&gt;   How did you hear about us?_Social Media How did you hear about us?_Radio</span>
<span class="co">#&gt; 3                                   FALSE                            FALSE</span>
<span class="co">#&gt; 4                                    TRUE                             TRUE</span>
<span class="co">#&gt; 5                                   FALSE                            FALSE</span>
<span class="co">#&gt; 6                                   FALSE                             TRUE</span>
<span class="co">#&gt;   How did you hear about us?_Flyer Age</span>
<span class="co">#&gt; 3                            FALSE  31</span>
<span class="co">#&gt; 4                            FALSE  23</span>
<span class="co">#&gt; 5                            FALSE  19</span>
<span class="co">#&gt; 6                            FALSE  24</span></pre></body></html></div>
</div>
<div id="spreadsheets" class="section level3">
<h3 class="hasAnchor">
<a href="#spreadsheets" class="anchor"></a>Spreadsheets</h3>
<p><code>unheadr</code> also includes a function for flattening font and cell formatting in spreadsheet files into character strings in the corresponding cell.</p>
<div id="supported-formatting" class="section level4">
<h4 class="hasAnchor">
<a href="#supported-formatting" class="anchor"></a>Supported formatting:</h4>
<ul>
<li>bold text</li>
<li>colored text</li>
<li>italic text,</li>
<li>text with strikethrough</li>
<li>underlined text</li>
<li>double underlined text</li>
<li>cell highlighting</li>
</ul>
<p>One of the example files bundled with <code>unheadr</code> looks like this:</p>
<p><img src="dogtest.png" width="300"></p>
<p>Font formatting and cell highlighting is being used to label an embedded grouping variable (meaningful formatting). The <code><a href="../reference/annotate_mf.html">annotate_mf()</a></code> function flattens the formatting into a character string describing the formatting. The hex8 code of the colors used for cell or text highlighting is also included.</p>
<div class="sourceCode" id="cb14"><html><body><pre class="r"><span class="no">example_spreadsheet</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span>(<span class="st">"extdata/dog_test.xlsx"</span>, <span class="kw">package</span> <span class="kw">=</span> <span class="st">"unheadr"</span>)
<span class="fu"><a href="../reference/annotate_mf.html">annotate_mf</a></span>(<span class="no">example_spreadsheet</span>, <span class="kw">orig</span> <span class="kw">=</span> <span class="no">Task</span>, <span class="kw">new</span> <span class="kw">=</span> <span class="no">Task_annotated</span>)
<span class="co">#&gt; # A tibble: 11 x 3</span>
<span class="co">#&gt;    Task                           Task_annotated                           Score</span>
<span class="co">#&gt;    &lt;chr&gt;                          &lt;chr&gt;                                    &lt;dbl&gt;</span>
<span class="co">#&gt;  1 Outdoor activities             (bold, highlight-FFFFF200) Outdoor acti…  7.67</span>
<span class="co">#&gt;  2 Walks on a loose leash withou… Walks on a loose leash without pulling    7   </span>
<span class="co">#&gt;  3 Walks without chasing bicycle… Walks without chasing bicycles, animals…  6   </span>
<span class="co">#&gt;  4 Greets friends and strangers … Greets friends and strangers without ju… 10   </span>
<span class="co">#&gt;  5 Home behavior                  (bold, highlight-FFFFF200) Home behavior  8.5 </span>
<span class="co">#&gt;  6 Moves location when directed … Moves location when directed without gr…  9   </span>
<span class="co">#&gt;  7 Does not rush through doorways Does not rush through doorways            8   </span>
<span class="co">#&gt;  8 General social skills and obe… (bold, highlight-FFFFF200) General soci…  7   </span>
<span class="co">#&gt;  9 Can play or interact appropri… Can play or interact appropriately with…  7   </span>
<span class="co">#&gt; 10 Can be groomed or handled wit… Can be groomed or handled without squir…  8   </span>
<span class="co">#&gt; 11 Stops barking on command       Stops barking on command                  6</span></pre></body></html></div>
<p>To apply this approach to all cells in a spreadsheet, we call <code><a href="../reference/annotate_mf_all.html">annotate_mf_all()</a></code>. In this other bundled example file, negative values (first quarter losses) are indicated by bold.</p>
<p><img src="boutiques.png" width="300"></p>
<div class="sourceCode" id="cb15"><html><body><pre class="r"><span class="no">example_spreadsheetb</span> <span class="kw">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span>(<span class="st">"extdata/boutiques.xlsx"</span>, <span class="kw">package</span> <span class="kw">=</span> <span class="st">"unheadr"</span>)
<span class="fu"><a href="../reference/annotate_mf_all.html">annotate_mf_all</a></span>(<span class="no">example_spreadsheetb</span>)
<span class="co">#&gt; # A tibble: 7 x 6</span>
<span class="co">#&gt;   `Store Location`    Q1_2012     Q1_2013     Q1_2014     Q1_2015     Q1_2016   </span>
<span class="co">#&gt;   &lt;chr&gt;               &lt;chr&gt;       &lt;chr&gt;       &lt;chr&gt;       &lt;chr&gt;       &lt;chr&gt;     </span>
<span class="co">#&gt; 1 (highlight-FFADC5E… (highlight… (bold, hig… (highlight… (highlight… (bold, hi…</span>
<span class="co">#&gt; 2 (highlight-FFADC5E… (highlight… (highlight… (highlight… (bold, hig… (highligh…</span>
<span class="co">#&gt; 3 (highlight-FFF37B7… (bold, hig… (highlight… (highlight… (highlight… (highligh…</span>
<span class="co">#&gt; 4 (highlight-FFADC5E… (highlight… (bold, hig… (bold, hig… (bold, hig… (highligh…</span>
<span class="co">#&gt; 5 (highlight-FFADC5E… (highlight… (highlight… (highlight… (highlight… (highligh…</span>
<span class="co">#&gt; 6 (highlight-FFF37B7… (bold, hig… (highlight… (bold, hig… (highlight… (bold, hi…</span>
<span class="co">#&gt; 7 (highlight-FFADC5E… (highlight… (highlight… (highlight… (highlight… (highligh…</span></pre></body></html></div>
</div>
</div>
<div id="character-vectors" class="section level3">
<h3 class="hasAnchor">
<a href="#character-vectors" class="anchor"></a>Character vectors</h3>
<p>Tables from PDF or other similar sources can often be imported into R as character vectors with one element for each line. These can then be parsed as fixed width files or separated into columns. <code>unheadr</code> now includes the <code><a href="../reference/regex_valign.html">regex_valign()</a></code> function for aligning elements in these vectors vertically by inserting padding whitespace (and optional separators) at positions along each line matched by a regular expression.</p>
<p>This example is based on how data on hotel guests (ID, State of Origin, and Date) in a PDF is parsed by <code><a href="https://docs.ropensci.org/pdftools/reference/pdftools.html">pdftools::pdf_text</a></code>.</p>
<div class="sourceCode" id="cb16"><html><body><pre class="r"><span class="no">guests</span> <span class="kw">&lt;-</span>
  <span class="fu"><a href="https://rdrr.io/r/base/unlist.html">unlist</a></span>(<span class="fu"><a href="https://rdrr.io/r/base/strsplit.html">strsplit</a></span>(<span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span>(<span class="st">"6     COAHUILA        20/03/2020
712        COAHUILA             20/03/2020"</span>),<span class="st">"\n"</span>))

<span class="no">guests</span>
<span class="co">#&gt; [1] "6     COAHUILA        20/03/2020"          </span>
<span class="co">#&gt; [2] "712        COAHUILA             20/03/2020"</span></pre></body></html></div>
<p>There is inconsistent whitespace between the first and second data ‘columns’. With a regular expression that matches a word boundary and uppercase letters, we can adjust the whitespace so that the matched positions are the same across lines.</p>
<div class="sourceCode" id="cb17"><html><body><pre class="r"><span class="fu"><a href="../reference/regex_valign.html">regex_valign</a></span>(<span class="no">guests</span>, <span class="st">"\\b(?=[A-Z])"</span>)
<span class="co">#&gt; [1] "6          COAHUILA        20/03/2020"     </span>
<span class="co">#&gt; [2] "712        COAHUILA             20/03/2020"</span></pre></body></html></div>
<p>This output is easier to parse with <code>readr</code> or other data-munging approaches.</p>
</div>
</div>
<div id="further-reading" class="section level2">
<h2 class="hasAnchor">
<a href="#further-reading" class="anchor"></a>Further reading</h2>
<p>The underlying reasoning, background, and possible uses of <code>unheadr</code> are described in this publication:</p>
<p><NAME>., <NAME>., <NAME>. (2018). Good practices for sharing analysis-ready data in mammalogy and biodiversity research. <em>Hystrix, the Italian Journal of Mammalogy</em>, 29(2), 155-161. <a href="https://doi.org/10.4404/hystrix-00133-2018">Open Access, 10.4404/hystrix-00133-2018</a>. </p>
</div>
  </div>

  <div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">

        <nav id="toc" data-toggle="toc"><h2 data-toc-skip>Contents</h2>
    </nav>
</div>

</div>



      <footer><div class="copyright">
  <p>Developed by <NAME>.</p>
</div>

<div class="pkgdown">
  <p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.5.1.</p>
</div>

      </footer>
</div>

  


  </body>
</html>
