<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/playing-map-and-reduce-in-r-subsetting/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://statcompute.wordpress.com/2018/09/08/playing-map-and-reduce-in-r-subsetting/"> S+/R – Yet Another Blog in Statistical Computing</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In the previous post (<a rel="nofollow" href="https://statcompute.wordpress.com/2018/09/03/playing-map-and-reduce-in-r-by-group-calculation" target="_blank">https://statcompute.wordpress.com/2018/09/03/playing-map-and-reduce-in-r-by-group-calculation</a>), I’ve shown how to employ the MapReduce when calculating by-group statistics. Actually, the same Divide-n-Conquer strategy can be applicable to other use cases, one of which is the subsetting operation. </p>
<p>In the example below, let’s still use the same iris data for the demonstration purpose. In R, the most convenient way to perform the subsetting might be the subset() function, which would search for rows meeting the condition described in the “expr” expression below throughout the entire data.frame. </p>
<pre>
data(iris)
expr = expression(Sepal.Length > 7 &amp; Sepal.Width > 3)
subset(iris, eval(expr))
</pre>
<p>With the whole data.frame partitioned into multiple pieces, the row searching operation can perfectly fit into the MapReduce paradigm, as described in the logic flow below.<br>
1. First of all, the iris data is divided into chucks with equal number of rows, e.g. two chunks in the example.<br>
2. Next, a Map() function is used to perform the row searching operation within each chuck.<br>
3. Upon the return of rows meeting the criteria from each chuck, a Reduce() function is employed to combine all outcomes together. </p>
<pre>
n &lt;- 2
lst &lt;- split(iris, sort((1:nrow(iris)) %% n))
Reduce(rbind, Map(function(x) x[with(x, which(eval(expr))), ], lst))
</pre>
<p>It is noted that the above map operation is still performed sequentially without leveraging the computing power of multiple CPUs. In the CPU usage, we can see that only one CPU is used and the rest are idle. </p>
<p><img data-orig-file="https://statcompute.files.wordpress.com/2018/09/single_core.png?w=456" data-orig-size="1920,1080" data-medium-file="https://statcompute.files.wordpress.com/2018/09/single_core.png?w=456?w=300" data-attachment-id="3221" data-image-meta="{" data-image-description="" src="https://statcompute.files.wordpress.com/2018/09/single_core.png?w=456" data-permalink="https://statcompute.wordpress.com/2018/09/08/playing-map-and-reduce-in-r-subsetting/single_core/" data-large-file="https://statcompute.files.wordpress.com/2018/09/single_core.png?w=456?w=1920" alt="single_core" data-image-title="single_core" sizes="(max-width: 1200px) 100vw, 1200px" data-comments-opened="0" class="wp-image-3221" data-recalc-dims="1"></p>
<p>Similar to the by-group summary, the by-chunk operation of row searching doesn’t have to be in the sequential order and can be distributed simultaneously across multiple CPUs by using the mcMap() function, as outlined below.<br>
1. Again, it starts with the data partition. However, there are two caveats in the example. Firstly, the data is split based upon the number of CPUs captured by the detectCores() function. Secondly, the partitioned data is NOT stored physically in the memory but reflected logically by a list of future abstractions, e.g. “flst” in the code snippet.<br>
2. In the second step, the mcMap() function is used to evaluate each future abstraction, return the partitioned data, and then perform the row searching within each chunk.<br>
3. At last, the Reduce() function collects and combines all outcomes. </p>
<pre>
pkgs &lt;- c(&quot;parallel&quot;, &quot;future&quot;)
mapply(function(x) require(x, character.only = T), pkgs)
n &lt;- detectCores()
flst &lt;- Map(function(x) future({x}), split(iris, sort((1:nrow(iris)) %% n)))
Reduce(rbind, mcMap(function(x) value(x)[with(value(x), which(eval(expr))), ], flst, mc.cores = n))
</pre>
<p>If we take a look at the CPU usage again, it is now shown that all CPUs are utilized. </p>
<p><img data-orig-file="https://statcompute.files.wordpress.com/2018/09/multicore.png?w=456" data-orig-size="1920,1080" data-medium-file="https://statcompute.files.wordpress.com/2018/09/multicore.png?w=456?w=300" data-attachment-id="3222" data-image-meta="{" data-image-description="" src="https://statcompute.files.wordpress.com/2018/09/multicore.png?w=456" data-permalink="https://statcompute.wordpress.com/2018/09/08/playing-map-and-reduce-in-r-subsetting/multicore/" data-large-file="https://statcompute.files.wordpress.com/2018/09/multicore.png?w=456?w=1920" alt="multicore" data-image-title="multicore" sizes="(max-width: 1200px) 100vw, 1200px" data-comments-opened="0" class="wp-image-3222" data-recalc-dims="1"></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="https://statcompute.wordpress.com/2018/09/08/playing-map-and-reduce-in-r-subsetting/"> S+/R – Yet Another Blog in Statistical Computing</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 alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/DWmlXOVNFbc" height="1"></a></div>