<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/use-switch-instead-of-ifelse-to-return-a-null/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-addict.com/2017/02/21/switch-ifelse.html"> http://r-addict.com</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img src="https://i0.wp.com/r-addict.com/images/fulls/statement_ifelse.png?w=456" data-recalc-dims="1"> Have you ever tried to return a <code>NULL</code> with the <code>ifelse()</code> function? This function is a simple vectorized workflow for conditional statements. However, one can’t just return a <code>NULL</code> value as a result of this evaluation. Check a tricky workaround solution in this post.</p>
<p>Imagine a simple <code>R</code> logical statement like</p>
<figure>
<pre><code data-lang="r"><span>statement</span><span> </span><span>&lt;-</span><span> </span><span>length</span><span>(</span><span>character</span><span>())</span><span> </span><span>></span><span> </span><span>0</span><span>
</span><span>statement</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">[1] FALSE</code></pre>
</figure>
<p>and depending on that logical value you would like to create a new variable called <code>res</code>. You can follow a regular <code>if</code> conditional statement</p>
<figure>
<pre><code data-lang="r"><span>if</span><span> </span><span>(</span><span>statement</span><span>)</span><span> </span><span>{</span><span>
</span><span>res</span><span> </span><span>&lt;-</span><span> </span><span>&quot;message&quot;</span><span>
</span><span>}</span><span> </span><span>else</span><span> </span><span>{</span><span>
</span><span>res</span><span> </span><span>&lt;-</span><span> </span><span>NULL</span><span>
</span><span>}</span><span>
</span><span>res</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">NULL</code></pre>
</figure>
<p>or use simplified interface with <code>ifelse()</code> function</p>
<figure>
<pre><code data-lang="r"><span>ifelse</span><span>(</span><span>statement</span><span>,</span><span> </span><span>&quot;message&quot;</span><span>,</span><span> </span><span>NULL</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">Error in ifelse(statement, &quot;message&quot;, NULL): replacement has length zero</code></pre>
</figure>
<p>However it looks like one is not able to return the <code>NULL</code> as a result of this operation.</p>
<p>A solution is to jump to the other conditional function called <code>switch()</code>, which for the first parameter takes number (<code>n</code>) and returns the value passed as the <code>n-th</code> parameter. If one treats logical values as <code>TRUE is 1</code> and <code>FALSE is 0</code> then primary <code>ifelse()</code> statement can be rebuild to <code>switch()</code> call like</p>
<figure>
<pre><code data-lang="r"><span>switch</span><span>(</span><span>statement</span><span> </span><span>+</span><span> </span><span>1</span><span>,</span><span> </span><span>NULL</span><span>,</span><span> </span><span>&quot;message&quot;</span><span>)</span></code></pre>
</figure>
<figure>
<pre><code data-lang="text">NULL</code></pre>
</figure>
<p>What do you think about such workaround? Do you use other solutions for such a situation?</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://r-addict.com/2017/02/21/switch-ifelse.html"> http://r-addict.com</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/PDLO0QnjlYM"></a></div>