adding posts

This commit is contained in:
Kyle Belanger 2023-10-12 08:52:22 -04:00
parent 78a2eb0980
commit b42aa0b54b
19 changed files with 2347 additions and 6 deletions

View file

@ -156,7 +156,7 @@ ul.task-list li input[type="checkbox"] {
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<h5 class="quarto-listing-category-title">Categories</h5><div class="quarto-listing-category category-default"><div class="category" data-category="">All <span class="quarto-category-count">(4)</span></div></div></div>
<h5 class="quarto-listing-category-title">Categories</h5><div class="quarto-listing-category category-default"><div class="category" data-category="">All <span class="quarto-category-count">(6)</span></div></div></div>
<!-- main -->
<main class="content quarto-banner-title-block column-page-left" id="quarto-document-content">
@ -169,7 +169,63 @@ ul.task-list li input[type="checkbox"] {
<div class="quarto-listing quarto-listing-container-default" id="listing-listing">
<div class="list quarto-listing-default">
<div class="quarto-post image-right" data-index="0" data-listing-date-sort="1581570000000" data-listing-file-modified-sort="1697056704620" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="11">
<div class="quarto-post image-right" data-index="0" data-listing-date-sort="1595649600000" data-listing-file-modified-sort="1697114923312" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="4">
<div class="thumbnail">
<p><a href="./posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html"> <p class="card-img-top"><img src="posts\2020-07-25_diabetes-data-collection-and-cleaning\data-cleaning.png" class="thumbnail-image card-img"/></p> </a></p>
</div>
<div class="body">
<a href="./posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html">
<h3 class="no-anchor listing-title">
Diabetes in Rural North Carolina : Data Collection and Cleaning
</h3>
<div class="listing-subtitle">
<p>This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data.</p>
</div>
<div class="listing-description">
This is the…
</div>
</a>
</div>
<div class="metadata">
<a href="./posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html">
<div class="listing-date">
Jul 25, 2020
</div>
<div class="listing-author">
Kyle Belanger
</div>
</a>
</div>
</div>
<div class="quarto-post image-right" data-index="1" data-listing-date-sort="1592798400000" data-listing-file-modified-sort="1697115048321" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="3">
<div class="thumbnail">
<p><a href="./posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html"> <p class="card-img-top"><img src="posts\2020-06-22_excel-data-multiple-headers\example_data_img1.png" alt="image of example data" class="thumbnail-image card-img"/></p> </a></p>
</div>
<div class="body">
<a href="./posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html">
<h3 class="no-anchor listing-title">
Importing Excel Data with Multiple Header Rows
</h3>
<div class="listing-subtitle">
<p>A solution for importing Excel Data that contains two header rows.</p>
</div>
<div class="listing-description">
Recently I tried to important some Microsoft Excel data into R, and ran into an issue were the data actually had two different header rows. The top row listed a group, and…
</div>
</a>
</div>
<div class="metadata">
<a href="./posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html">
<div class="listing-date">
Jun 22, 2020
</div>
<div class="listing-author">
Kyle Belanger
</div>
</a>
</div>
</div>
<div class="quarto-post image-right" data-index="2" data-listing-date-sort="1581570000000" data-listing-file-modified-sort="1697056704620" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="11">
<div class="thumbnail">
<p><a href="./posts/2020-02-13_basic-who-TB-data/basic-exploration-of-who-tuberculosis-data.html"> <p class="card-img-top"><img src="posts\2020-02-13_basic-who-TB-data\basic-exploration-of-who-tuberculosis-data_files\figure-html\unnamed-chunk-5-1.png" class="thumbnail-image card-img"/></p> </a></p>
</div>
@ -197,7 +253,7 @@ Kyle Belanger
</a>
</div>
</div>
<div class="quarto-post image-right" data-index="1" data-listing-date-sort="1581310800000" data-listing-file-modified-sort="1697114568395" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="8">
<div class="quarto-post image-right" data-index="3" data-listing-date-sort="1581310800000" data-listing-file-modified-sort="1697114568395" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="8">
<div class="thumbnail">
<p><a href="./posts/2020-02-10_line-graphs-and-interactivity/line-graphs-and-interactivity.html"> <p class="card-img-top"><img src="posts\2020-02-10_line-graphs-and-interactivity\flu_surveillance.png" alt="Example Line Graph" class="thumbnail-image card-img"/></p> </a></p>
</div>
@ -225,7 +281,7 @@ Kyle Belanger
</a>
</div>
</div>
<div class="quarto-post image-right" data-index="2" data-listing-date-sort="1580274000000" data-listing-file-modified-sort="1697114439706" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="3">
<div class="quarto-post image-right" data-index="4" data-listing-date-sort="1580274000000" data-listing-file-modified-sort="1697114439706" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="3">
<div class="thumbnail">
<p><a href="./posts/2020-01-29_facets-and-humility/facets-and-a-lesson-in-humility.html"> <p class="card-img-top"><img src="posts\2020-01-29_facets-and-humility\WHO_LIFE.png" class="thumbnail-image card-img"/></p> </a></p>
</div>
@ -253,7 +309,7 @@ Kyle Belanger
</a>
</div>
</div>
<div class="quarto-post image-right" data-index="3" data-listing-date-sort="1579842000000" data-listing-file-modified-sort="1697052101192" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="2">
<div class="quarto-post image-right" data-index="5" data-listing-date-sort="1579842000000" data-listing-file-modified-sort="1697052101192" data-listing-date-modified-sort="NaN" data-listing-reading-time-sort="2">
<div class="thumbnail">
<p><a href="./posts/2020-01-04_my-start-to-r/my-start-to-r.html"> <p class="card-img-top"><img src="posts\2020-01-04_my-start-to-r\my-start-to-r_files\figure-html\unnamed-chunk-5-1.png" class="thumbnail-image card-img"/></p> </a></p>
</div>

View file

@ -2,6 +2,8 @@
{
"listing": "/blog.html",
"items": [
"/posts/2020-07-25_diabetes-data-collection-and-cleaning/diabetes-in-rural-north-carolina-data-collection-and-cleaning.html",
"/posts/2020-06-22_excel-data-multiple-headers/importing-excel-data-with-multiple-header-rows.html",
"/posts/2020-02-13_basic-who-TB-data/basic-exploration-of-who-tuberculosis-data.html",
"/posts/2020-02-10_line-graphs-and-interactivity/line-graphs-and-interactivity.html",
"/posts/2020-01-29_facets-and-humility/facets-and-a-lesson-in-humility.html",

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View file

@ -0,0 +1,625 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Kyle Belanger">
<meta name="dcterms.date" content="2020-06-22">
<title>Kyle Belanger - Importing Excel Data with Multiple Header Rows</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="../../site_libs/quarto-nav/quarto-nav.js"></script>
<script src="../../site_libs/quarto-nav/headroom.min.js"></script>
<script src="../../site_libs/clipboard/clipboard.min.js"></script>
<script src="../../site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="../../site_libs/quarto-search/fuse.min.js"></script>
<script src="../../site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="../../">
<script src="../../site_libs/quarto-html/quarto.js"></script>
<script src="../../site_libs/quarto-html/popper.min.js"></script>
<script src="../../site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="../../site_libs/quarto-html/anchor.min.js"></script>
<link href="../../site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="../../site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="../../site_libs/bootstrap/bootstrap.min.js"></script>
<link href="../../site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="../../site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "navbar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "end",
"type": "overlay",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<link rel="stylesheet" href="../../styles.css">
</head>
<body class="floating nav-fixed">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="navbar navbar-expand-lg navbar-dark ">
<div class="navbar-container container-fluid">
<div class="navbar-brand-container">
<a class="navbar-brand" href="../../index.html">
<span class="navbar-title">Kyle Belanger</span>
</a>
</div>
<div id="quarto-search" class="" title="Search"></div>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav navbar-nav-scroll ms-auto">
<li class="nav-item">
<a class="nav-link" href="../../blog.html" rel="" target="">
<span class="menu-text">Posts</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="../../kyle_resume.pdf" rel="" target="">
<span class="menu-text">Resume</span></a>
</li>
<li class="nav-item compact">
<a class="nav-link" href="https://github.com/mmmmtoasty19" rel="" target=""><i class="bi bi-github" role="img">
</i>
<span class="menu-text"></span></a>
</li>
</ul>
<div class="quarto-navbar-tools">
</div>
</div> <!-- /navcollapse -->
</div> <!-- /container-fluid -->
</nav>
</header>
<!-- content -->
<header id="title-block-header" class="quarto-title-block default toc-left page-columns page-full">
<div class="quarto-title-banner page-columns page-full">
<div class="quarto-title column-body">
<h1 class="title">Importing Excel Data with Multiple Header Rows</h1>
<p class="subtitle lead"></p><p>A solution for importing Excel Data that contains two header rows.</p><p></p>
</div>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p><a href="https://kyleb.rbind.io/">Kyle Belanger</a> </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">June 22, 2020</p>
</div>
</div>
</div>
</header><div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article page-navbar">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal sidebar-navigation floating overflow-auto">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#problem" id="toc-problem" class="nav-link active" data-scroll-target="#problem">Problem</a></li>
<li><a href="#example-data" id="toc-example-data" class="nav-link" data-scroll-target="#example-data">Example Data</a></li>
<li><a href="#check-data" id="toc-check-data" class="nav-link" data-scroll-target="#check-data">Check Data</a></li>
<li><a href="#new-header-names" id="toc-new-header-names" class="nav-link" data-scroll-target="#new-header-names">New Header Names</a>
<ul class="collapse">
<li><a href="#step-1" id="toc-step-1" class="nav-link" data-scroll-target="#step-1">Step 1</a></li>
<li><a href="#step-2" id="toc-step-2" class="nav-link" data-scroll-target="#step-2">Step 2</a></li>
<li><a href="#step-3" id="toc-step-3" class="nav-link" data-scroll-target="#step-3">Step 3</a></li>
<li><a href="#step-4" id="toc-step-4" class="nav-link" data-scroll-target="#step-4">Step 4</a></li>
<li><a href="#step-4a" id="toc-step-4a" class="nav-link" data-scroll-target="#step-4a">Step 4a</a></li>
<li><a href="#step-5" id="toc-step-5" class="nav-link" data-scroll-target="#step-5">Step 5</a></li>
</ul></li>
<li><a href="#final-data" id="toc-final-data" class="nav-link" data-scroll-target="#final-data">Final Data</a></li>
<li><a href="#other-help" id="toc-other-help" class="nav-link" data-scroll-target="#other-help">Other Help</a></li>
</ul>
</nav>
</nav>
<div id="quarto-sidebar-glass" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
</div>
<!-- main -->
<main class="content quarto-banner-title-block" id="quarto-document-content">
<section id="problem" class="level1">
<h1>Problem</h1>
<p>Recently I tried to important some Microsoft Excel data into R, and ran into an issue were the data actually had two different header rows. The top row listed a group, and then the second row listed a category within that group. Searching goggle I couldnt really find a good example of what I was looking for, so I am putting it here in hopes of helping someone else!</p>
</section>
<section id="example-data" class="level1">
<h1>Example Data</h1>
<p>I have created a small Excel file to demonstrate what I am talking about. Download it <a href="https://github.com/mmmmtoasty19/kyleb/tree/master/content/post/2020-06-15-importing-excel-data-with-multiple-headers/example_data.xlsx">here</a>. This is the data from Excel. <img src="example_data_img1.png" class="img-fluid" alt="image of example data"></p>
</section>
<section id="check-data" class="level1">
<h1>Check Data</h1>
<p>First we will read the file in using the package readxl and view the data without doing anything special to it.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readxl) <span class="co"># load the readxl library</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse) <span class="co"># load the tidyverse for manipulating the data</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>file_path <span class="ot">&lt;-</span> <span class="st">"example_data.xlsx"</span> <span class="co"># set the file path</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>ds0 <span class="ot">&lt;-</span> <span class="fu">read_excel</span>(file_path) <span class="co"># read the file</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a>ds0</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 7
Name `Test 1` ...3 ...4 `Test 2` ...6 ...7
&lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
1 &lt;NA&gt; Run 1 Run 2 Run 3 Run 1 Run 2 Run 3
2 Max 22 23 24 25 26 27
3 Phoebe 34 34 32 34 51 12
4 Scamp 35 36 21 22 23 24
5 Chance 1234 1235 1236 1267 173 1233
6 Aimee 420 123 690 42 45 12
7 Kyle 22 23 25 26 67 54 </code></pre>
</div>
</div>
</section>
<section id="new-header-names" class="level1">
<h1>New Header Names</h1>
<section id="step-1" class="level3">
<h3 class="anchored" data-anchor-id="step-1">Step 1</h3>
<p>First lets read back the data, this time however with some options. We will set the n_max equal to 2, to only read the first two rows, and set col_names to FALSE so we do not read the first row as headers.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>ds1 <span class="ot">&lt;-</span> <span class="fu">read_excel</span>(file_path, <span class="at">n_max =</span> <span class="dv">2</span>, <span class="at">col_names =</span> <span class="cn">FALSE</span>)</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>ds1</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 2 × 7
...1 ...2 ...3 ...4 ...5 ...6 ...7
&lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
1 Name Test 1 &lt;NA&gt; &lt;NA&gt; Test 2 &lt;NA&gt; &lt;NA&gt;
2 &lt;NA&gt; Run 1 Run 2 Run 3 Run 1 Run 2 Run 3</code></pre>
</div>
</div>
</section>
<section id="step-2" class="level3">
<h3 class="anchored" data-anchor-id="step-2">Step 2</h3>
<p>Now that we have our headers lets first transpose them to a vertical matrix using the base function t(), then we will turn it back into a tibble to allow us to use tidyr fill function.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>names <span class="ot">&lt;-</span> ds1 <span class="sc">%&gt;%</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">t</span>() <span class="sc">%&gt;%</span> <span class="co">#transpose to a matrix</span></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">as_tibble</span>() <span class="co">#back to tibble</span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>names</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 2
V1 V2
&lt;chr&gt; &lt;chr&gt;
1 Name &lt;NA&gt;
2 Test 1 Run 1
3 &lt;NA&gt; Run 2
4 &lt;NA&gt; Run 3
5 Test 2 Run 1
6 &lt;NA&gt; Run 2
7 &lt;NA&gt; Run 3</code></pre>
</div>
</div>
<p>Note that tidyr fill can not work row wise, thus the need to flip the tibble so it is long vs wide.</p>
</section>
<section id="step-3" class="level3">
<h3 class="anchored" data-anchor-id="step-3">Step 3</h3>
<p>Now we use tidyr fill function to fill the NAs with whatever value it finds above.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>names <span class="ot">&lt;-</span> names <span class="sc">%&gt;%</span> <span class="fu">fill</span>(V1) <span class="co">#use dplyr fill to fill in the NA's</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>names</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 2
V1 V2
&lt;chr&gt; &lt;chr&gt;
1 Name &lt;NA&gt;
2 Test 1 Run 1
3 Test 1 Run 2
4 Test 1 Run 3
5 Test 2 Run 1
6 Test 2 Run 2
7 Test 2 Run 3</code></pre>
</div>
</div>
</section>
<section id="step-4" class="level3">
<h3 class="anchored" data-anchor-id="step-4">Step 4</h3>
<p>This is where my data differed from many of the examples I could find online. Because the second row is also a header we can not just get rid of them. We can solve this using paste() combined with dplyr mutate to form a new column that combines the first and second column.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>names <span class="ot">&lt;-</span> names <span class="sc">%&gt;%</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="at">new_names =</span> <span class="fu">paste</span>(V1,V2, <span class="at">sep =</span> <span class="st">"_"</span>)</span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a>names</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 3
V1 V2 new_names
&lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
1 Name &lt;NA&gt; Name_NA
2 Test 1 Run 1 Test 1_Run 1
3 Test 1 Run 2 Test 1_Run 2
4 Test 1 Run 3 Test 1_Run 3
5 Test 2 Run 1 Test 2_Run 1
6 Test 2 Run 2 Test 2_Run 2
7 Test 2 Run 3 Test 2_Run 3</code></pre>
</div>
</div>
</section>
<section id="step-4a" class="level3">
<h3 class="anchored" data-anchor-id="step-4a">Step 4a</h3>
<p>One more small clean up task, in the example data the first column header Name, did not have a second label, this has created a name with an NA attached. We can use stringr to remove this NA.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>names <span class="ot">&lt;-</span> names <span class="sc">%&gt;%</span> <span class="fu">mutate</span>(<span class="fu">across</span>(new_names, <span class="sc">~</span><span class="fu">str_remove_all</span>(.,<span class="st">"_NA"</span>)))</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>names</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 7 × 3
V1 V2 new_names
&lt;chr&gt; &lt;chr&gt; &lt;chr&gt;
1 Name &lt;NA&gt; Name
2 Test 1 Run 1 Test 1_Run 1
3 Test 1 Run 2 Test 1_Run 2
4 Test 1 Run 3 Test 1_Run 3
5 Test 2 Run 1 Test 2_Run 1
6 Test 2 Run 2 Test 2_Run 2
7 Test 2 Run 3 Test 2_Run 3</code></pre>
</div>
</div>
</section>
<section id="step-5" class="level3">
<h3 class="anchored" data-anchor-id="step-5">Step 5</h3>
<p>Now that are new name column is the way we want it, we can use dpylrs pull to return a vector of just that column</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>names <span class="ot">&lt;-</span> names <span class="sc">%&gt;%</span> <span class="fu">pull</span>(new_names)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
<section id="final-data" class="level1">
<h1>Final Data</h1>
<p>Now that we have a vector of column names lets read in the original file using our new names. We set the skip argument to 2, to skip the first two rows, and set col_names equal to our vector of names. Note the last step I used the janitor package to provide names in snake case (the default for the clean names function.)</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>example_data <span class="ot">&lt;-</span> readxl<span class="sc">::</span><span class="fu">read_excel</span>(file_path, <span class="at">col_names =</span> names, <span class="at">skip =</span> <span class="dv">2</span>) <span class="sc">%&gt;%</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a> janitor<span class="sc">::</span><span class="fu">clean_names</span>()</span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a>example_data</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 6 × 7
name test_1_run_1 test_1_run_2 test_1_run_3 test_2_run_1 test_2_run_2
&lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt;
1 Max 22 23 24 25 26
2 Phoebe 34 34 32 34 51
3 Scamp 35 36 21 22 23
4 Chance 1234 1235 1236 1267 173
5 Aimee 420 123 690 42 45
6 Kyle 22 23 25 26 67
# 1 more variable: test_2_run_3 &lt;dbl&gt;</code></pre>
</div>
</div>
</section>
<section id="other-help" class="level1">
<h1>Other Help</h1>
<p>While searching for some solutions to my problem I found two good examples, however neither did exactly what I was trying to do.</p>
<ol type="1">
<li><p>This post by Lisa Deburine is pretty close to what I was trying to accomplish and gave me a good starting point. Read it <a href="https://debruine.github.io/posts/multi-row-headers/">here</a></p></li>
<li><p>This post by Alison Hill solves a simlar but slightly different problem. In her data the 2nd row is actually metadata not a second set of headers. Read it <a href="https://alison.rbind.io/post/2018-02-23-read-multiple-header-rows/">here</a></p></li>
</ol>
</section>
<div id="quarto-appendix" class="default"><section class="quarto-appendix-contents"><h2 class="anchored quarto-appendix-heading">Reuse</h2><div id="quarto-reuse" class="quarto-appendix-contents"><div><a rel="license" href="https://creativecommons.org/licenses/by/4.0/">https://creativecommons.org/licenses/by/4.0/</a></div></div></section><section class="quarto-appendix-contents"><h2 class="anchored quarto-appendix-heading">Citation</h2><div><div class="quarto-appendix-secondary-label">BibTeX citation:</div><pre class="sourceCode code-with-copy quarto-appendix-bibtex"><code class="sourceCode bibtex">@online{belanger2020,
author = {Belanger, Kyle},
title = {Importing {Excel} {Data} with {Multiple} {Header} {Rows}},
date = {2020-06-22},
langid = {en}
}
</code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre><div class="quarto-appendix-secondary-label">For attribution, please cite this work as:</div><div id="ref-belanger2020" class="csl-entry quarto-appendix-citeas" role="listitem">
Belanger, Kyle. 2020. <span>“Importing Excel Data with Multiple Header
Rows.”</span> June 22, 2020.
</div></div></section></div></main> <!-- /main -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const clipboard = new window.ClipboardJS('.code-copy-button', {
text: function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
let selectedAnnoteEl;
const selectorForAnnotation = ( cell, annotation) => {
let cellAttr = 'data-code-cell="' + cell + '"';
let lineAttr = 'data-code-annotation="' + annotation + '"';
const selector = 'span[' + cellAttr + '][' + lineAttr + ']';
return selector;
}
const selectCodeLines = (annoteEl) => {
const doc = window.document;
const targetCell = annoteEl.getAttribute("data-target-cell");
const targetAnnotation = annoteEl.getAttribute("data-target-annotation");
const annoteSpan = window.document.querySelector(selectorForAnnotation(targetCell, targetAnnotation));
const lines = annoteSpan.getAttribute("data-code-lines").split(",");
const lineIds = lines.map((line) => {
return targetCell + "-" + line;
})
let top = null;
let height = null;
let parent = null;
if (lineIds.length > 0) {
//compute the position of the single el (top and bottom and make a div)
const el = window.document.getElementById(lineIds[0]);
top = el.offsetTop;
height = el.offsetHeight;
parent = el.parentElement.parentElement;
if (lineIds.length > 1) {
const lastEl = window.document.getElementById(lineIds[lineIds.length - 1]);
const bottom = lastEl.offsetTop + lastEl.offsetHeight;
height = bottom - top;
}
if (top !== null && height !== null && parent !== null) {
// cook up a div (if necessary) and position it
let div = window.document.getElementById("code-annotation-line-highlight");
if (div === null) {
div = window.document.createElement("div");
div.setAttribute("id", "code-annotation-line-highlight");
div.style.position = 'absolute';
parent.appendChild(div);
}
div.style.top = top - 2 + "px";
div.style.height = height + 4 + "px";
let gutterDiv = window.document.getElementById("code-annotation-line-highlight-gutter");
if (gutterDiv === null) {
gutterDiv = window.document.createElement("div");
gutterDiv.setAttribute("id", "code-annotation-line-highlight-gutter");
gutterDiv.style.position = 'absolute';
const codeCell = window.document.getElementById(targetCell);
const gutter = codeCell.querySelector('.code-annotation-gutter');
gutter.appendChild(gutterDiv);
}
gutterDiv.style.top = top - 2 + "px";
gutterDiv.style.height = height + 4 + "px";
}
selectedAnnoteEl = annoteEl;
}
};
const unselectCodeLines = () => {
const elementsIds = ["code-annotation-line-highlight", "code-annotation-line-highlight-gutter"];
elementsIds.forEach((elId) => {
const div = window.document.getElementById(elId);
if (div) {
div.remove();
}
});
selectedAnnoteEl = undefined;
};
// Attach click handler to the DT
const annoteDls = window.document.querySelectorAll('dt[data-target-cell]');
for (const annoteDlNode of annoteDls) {
annoteDlNode.addEventListener('click', (event) => {
const clickedEl = event.target;
if (clickedEl !== selectedAnnoteEl) {
unselectCodeLines();
const activeEl = window.document.querySelector('dt[data-target-cell].code-annotation-active');
if (activeEl) {
activeEl.classList.remove('code-annotation-active');
}
selectCodeLines(clickedEl);
clickedEl.classList.add('code-annotation-active');
} else {
// Unselect the line
unselectCodeLines();
clickedEl.classList.remove('code-annotation-active');
}
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,8 @@
$(document).ready(function(){
if (typeof $('[data-toggle="tooltip"]').tooltip === 'function') {
$('[data-toggle="tooltip"]').tooltip();
}
if ($('[data-toggle="popover"]').popover === 'function') {
$('[data-toggle="popover"]').popover();
}
});

View file

@ -0,0 +1,272 @@
/*!
* lightable v0.0.1
* Copyright 2020 Hao Zhu
* Licensed under MIT (https://github.com/haozhu233/kableExtra/blob/master/LICENSE)
*/
.lightable-minimal {
border-collapse: separate;
border-spacing: 16px 1px;
width: 100%;
margin-bottom: 10px;
}
.lightable-minimal td {
margin-left: 5px;
margin-right: 5px;
}
.lightable-minimal th {
margin-left: 5px;
margin-right: 5px;
}
.lightable-minimal thead tr:last-child th {
border-bottom: 2px solid #00000050;
empty-cells: hide;
}
.lightable-minimal tbody tr:first-child td {
padding-top: 0.5em;
}
.lightable-minimal.lightable-hover tbody tr:hover {
background-color: #f5f5f5;
}
.lightable-minimal.lightable-striped tbody tr:nth-child(even) {
background-color: #f5f5f5;
}
.lightable-classic {
border-top: 0.16em solid #111111;
border-bottom: 0.16em solid #111111;
width: 100%;
margin-bottom: 10px;
margin: 10px 5px;
}
.lightable-classic tfoot tr td {
border: 0;
}
.lightable-classic tfoot tr:first-child td {
border-top: 0.14em solid #111111;
}
.lightable-classic caption {
color: #222222;
}
.lightable-classic td {
padding-left: 5px;
padding-right: 5px;
color: #222222;
}
.lightable-classic th {
padding-left: 5px;
padding-right: 5px;
font-weight: normal;
color: #222222;
}
.lightable-classic thead tr:last-child th {
border-bottom: 0.10em solid #111111;
}
.lightable-classic.lightable-hover tbody tr:hover {
background-color: #F9EEC1;
}
.lightable-classic.lightable-striped tbody tr:nth-child(even) {
background-color: #f5f5f5;
}
.lightable-classic-2 {
border-top: 3px double #111111;
border-bottom: 3px double #111111;
width: 100%;
margin-bottom: 10px;
}
.lightable-classic-2 tfoot tr td {
border: 0;
}
.lightable-classic-2 tfoot tr:first-child td {
border-top: 3px double #111111;
}
.lightable-classic-2 caption {
color: #222222;
}
.lightable-classic-2 td {
padding-left: 5px;
padding-right: 5px;
color: #222222;
}
.lightable-classic-2 th {
padding-left: 5px;
padding-right: 5px;
font-weight: normal;
color: #222222;
}
.lightable-classic-2 tbody tr:last-child td {
border-bottom: 3px double #111111;
}
.lightable-classic-2 thead tr:last-child th {
border-bottom: 1px solid #111111;
}
.lightable-classic-2.lightable-hover tbody tr:hover {
background-color: #F9EEC1;
}
.lightable-classic-2.lightable-striped tbody tr:nth-child(even) {
background-color: #f5f5f5;
}
.lightable-material {
min-width: 100%;
white-space: nowrap;
table-layout: fixed;
font-family: Roboto, sans-serif;
border: 1px solid #EEE;
border-collapse: collapse;
margin-bottom: 10px;
}
.lightable-material tfoot tr td {
border: 0;
}
.lightable-material tfoot tr:first-child td {
border-top: 1px solid #EEE;
}
.lightable-material th {
height: 56px;
padding-left: 16px;
padding-right: 16px;
}
.lightable-material td {
height: 52px;
padding-left: 16px;
padding-right: 16px;
border-top: 1px solid #eeeeee;
}
.lightable-material.lightable-hover tbody tr:hover {
background-color: #f5f5f5;
}
.lightable-material.lightable-striped tbody tr:nth-child(even) {
background-color: #f5f5f5;
}
.lightable-material.lightable-striped tbody td {
border: 0;
}
.lightable-material.lightable-striped thead tr:last-child th {
border-bottom: 1px solid #ddd;
}
.lightable-material-dark {
min-width: 100%;
white-space: nowrap;
table-layout: fixed;
font-family: Roboto, sans-serif;
border: 1px solid #FFFFFF12;
border-collapse: collapse;
margin-bottom: 10px;
background-color: #363640;
}
.lightable-material-dark tfoot tr td {
border: 0;
}
.lightable-material-dark tfoot tr:first-child td {
border-top: 1px solid #FFFFFF12;
}
.lightable-material-dark th {
height: 56px;
padding-left: 16px;
padding-right: 16px;
color: #FFFFFF60;
}
.lightable-material-dark td {
height: 52px;
padding-left: 16px;
padding-right: 16px;
color: #FFFFFF;
border-top: 1px solid #FFFFFF12;
}
.lightable-material-dark.lightable-hover tbody tr:hover {
background-color: #FFFFFF12;
}
.lightable-material-dark.lightable-striped tbody tr:nth-child(even) {
background-color: #FFFFFF12;
}
.lightable-material-dark.lightable-striped tbody td {
border: 0;
}
.lightable-material-dark.lightable-striped thead tr:last-child th {
border-bottom: 1px solid #FFFFFF12;
}
.lightable-paper {
width: 100%;
margin-bottom: 10px;
color: #444;
}
.lightable-paper tfoot tr td {
border: 0;
}
.lightable-paper tfoot tr:first-child td {
border-top: 1px solid #00000020;
}
.lightable-paper thead tr:last-child th {
color: #666;
vertical-align: bottom;
border-bottom: 1px solid #00000020;
line-height: 1.15em;
padding: 10px 5px;
}
.lightable-paper td {
vertical-align: middle;
border-bottom: 1px solid #00000010;
line-height: 1.15em;
padding: 7px 5px;
}
.lightable-paper.lightable-hover tbody tr:hover {
background-color: #F9EEC1;
}
.lightable-paper.lightable-striped tbody tr:nth-child(even) {
background-color: #00000008;
}
.lightable-paper.lightable-striped tbody td {
border: 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

View file

@ -0,0 +1,47 @@
#These first few lines run only when the file is run in RStudio, !!NOT when an Rmd/Rnw file calls it!!
rm(list=ls(all=TRUE)) #Clear the variables from previous runs.
cat("\f") # clear console
# ---- example-data ----
library(readxl) # load the readxl library
library(tidyverse) # load the tidyverse for manipulating the data
file_path <- "example_data.xlsx" # set the file path
ds0 <- read_excel(file_path) # read the file
ds0
# ---- fix-it-data ----
ds1 <- read_excel(file_path, n_max = 2, col_names = FALSE)
ds1
# ---- fix-it-names ----
names <- ds1 %>%
t() %>% #transpose to a matrix
as_tibble() #back to tibble
names
# ---- fix-it-names-1 ----
names <- names %>% fill(V1) #use dplyr fill to fill in the NA's
names
# ---- fix-it-names-2 ----
names <- names %>%
mutate(
new_names = paste(V1,V2, sep = "_")
)
names
# ---- fix-it-names-3 ----
names <- names %>% mutate(across(new_names, ~str_remove_all(.,"_NA")))
names
# ---- fix-it-names-4 ----
names <- names %>% pull(new_names)
# ---- fix-it-final ----
example_data <- readxl::read_excel(file_path, col_names = names, skip = 2) %>%
janitor::clean_names()
example_data

View file

@ -0,0 +1,99 @@
---
title: "Importing Excel Data with Multiple Header Rows"
subtitle: |
A solution for importing Excel Data that contains two header rows.
date: 06-22-2020
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, echo=FALSE}
knitr::read_chunk("excel_headers.R")
```
# Problem
Recently I tried to important some Microsoft Excel data into R, and ran into an issue were the data actually had two different header rows. The top row listed a group, and then the second row listed a category within that group. Searching goggle I couldn't really find a good example of what I was looking for, so I am putting it here in hopes of helping someone else!
# Example Data
I have created a small Excel file to demonstrate what I am talking about. Download it [here](https://github.com/mmmmtoasty19/kyleb/tree/master/content/post/2020-06-15-importing-excel-data-with-multiple-headers/example_data.xlsx). This is the data from Excel.
![image of example data](example_data_img1.png)
# Check Data
First we will read the file in using the package readxl and view the data without doing anything special to it.
```{r example-data}
```
# New Header Names
### Step 1
First lets read back the data, this time however with some options. We will set the n_max equal to 2, to only read the first two rows, and set col_names to FALSE so we do not read the first row as headers.
```{r fix-it-data }
```
### Step 2
Now that we have our headers lets first transpose them to a vertical matrix using the base function t(), then we will turn it back into a tibble to allow us to use tidyr fill function.
```{r fix-it-names}
```
Note that tidyr fill can not work row wise, thus the need to flip the tibble so it is long vs wide.
### Step 3
Now we use tidyr fill function to fill the NA's with whatever value it finds above.
```{r fix-it-names-1}
```
### Step 4
This is where my data differed from many of the examples I could find online. Because the second row is also a header we can not just get rid of them. We can solve this using paste() combined with dplyr mutate to form a new column that combines the first and second column.
```{r fix-it-names-2}
```
### Step 4a
One more small clean up task, in the example data the first column header Name, did not have a second label, this has created a name with an NA attached. We can use stringr to remove this NA.
```{r fix-it-names-3}
```
### Step 5
Now that are new name column is the way we want it, we can use dpylrs pull to return a vector of just that column
```{r fix-it-names-4}
```
# Final Data
Now that we have a vector of column names lets read in the original file using our new names. We set the skip argument to 2, to skip the first two rows, and set col_names equal to our vector of names. Note the last step I used the janitor package to provide names in snake case (the default for the clean names function.)
```{r fix-it-final}
```
# Other Help
While searching for some solutions to my problem I found two good examples, however neither did exactly what I was trying to do.
1. This post by Lisa Deburine is pretty close to what I was trying to accomplish and gave me a good starting point. Read it [here](https://debruine.github.io/posts/multi-row-headers/)
2. This post by Alison Hill solves a simlar but slightly different problem. In her data the 2nd row is actually metadata not a second set of headers. Read it [here](https://alison.rbind.io/post/2018-02-23-read-multiple-header-rows/)

View file

@ -0,0 +1,5 @@
Measure,Data Source,First Year Available
Adult smoking,Behavioral Risk Factor Surveillance System,2010
Adult obesity,CDC Diabetes Interactive Atlas,2010
Physical inactivity,CDC Diabetes Interactive Atlas,2011
Food environment index,"USDA Food Environment Atlas, Map the Meal Gap",2014
1 Measure Data Source First Year Available
2 Adult smoking Behavioral Risk Factor Surveillance System 2010
3 Adult obesity CDC Diabetes Interactive Atlas 2010
4 Physical inactivity CDC Diabetes Interactive Atlas 2011
5 Food environment index USDA Food Environment Atlas, Map the Meal Gap 2014

Binary file not shown.

After

Width:  |  Height:  |  Size: 71 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View file

@ -0,0 +1,5 @@
Data,Structure,Source,Notes
2010 Census Rural/Urban Housing,one row per county,US Census,
County Health Rankings,"one row per county, year",County Health Rankings,Raw data is one year per file
Population Estimates,"one row per county, year, age group",US Census,
Diabetes Data,"one row per county, year",CDC Diabetes Atlas,Raw data is one year per file
1 Data Structure Source Notes
2 2010 Census Rural/Urban Housing one row per county US Census
3 County Health Rankings one row per county, year County Health Rankings Raw data is one year per file
4 Population Estimates one row per county, year, age group US Census
5 Diabetes Data one row per county, year CDC Diabetes Atlas Raw data is one year per file

View file

@ -0,0 +1,123 @@
---
title: "Diabetes in Rural North Carolina : Data Collection and Cleaning"
subtitle: |
This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data.
date: 07-25-2020
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
# Abstract
This is the second post in the series exploring Diabetes in rural North Carolina. This post will explore the data used for this project, from collection, cleaning, and analysis ready data.
# Data
## Overall
Overall there are four data sources that have been used to create the analysis ready data for this project. There is one additional metadata file that contains the list of all county FIP codes, used for linking the various data sets. All data sets use the county FIPS as the county identifier, the county name is added at the end using the metadata. The image below shows the steps taken to achieve the analysis data set, as well as a table below showing the structure of each data set.
![](data-cleaning.png)
---
```{r}
library(magrittr)
data_sources <- readr::read_csv("data-sources-descrip.csv")
data_sources %>% knitr::kable(caption = "Data Sources") %>% kableExtra::kable_styling()
```
## Rural Housing
The first data set comes from the [US Census](https://data.census.gov/cedsci/table?q=rural%20house&hidePreview=true&tid=DECENNIALSF12010.H2&vintage=2010&tp=true&g=0100000US.050000), and contains the amount of housing units inside both Urban and Rural areas. The raw data was taken and used to calculate the percentage of housing units in rural areas, as well as adding the classifications of Rural, Mostly Rural, and Mostly Urban. More about these classifications can be read [here](https://www2.census.gov/geo/pdfs/reference/ua/Defining_Rural.pdf). This data set is from the 2010 US Census, which is then used to set the rural classification until the next Census (2020).
View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-census-rural-housing.md)
```{r}
ds_rural_housing <- readr::read_csv(
"https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/percentage-rural.csv")
head(ds_rural_housing) %>% janitor::clean_names(case = "title") %>%
knitr::kable(align = 'c', caption = "Rural Housing Data Set") %>%
kableExtra::kable_styling(full_width = TRUE) %>%
kableExtra::footnote(general = "Displaying 6 of 3,143 rows")
```
## County Health Rankings
The second data set comes from [County Health Rankings](https://www.countyhealthrankings.org/) and contains data for the risk factors associated with diabetes, this data set is complied from many different data sources. The data was downloaded by year, and then combine to form one data set. County Health Rankings uses this data to rate health outcomes across all counties of the United States, for this analysis four categories have been extracted from the overall data set. Note that the food environment index is itself a combine measure, it is a score of both access to healthy food based on distance to grocery stores, as well as access based on cost.
View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-county-rankings-national.md)
```{r}
county_risk_sources <- readr::read_csv("county_health_rankings_sources.csv")
county_risk_sources %>% knitr::kable(caption = "County Health Rankings Sources") %>% kableExtra::kable_styling() %>%
kableExtra::footnote(general = "https://www.countyhealthrankings.org/explore-health-rankings/measures-data-sources/2020-measures" , general_title = "Source: ")
```
---
```{r}
ds_county_risk <- readr::read_csv(
"https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/national-diabetes-risk-factors-2010-2020.csv"
)
head(ds_county_risk) %>% janitor::clean_names(case = "title") %>%
knitr::kable(align = 'c', caption = "County Risk Factors Data Set") %>%
kableExtra::kable_styling() %>%
kableExtra::footnote(general = "Displaying 6 of 34,555 rows")
```
## Population Estimates
The third data set also comes from the [US Census](https://www.census.gov/data/tables/time-series/demo/popest/2010s-counties-detail.html) and contains population estimates for each county in the United States broken down by: year, age-group, sex, race, and ethnicity. For each row in the table the percent of each type of population was calculated using the yearly population total for the county. This breakdown is useful for this project as African-Americans and Hispanics suffer from diabetes at a higher rate then other groups.
View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-us-county-population-estimates.md)
```{r}
ds_population <- readr::read_csv(
"https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/us-county-population-estimates-v2.csv"
)
head(ds_population) %>% janitor::clean_names(case = "title") %>%
# dplyr::select(1:6) %>%
knitr::kable(caption = "US Population Estimates Data Set") %>%
kableExtra::kable_styling() %>%
kableExtra::scroll_box(width = "100%") %>%
kableExtra::footnote(general = "Displaying 6 of 565560 rows")
```
## Diabetes Percentages
The final data set comes from the [CDC Diabetes Atlas](https://www.cdc.gov/diabetes/data/index.html) and contains the estimated prevalence of diabetes in each county of the United States, by year. The data set also includes the upper and lower estimated limits, see the [previous post](https://kyleb.rbind.io/post/2020-06-25-diabetes-1/) for an explanation of how these numbers are calculated. The data was downloaded by year, and then merged into one data set for the project.
View greeter script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/0-greeter-us-diabetes.md)
```{r}
ds_diabetes <- readr::read_csv(
"https://raw.githubusercontent.com/mmmmtoasty19/nc-diabetes-epidemic-2020/master/data-public/derived/us-diabetes-data.csv"
)
head(ds_diabetes) %>% janitor::clean_names(case = "title") %>%
knitr::kable(caption = "US Diabetes Data") %>%
kableExtra::kable_styling()
```
---
# Analyis Data
After all data has been made ready the data is joined into a final analysis ready data set. Each row of this data set represents, one county, one year, and one age group. The data is also filtered to remove Alaska and Hawaii as these states could affect trends seen in the continental United States.
View the scribe script [here](https://github.com/mmmmtoasty19/nc-diabetes-epidemic-2020/blob/master/manipulation/publish/1-scribe-diabetes-data-set.md)
![](data-join.png)