<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/vectorized-block-ifelse-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2017/11/vectorized-block-ifelse-in-r/"> 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/" target="_blank">Win-Vector LLC</a> has been working on porting some significant large scale production systems from <a rel="nofollow" href="https://www.sas.com/en_us/home.html" target="_blank">SAS</a> to <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a>.</p>
<p>From this experience we want to share how to simulate, in R with Apache Spark (via Sparklyr), a nifty SAS feature: the vectorized “block <code>if(){}else{}</code>” structure.<span></span></p>
<p>
<p>When porting code from one language to another you hope the expressive power and style of the languages are similar.</p>
<ul>
<li>If the source language is too weak then the original code will be very long (and essentially over specified), meaning a direct transliteration will be unlikely to be efficient, as you are not using the higher order operators of the target language.</li>
<li>If the source language is too strong you will have operators that don’t have direct analogues in the target language. </li>
</ul>
<p>SAS has some strong and powerful operators. One such is what I am calling “the vectorized block <code>if(){}else{}</code>“. From <a rel="nofollow" href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000201978.htm" target="_blank">SAS documentation</a>:</p>
<blockquote>
<p>The subsetting IF statement causes the DATA step to continue processing only those raw data records or those observations from a SAS data set that meet the condition of the expression that is specified in the IF statement.</p>
</blockquote>
<p>That is a really wonderful operator!</p>
<p>R has some available related operators: <code>base::ifelse()</code>, <code>dplyr::if_else()</code>, and <code>dplyr::mutate_if()</code>. However, none of these has the full expressive power of the SAS operator, which can per data row:</p>
<ul>
<li>Conditionally choose where different assignments are made to (not just choose conditionally which values are taken).</li>
<li>Conditionally specify blocks of assignments that happen together.</li>
<li>Be efficiently nested and chained with other IF statements.</li>
</ul>
<p>To help achieve such expressive power in R Win-Vector is introducing <a rel="nofollow" href="https://winvector.github.io/seplyr/reference/if_else_device.html" target="_blank"><code>seplyr::if_else_device()</code></a>. When combined with <a rel="nofollow" href="https://winvector.github.io/seplyr/reference/partition_mutate_se.html" target="_blank"><code>seplyr::partition_mutate_se()</code></a> you get a good high performance simulation of the SAS power in R. These are now available in the open source R package <a rel="nofollow" href="https://winvector.github.io/seplyr/" target="_blank">seplyr</a>.</p>
<p>For more information please <a rel="nofollow" href="mailto:contact@win-vector.com" target="_blank">reach out to us here at Win-Vector</a> or try <a rel="nofollow" href="https://winvector.github.io/seplyr/reference/if_else_device.html" target="_blank"><code>help(if_else_device)</code></a>. </p>
<p>Also, we will publicize more documentation and examples shortly (especially showing big data scale use with Apache Spark via Sparklyr).</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.win-vector.com/blog/2017/11/vectorized-block-ifelse-in-r/"> 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/1UAhGzSOZMo" height="1"></a></div>