<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/generating-codebooks-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.sandsynligvis.dk/articles/18/codebook.html"> sandsynligvis.dk</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><span>A codebook is a</span> technical document that provides an<br>
overview of and information about the variables in a dataset. The<br>
codebook ensures that the statistician has the complete background<br>
information necessary to undertake the analysis, and a codebook<br>
documents the data to make sure that the data is well understood and<br>
reusable in the future. Here we will show how to create codebooks in R<br>
using the <code>dataMaid</code> packages.</p>
<p><span></span></p>
<p>The help pages for the datasets in R packages usually provide thorough<br>
information although the level of detail may vary quite substantially<br>
from dataset to dataset. As an example we will consider the <code>iris</code><br>
dataset. The help page gives decent information so we will just use it<br>
to show how we would create a codebook.</p>
<div>
<pre><code><span>help</span><span>(</span><span>iris</span><span>)</span>
</code></pre>
</div>
<p>Real datasets, however, are messy and not as polished as the datasets<br>
found in R packages. A substantial amount of data wrangling, tweaks,<br>
cleaning, and custom solutions are necessary to transform the data<br>
into shape before it is ready for statistical analysis. Creating the<br>
finished dataset is not enough as it is also necessary to produce the<br>
corresponding data documentation.</p>
<p>We have <a rel="nofollow" href="http://www.sandsynligvis.dk/articles/17/datamaid.html" target="_blank">previously<br>
shown</a> how the<br>
<code>dataMaid</code> package can produce automated reports to summarise<br>
datasets, to identify potential errors, and to check the data quality<br>
and integrity.</p>
<p>The <code>dataMaid</code> package produces an <code>Rmarkdown</code> summary document with<br>
information on each variable in the data frame, and the document can<br>
be rendered to a report in HTML, pdf, or word. The final report can be<br>
given to scientific collaborators since proper data validation often<br>
requires a collaborative effort between an expert in the field and a<br>
data scientist. It is easy to tweak report generated by <code>dataMaid</code> to<br>
obtain a document that can serve as codebook for the cleaned dataset.</p>
<p>The function <code>makeCodebook()</code> accepts a data frame and produces a<br>
document that provides a summary of the data frame and its variables.</p>
<div>
<pre><code><span>library</span><span>(</span><span>dataMaid</span><span>)</span>
<span>makeCodebook</span><span>(</span><span>iris</span><span>)</span>
</code></pre>
</div>
<p>The result is the 3-page document reproduced below in the two<br>
figures. The codebook consists of 4 parts: the first two parts are<br>
tables giving an overview of the data frame and the variables. Here we<br>
see the number of observations, the number of variables, their class<br>
type, and the proportion of missing observations.</p>
<p>Part 3 lists each variable and provides class-dependent summary<br>
statistics and a data-visualisation. If a variable is a <code>factor</code> then<br>
the unique factor levels are listed beneath the summary statistics.</p>
<p>Part 4 documents the report generation information (who made it, when,<br>
directory, the function call, the operating system platform).</p>
<p><img alt="plot of chunk includeReport11" src="https://i2.wp.com/www.sandsynligvis.dk/assets/img/codebook1.png?w=456" data-recalc-dims="1"></p>
<figure><img src="https://i0.wp.com/www.sandsynligvis.dk/assets/img/codebook2.png?w=456" data-recalc-dims="1"><figcaption>The full codebook generated with the default arguments to `makeCodebook()`. The codebook created is an Rmarkdown document that can be rendered to pdf, html, or word.</figcaption></figure>
<p></p>
<p>While the information about each variable (pages 1 and 2) serves as a<br>
reference manual when doing subsequent analyses, the last page<br>
provides meta-information about the codebook to ensure documentable<br>
and reproducible research.</p>
<p>The codebooks can be improved by adding additional information about<br>
the variables. There are two ways to add extra information to the<br>
codebook. The first uses the same approach as the <code>labelled</code> package<br>
where the attribute <code>labels</code> can be set for a variable in a data frame<br>
to contain label information. These labels are set directly</p>
<div>
<pre><code><span>attr</span><span>(</span><span>iris</span><span>$</span><span>Sepal.Length</span><span>,</span> <span>&quot;labels&quot;</span><span>)</span> <span>&lt;-</span> <span>&quot;Sepal length in cm&quot;</span>
</code></pre>
</div>
<p>or it is possible to use the functions from the <code>labelled</code><br>
package. The <code>labels</code> attribute is intended for condensed information<br>
and it is particularly useful if the variable names are not<br>
meaningful. When variable names are not self-explanatory we can keep<br>
the original variable names from the raw data but provide meaningful, explanatory<br>
labels through the <code>labels</code> attribute.</p>
<p>Another type of label is the <code>shortDescription</code> attribute. This is<br>
intended to provide additional details that might come in handy<br>
later. The <code>shortDescription</code> attribute is set similarly to the<br>
<code>labels</code> attribute.</p>
<div>
<pre><code><span>attr</span><span>(</span><span>iris</span><span>$</span><span>Sepal.Length</span><span>,</span> <span>&quot;shortDescription&quot;</span><span>)</span> <span>&lt;-</span>
     <span>&quot;Measured using a line gauge produced by Acme factories.&quot;</span>
<span>attr</span><span>(</span><span>iris</span><span>$</span><span>Species</span><span>,</span> <span>&quot;shortDescription&quot;</span><span>)</span> <span>&lt;-</span> <span>paste0</span><span>(</span>
     <span>&quot;Two of the three species were collected in the Gaspé &quot;</span><span>,</span>
     <span>&quot;Peninsula all from the same pasture, and picked on the &quot;</span><span>,</span>
     <span>&quot;same day and measured at the same time by the same &quot;</span><span>,</span>
     <span>&quot;person with the same apparatus&quot;</span><span>)</span>
</code></pre>
</div>
<p>When we run <code>makeCodebook()</code> again (with argument <code>replace=TRUE</code> to<br>
overwrite the report we generated earlier) we can see the additional<br>
information appear in the codebook produced.</p>
<div>
<pre><code><span>makeCodebook</span><span>(</span><span>iris</span><span>,</span> <span>replace</span><span>=</span><span>TRUE</span><span>)</span>
</code></pre>
</div>
<p><img alt="plot of chunk includeReport21" src="https://i2.wp.com/www.sandsynligvis.dk/assets/img/codebook3.png?w=456" data-recalc-dims="1"></p>
<p><code>makeCodebook()</code> works by tweaking the arguments for<br>
<code>makeDataReport()</code> from the <code>dataMaid</code> package. The <code>makeDataReport()</code><br>
function is <a rel="nofollow" href="http://www.sandsynligvis.dk/articles/17/datamaid.html" target="_blank">very<br>
versatile</a> and<br>
it is possible to change the arguments to modify the content of the<br>
material that goes into the codebook.</p>
<p>Hopefully, the <code>makeCodebook()</code> function in the <code>dataMaid</code> package<br>
should make it easier to create and provide codebooks for small and<br>
larger projects, and will encourage more people to provide<br>
documentable and reproducible research. Comments and suggestions to<br>
expand the codebook possibilities <a rel="nofollow" href="https://github.com/ekstroem/dataMaid" target="_blank">are very<br>
welcome</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.sandsynligvis.dk/articles/18/codebook.html"> sandsynligvis.dk</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/UeOzhUhPpLc" height="1"></a></div>