<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/r-tip-use-istrue/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/06/r-tip-use-istrue/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">R Tip</a>: use <code>isTRUE()</code>.</p>
<p>A lot of <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> functions are <em>type unstable</em>, which means they return different types or classes depending on details of their values.</p>
<p>For example consider <code>all.equal()</code>, it returns the logical value <code>TRUE</code> when the items being compared are equal:</p>
<pre>
all.equal(1:3, c(1, 2, 3))
# [1] TRUE
</pre>
<p>However, when the items being compared are not equal <code>all.equal()</code> instead returns a message:</p>
<pre>
all.equal(1:3, c(1, 2.5, 3))
# [1] &quot;Mean relative difference: 0.25&quot;
</pre>
<p>This can be inconvenient in using functions similar to <code>all.equal()</code> as tests in <code>if()</code>-statements and other program control structures.</p>
<p>The saving functions is <code>isTRUE()</code>. <code>isTRUE()</code> returns <code>TRUE</code> if its argument <em>value</em> is equivalent to <code>TRUE</code>, and returns <code>FALSE</code> otherwise. <code>isTRUE()</code> makes <code>R</code> programming much easier.</p>
<p><span></span></p>
<p>
<p>Some examples of <code>isTRUE()</code> are given below:</p>
<pre>
isTRUE(TRUE)
# [1] TRUE

isTRUE(FALSE)
[1] FALSE

isTRUE(NULL)
# [1] FALSE

isTRUE(NA)
# [1] FALSE

isTRUE(all.equal(1:3, c(1, 2.5, 3)))
# [1] FALSE

isTRUE(all.equal(1:3, c(1, 2, 3)))
# [1] TRUE

lst &lt;- list(x = 5)
isTRUE(lst$y == 7)
# [1] FALSE
lst$y == 7

logical(0)
isTRUE(logical(0))
# [1] FALSE
</pre>
<p>Using <code>isTRUE()</code> one can write safe and legible code such as the following:</p>
<pre>
# Pretend this assignment was performed by somebody else.
lst &lt;- list(x = 5)

# Our own sanitization code.
if(!isTRUE(lst$y > 3)) {
  lst$y = lst$x
}

print(lst)

# $x
# [1] 5
#
# $y
# [1] 5
</pre>
<p><code>R</code> now also has <code>isFALSE()</code>, but by design it does not mean the same thing as <code>!isTRUE()</code>. The ideas is: for a value <code>v</code> at most of one of <code>isTRUE()</code> or <code>isFALSE()</code> is set, and both are non-NA unnamed scalar logical values. (example: <code>isTRUE(5)</code>, <code>isFALSE(5)</code>).</p>
<p>Or as <code>help(isTRUE)</code> puts it:</p>
<blockquote>
<p>
… if(isTRUE(cond)) may be preferable to if(cond) …
</p>
</blockquote>
<p>Note: prior to <code>R</code> 3.5 <code>isTRUE</code> (the current version!) was defined as “<code>isTRUE &lt;- function(x) identical(x, TRUE)</code>” (please see change-log <a rel="nofollow" href="https://cran.r-project.org/doc/manuals/r-devel/NEWS.html" target="_blank">here</a>). This seemed clever, but failed on named logical values (violating a principle of least surprise):</p>
<pre>
oldTRUE &lt;- function(x) identical(x, TRUE)

v &lt;- TRUE
oldTRUE(v)
# [1] TRUE
isTRUE(v)
# [1] TRUE

names(v) &lt;- &quot;condition&quot;
oldTRUE(v)
# [1] FALSE
isTRUE(v)
# [1] TRUE
</pre>
<p>This caused a lot of problems (example taken from <a rel="nofollow" href="https://cran.r-project.org/doc/manuals/r-devel/NEWS.html" target="_blank">R3.5.0 NEWS</a>):</p>
<pre>
x &lt;- rlnorm(99)

isTRUE(median(x) == quantile(x)[&quot;50%&quot;])
# [1] TRUE

oldTRUE(median(x) == quantile(x)[&quot;50%&quot;])
# [1] FALSE
</pre>
	<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.win-vector.com/blog/2018/06/r-tip-use-istrue/"> R – Win-Vector Blog</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/pyNIhmOtot4" height="1"></a></div>