<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/data-validation-with-the-assertr-package/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.onthelambda.com/2017/03/20/data-validation-with-the-assertr-package/"> R – On the lambda</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><small><i>Version 2.0 of my data set validation package <code>assertr</code> hit CRAN just this weekend. It has some pretty great improvements over version 1. For those new to the package, what follows is a short and new introduction. For those who are already using <code>assertr</code>, the text below will point out the improvements.</i></small></p>
<p>I can (and have) go on and on about the treachery of messy/bad datasets. Though its substantially less exciting than… pretty much everything else, I believe (proportional to the heartache and stress it causes) we don’t spend enough time talking about it or building solutions around it. No matter how new and fancy your ML algorithm is, it’s success is predicated upon a properly sanitized dataset. If you are using bad data, your approach will fail—either flagrantly (best case), or unnoticeably (considerably more probable and considerably more pernicious). </p>
<p><code>assertr</code> is a R package to help you identify common dataset errors. More specifically, it helps you easily spell out your assumptions about how the data should look and alert you of any deviation from those assumptions.</p>
<p>I’ll return to this point later in the post when we have more background, but I want to be up front about the goals of the package; <code>assertr</code> is not (and can never be) a “one-stop shop” for all of your data validation needs. The specific kind of checks individuals or teams have to perform any particular dataset are often far too idiosyncratic to ever be exhaustively addressed by a single package (although, the <code>assertive</code> meta-package may come very close!) But all of these checks will reuse motifs and follow the same patterns. So, instead, I’m trying to sell <code>assertr</code> as a way of thinking about dataset validations—a set of common dataset validation <i>actions</i>. If we think of these actions as <i>verbs</i>, you could say that <code>assertr</code> attempts to impose a grammar of error checking for datasets.</p>
<p>In my experience, the overwhelming majority of data validation tasks fall into only five different patterns:</p>
<ul>
<li><i>For every element in a column</i>, you want to make sure it fits certain criteria. Examples of this strain of error checking would be to make sure every element is a valid credit card number, or fits a certain regex pattern, or represents a date between two other dates. <code>assertr</code> calls this verb <code>assert</code>.</li>
<li><i>For every element in a column</i>, you want to make sure certain criteria are met <b>but the criteria can only be decided only <i>after</i> looking at the entire column as a whole</b>. For example, testing whether each element is within <i>n</i> standard deviations of the mean of the elements requires computation on the elements prior to inform the criteria to check for. <code>assertr</code> calls this verb <code>insist</code>.</li>
<li><i>For every row of a dataset</i>, you want to make sure certain assumptions hold. Examples include ensuring that no row has more than <i>n</i> number of missing values, or that a group of columns are jointly unique and never duplicated. <code>assertr</code> calls this verb <code>assert_rows</code>.</li>
<li><i>For every row of a dataset</i>, you want to make sure certain assumptions hold <b>but the criteria can only be decided only <i>after</i> looking at the entire column as a whole</b>. This closely mirrors the distinction between <code>assert</code> and <code>insist</code>, but for entire rows (not individual elements). An example of using this would be checking to make sure that the <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Mahalanobis_distance">Mahalanobis distance</a> between each row and all other rows are within <i>n</i> number of standard deviations of the mean distance. <code>assertr</code> calls this verb <code>insist_rows</code>.</li>
<li> You want to check some property of the dataset as a whole object. Examples include making sure the dataset has more than <i>n</i> columns, making sure the dataset has some specified column names, etc… <code>assertr</code> calls this last verb <code>verify</code>.</li>
</ul>
<p>Some of this might sound a little complicated, but I promise this is a worthwhile way to look at dataset validation. Now we can begin with an example of what can be achieved with these verbs. The following example is borrowed from the package vignette and README…</p>
<p>Pretend that, before finding the average miles per gallon for each number of engine cylinders in the <code>mtcars</code> dataset, we wanted to confirm the following dataset assumptions…<br>
<small>
<ul>
<li>that it has the columns <code>mpg</code>, <code>vs</code>, and <code>am</code></li>
<li>that the dataset contains more than 10 observations</li>
<li>that the column for ‘miles per gallon’ (mpg) is a positive number</li>
<li>that the column for ‘miles per gallon’ (mpg) does not contain a datum that is outside 4 standard deviations from its mean</li>
<li>that the <code>am</code> and <code>vs</code> columns (automatic/manual and v/straight engine, respectively) contain 0s and 1s only</li>
<li>each row contains at most 2 NAs</li>
<li>each row is unique jointly between the <code>mpg</code>, <code>am</code>, and <code>wt</code> columns</li>
<li>each row’s mahalanobis distance is within 10 median absolute deviations of all the distances (for outlier detection)</li>
</ul>
<pre>
library(dplyr)
library(assertr)

mtcars %>%
  verify(has_all_names(&quot;mpg&quot;, &quot;vs&quot;, &quot;am&quot;, &quot;wt&quot;)) %>%
  verify(nrow(.) > 10) %>%
  verify(mpg > 0) %>%
  insist(within_n_sds(4), mpg) %>%
  assert(in_set(0,1), am, vs) %>%
  assert_rows(num_row_NAs, within_bounds(0,2), everything()) %>%
  assert_rows(col_concat, is_uniq, mpg, am, wt) %>%
  insist_rows(maha_dist, within_n_mads(10), everything()) %>%
  group_by(cyl) %>%
  summarise(avg.mpg=mean(mpg))
</pre>
<p></p>
<p>Before <code>assertr</code> version 2, the pipeline would immediately terminate at the first failure. Sometimes this is a good thing. However, sometimes we’d like to run a dataset through our entire suite of checks and record all failures. The latest version includes the <code>chain_start</code> and <code>chain_end</code> functions; all assumptions within a chain (below a call to <code>chain_start</code> and above <code>chain_end</code>) will run from beginning to end and accumulate errors along the way. At the end of the chain, a specific action can be taken but the default is to halt execution and display a comprehensive report of what failed including line numbers and the offending datum, where applicable. </p>
<p>Another major improvement since the last version of <code>assertr</code> of CRAN is that <code>assertr</code> errors are now S3 classes (instead of dumb strings). Additionally, the behavior of each assertion statement on success (no error) and failure can now be flexibly customized. For example, you can now tell <code>assertr</code> to just return TRUE and FALSE instead of returning the data passed in or halting execution, respectively. Alternatively, you can instruct <code>assertr</code> to just give a warning instead of throwing a fatal error. For more information on this, see <code>help(&quot;success_and_error_functions&quot;)</code></p>
<h3>Beyond these examples</h3>
<p>Since the package was initially published on CRAN (almost exactly two years ago) many people have asked me how they should go about using <code>assertr</code> to test a particular assumption (and I’m very happy to help if you have one of your own, dear reader!) In every single one of these cases, I’ve been able to express it as an incantation using one of these 5 verbs. It also underscored, to me, that creating specialized functions for every need is a pipe dream. There is, however, two good pieces of news.</p>
<p>The first is that there is another package, <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=assertive"><code>assertive</code></a> (vignette here) that greatly enhances the <code>assertr</code> experience. The predicates (functions that start with “is_”) from this (meta)package can be used in <code>assertr</code> pipelines just as easily as <code>assertr</code>’s own predicates. And <code>assertive</code> has an enormous amount of them! Some specialized and exciting examples include <code>is_hex_color</code>, <code>is_ip_address</code>, and <code>is_isbn_code</code>!</p>
<p>The second is if <code>assertive</code> doesn’t have what you’re looking for, with just a little bit of studying the <code>assertr</code> grammar, you can whip up your own predicates with relative ease. Using some these basic constructs and a little effort, I’m confident that the grammar is expressive enough to completely adapt to your needs.</p>
<p><i>If this package interests you, I urge you to read the most recent package vignette <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=assertr/vignettes/assertr.html">here</a>. If you’re a <code>assertr</code> old-timer, I point you to <a target="_blank" rel="nofollow" href="https://cran.r-project.org/package=assertr/NEWS">this NEWS file</a> that list the changes from the previous version.</i></p>
<p>share this: <a title="Share on Facebook" data-provider="facebook" target="_blank" rel="nofollow" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F&t=Data%20validation%20with%20the%20assertr%20package&%23038;s=100&%23038;p&%23091;url&%23093;=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F&%23038;p&%23091;images&%23093;&%23091;0&%23093;=http%3A%2F%2Fwww.onthelambda.com%2Fwp-content%2Fuploads%2F2017%2F03%2Fassertr_logo.png&%23038;p&%23091;title&%23093;=Data%20validation%20with%20the%20assertr%20package"><img title="Share on Facebook" alt="Facebook" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/facebook.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Twitter" data-provider="twitter" target="_blank" rel="nofollow" href="https://twitter.com/intent/tweet?url=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F&text=Hey%20check%20this%20out"><img title="Share on Twitter" alt="twitter" src="https://i2.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/twitter.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Google+" data-provider="google_plus" target="_blank" rel="nofollow" href="https://plus.google.com/share?url=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F"><img title="Share on Google+" alt="google_plus" src="https://i0.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/google_plus.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Reddit" data-provider="reddit" target="_blank" rel="nofollow" href="http://www.reddit.com/submit?url=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F&title=Data%20validation%20with%20the%20assertr%20package"><img title="Share on Reddit" alt="reddit" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/reddit.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Pin it with Pinterest" data-provider="pinterest" target="_blank" rel="nofollow" href="http://pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F&media=http%3A%2F%2Fwww.onthelambda.com%2Fwp-content%2Fuploads%2F2017%2F03%2Fassertr_logo.png&%23038;description=Data%20validation%20with%20the%20assertr%20package"><img title="Pin it with Pinterest" alt="pinterest" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/pinterest.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on Linkedin" data-provider="linkedin" target="_blank" rel="nofollow" href="http://www.linkedin.com/shareArticle?mini=true&url=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F&%23038;title=Data%20validation%20with%20the%20assertr%20package"><img title="Share on Linkedin" alt="linkedin" src="https://i0.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/linkedin.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share on tumblr" data-provider="tumblr" target="_blank" rel="nofollow" href="http://tumblr.com/share?s=&v=3&%23038;t=Data%20validation%20with%20the%20assertr%20package&%23038;u=http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F"><img title="Share on tumblr" alt="tumblr" src="https://i0.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/tumblr.png?resize=16%2C16" data-recalc-dims="1"></a><a title="Share by email" data-provider="mail" target="_blank" rel="nofollow" href="mailto:?subject=Data%20validation%20with%20the%20assertr%20package&body=Hey%20check%20this%20out:%20http%3A%2F%2Fwww.onthelambda.com%2F2017%2F03%2F20%2Fdata-validation-with-the-assertr-package%2F"><img title="Share by email" alt="mail" src="https://i1.wp.com/www.onthelambda.com/wp-content/plugins/social-media-feather/synved-social/image/social/regular/32x32/mail.png?resize=16%2C16" data-recalc-dims="1"></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.onthelambda.com/2017/03/20/data-validation-with-the-assertr-package/"> R – On the lambda</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/k3EwQCXGRXg"></a></div></small>