<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/to-purrr-or-not-to-purrr/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.mango-solutions.com/blog/to-purrr-or-not-to-purrr"> Mango Solutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <img sizes="100vw" src="https://i0.wp.com/www.mango-solutions.com/blog/images/9/f/d/b/b/9fdbbcfb57da9193d89e58b62ddef678b58b8d9a-purr-blog-header.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h5><em>Nicolas Attalides, Data Scientist</em></h5>
<pre><code>library(purrr)
library(magrittr)</code></pre>
<p>I first started using R long before the RStudio and tidyverse days… I remember writing chunks of code in a text editor and copy/pasting it into the R console! Yes I know, shocking. Nonetheless, most of us will have written code over the years that works just fine in base R, however in my case, the ever-growing adoption of the tidyverse packages (and my personal aspiration to improve my coding skills) has created a sense of necessity to re-write parts of it to fit within the tidyverse setting.</p>
<p>In this blog post I explore the <code>purrr</code> package (part of tidyverse collection) and its use within a data<br>
scientist’s toolset. I aim to present the case for using the <code>purrr</code> functions and through the use of examples compare them with base R functionality. To do this, we will concentrate on two typical coding scenarios in base R: 1) loops and 2) the suite of apply functions and then compare them with their relevant counterpart map functions in the <code>purrr</code> package.</p>
<p>However, before I start, I wanted to make it clear that I do sympathise with those of you whose first reaction to <code>purrr</code> is <em>“but I can do all this stuff in base R”</em>. Putting that aside, the obvious first obstacle for us to overcome is to lose the notion of <em>“if it’s not broken why change it”</em> and open our ‘coding’ minds to change. At least, I hope you agree with me that the silver lining of this kind of exercise is to satisfy ones curiosity about the <code>purrr</code> package and maybe learn something new!</p>
<p>Let us first briefly describe the concept of functional programming (FP) in case you are not familiar with it.</p>
<h3>Functional programming (FP)</h3>
<p>R is a functional programming language which means that a user of R has the necessary tools to create and manipulate functions. There is no need to go into too much depth here but it suffices to know that FP is the process of writing code in a structured way and through functions remove code duplications and redundancies. In effect, computations or evaluations are treated as mathematical functions and the output of a function only depends on the values of its inputs – known as arguments. FP ensures that any side-effects such as changes in state do not affect the expected output such that if you call the same function twice with the same arguments the function returns the same output.</p>
<p>For those that are interested to find out more, I suggest reading Hadley Wickham’s <em>Functional Programming</em> chapter in the “Advanced R” book. The companion website for this can be found <a rel="nofollow" href="http://adv-r.had.co.nz/" target="_blank">here.</a></p>
<p>The <code>purrr</code> package, which forms part of the tidyverse ecosystem of packages, further enhances the functional programming aspect of R. It allows the user to write functional code with less friction in a complete and consistent manner. The <code>purrr</code> functions can be used, among other things, to replace loops and the suite of apply functions.</p>
<h3>Let’s talk about loops</h3>
<p>The motivation behind the examples we are going to look at involve iterating in R for various scenarios. For example, iterate over elements of a vector or list, iterate over rows or columns of a matrix … the list (pun intended) can go on and on!</p>
<p>One of the first things that one gets very excited to ‘play’ when learning to use R – at least that was the case for me – is loops! Lot’s of loops, elaborate, complex… dare I say never ending infinite loops (queue hysteric laughter emoji). Joking aside, it is usually the default answer to a problem that involves iteration of some sort as I demonstrate below. </p>
<pre><code># Create a vector of the mean values of all the columns of the mtcars dataset
# The long repetitive way
mean_vec &lt;- c(mean(mtcars$mpg), mean(mtcars$cyl), mean(mtcars$disp), mean(mtcars$hp),
              mean(mtcars$drat), mean(mtcars$wt), mean(mtcars$qsec), mean(mtcars$vs),
              mean(mtcars$am), mean(mtcars$gear), mean(mtcars$carb))
mean_vec

# The loop way
mean_vec_loop &lt;- vector(&quot;double&quot;, ncol(mtcars))
for (i in seq_along(mtcars)) {
  mean_vec_loop[[i]] &lt;- mean(mtcars[[i]])
}
mean_vec_loop</code></pre>
<p>The resulting vectors are the same and the difference in speed (milliseconds) is negligible. I hope that we can all agree that the long way is definitely not advised and actually is bad coding practice, let alone the frustration (and error-prone task) of copy/pasting. Having said that, I am sure there are other ways to do this – I demonstrate this later using <code>lapply</code> – but my aim was to show the benefit of using a for loop in base R for an iteration problem.</p>
<p>Now imagine if in the above example I wanted to calculate the variance of each column as well…</p>
<pre><code># Create two vectors of the mean and variance of all the columns of the mtcars dataset

# For mean
mean_vec_loop &lt;- vector(&quot;double&quot;, ncol(mtcars))
for (i in seq_along(mtcars)) {
  mean_vec_loop[[i]] &lt;- mean(mtcars[[i]])
}
mean_vec_loop

#For variance
var_vec_loop &lt;- vector(&quot;double&quot;, ncol(mtcars))
for (i in seq_along(mtcars)) {
  var_vec_loop[[i]] &lt;- var(mtcars[[i]])
}
var_vec_loop

# Or combine both calculations in one loop
for (i in seq_along(mtcars)) {
  mean_vec_loop[[i]] &lt;- mean(mtcars[[i]])
  var_vec_loop[[i]] &lt;- var(mtcars[[i]])
}
mean_vec_loop
var_vec_loop</code></pre>
<p>Now let us assume that we know that we want to create these vectors not just for the mtcars dataset but for other datasets as well. We could in theory copy/paste the <code>for</code> loops and just change the dataset we supply in the loop but one should agree that this action is repetitive and could result to mistakes. Instead we can generalise this into functions. This is where FP comes into play.</p>
<pre><code># Create two functions that returns the mean and variance of the columns of a dataset

# For mean
col_mean &lt;- function(df) {
  output &lt;- vector(&quot;double&quot;, length(df))
  for (i in seq_along(df)) {
    output[[i]] &lt;- mean(df[[i]])
  }
  output
}
col_mean(mtcars)

#For variance
col_variance &lt;- function(df) {
  output &lt;- vector(&quot;double&quot;, length(df))
  for (i in seq_along(df)) {
    output[[i]] &lt;- var(df[[i]])
  }
  output
}
col_variance(mtcars)</code></pre>
<p>Why not take this one step further and take full advantage of R’s functional programming tools by creating a function that takes as an argument a function! Yes, you read it correctly… a function within a function!</p>
<p>Why do we want to do that? Well, the code for the two functions above, as clean as it might look, is still repetitive and the only real difference between <code>col_mean</code> and <code>col_var</code> is the mathematical function that we are calling. So why not generalise this further? </p>
<pre><code># Create a function that returns a computational value (such as mean or variance)
# for a given dataset

col_calculation &lt;- function(df, fun) {
  output &lt;- vector(&quot;double&quot;, length(df))
  for (i in seq_along(df)) {
    output[[i]] &lt;- fun(df[[i]])
  }
  output
}
col_calculation(mtcars, mean)
col_calculation(mtcars, var)</code></pre>
<h3>Did someone say <em>apply?</em></h3>
<p>I mentioned earlier that an alternative way to solve the problem is to use the <code>apply</code> function (or suite of <code>apply</code> functions such as <code>lapply</code>, <code>sapply</code>, <code>vapply</code>, etc). In fact, these functions are what we call Higher Order Functions. Similar to what we did earlier, these are functions that can take other functions as an argument. </p>
<p>The benefit of using higher order functions instead of a <code>for</code> loop is that they allow us to think about what code we are executing at a higher level. Think of it as: “apply this to that” rather than “take the first item, do this, take the next item, do this…”</p>
<p>I must admit that at first it might take a little while to get used to but there is definitely a sense of pride when you can improve your code by eliminating <code>for</code> loops and replace them with <em>apply-type</em> functions.</p>
<pre><code># Create a list/vector of the mean values of all the columns of the mtcars dataset
lapply(mtcars, mean) %>% head # Returns a list
sapply(mtcars, mean) %>% head # Returns a vector</code></pre>
<p>Once again, speed of execution is not the issue and neither is the common misconception about loops being slow compared to <code>apply</code> functions. As a matter of fact the main argument in favour of using <code>lapply</code> or any of the <code>purrr</code> functions as we will see later is the pure <em>simplicity</em> and <em>readability</em> of the code. Full stop.</p>
<h3>Enter the purrr</h3>
<p>The best place to start when exploring the <code>purrr</code> package is the <code>map</code> function. The reader will notice that these functions are utilised in a very similar way to the <code>apply</code> family of functions. The subtle difference is that the <code>purrr</code> functions are consistent and the user can be assured of the output – as opposed to some cases when using for example <code>sapply</code> as I demonstrate later on.</p>
<pre><code># Create a list/vector of the mean values of all the columns of the mtcars dataset
map(mtcars, mean) %>% head # Returns a list
map_dbl(mtcars, mean) %>% head # Returns a vector - of class double</code></pre>
<p>Let us introduce the iris dataset with a slight modification in order to demonstrate the inconsistency that sometimes can occur when using the <code>sapply</code> function. This can often cause issues with the code and introduce mystery bugs that are hard to spot.</p>
<pre><code># Modify iris dataset
iris_mod &lt;- iris
iris_mod$Species &lt;- ordered(iris_mod$Species) # Ordered factor levels

class(iris_mod$Species) # Note: The ordered function changes the class

# Extract class of every column in iris_mod
sapply(iris_mod, class) %>% str # Returns a list of the results
sapply(iris_mod[1:3], class) %>% str # Returns a character vector!?!? - Note: inconsistent object type</code></pre>
<p>Since by default <code>map</code> returns a list one can ensure that an object of the same class is returned without any unexpected (and unwanted) surprises. This is inline with FP consistency.</p>
<pre><code># Extract class of every column in iris_mod
map(iris_mod, class) %>% str # Returns a list of the results
map(iris_mod[1:3], class) %>% str # Returns a list of the results</code></pre>
<p>To further demonstrate the consistency of the <code>purrr</code> package in this type of setting, the <code>map_*()</code> functions (see below) can be used to return a vector of the expected type, otherwise you get an informative error.</p>
<ul>
<li><code>map_lgl()</code> makes a logical vector.</li>
<li><code>map_int()</code> makes an integer vector.</li>
<li><code>map_dbl()</code> makes a double vector.</li>
<li><code>map_chr()</code> makes a character vector.</li>
</ul>
<pre><code># Extract class of every column in iris_mod
map_chr(iris_mod[1:4], class) %>% str # Returns a character vector
map_chr(iris_mod, class) %>% str # Returns a meaningful error

# As opposed to the equivalent base R function vapply
vapply(iris_mod[1:4], class, character(1)) %>% str  # Returns a character vector
vapply(iris_mod, class, character(1)) %>% str  # Returns a possibly harder to understand error</code></pre>
<p>It is worth noting that if the user does not wish to rely on tidyverse dependencies they can always use base R functions but need to be extra careful of the potential inconsistencies that might arise.</p>
<h3>Multiple arguments and neat tricks</h3>
<p>In case we wanted to apply a function to multiple vector arguments we have the option of <code>mapply</code> from base R or the <code>map2</code> from <code>purrr</code>.</p>
<pre><code># Create random normal values from a list of means and a list of standard deviations
mu &lt;- list(10, 100, -100)
sigma &lt;- list(0.01, 1, 10)

mapply(rnorm, n = 5, mu, sigma, SIMPLIFY = FALSE) # I need SIMPLIFY = FALSE because otherwise I get a matrix

map2(mu, sigma, rnorm, n = 5)</code></pre>
<p>The <code>map2</code> function can easily extend to further arguments – not just two as in the example above – and that is where the <code>pmap</code> function comes in.</p>
<p>I also thought of sharing a couple of neat tricks that one can use with the <code>map</code> function.</p>
<p>1) Say you want to fit a linear model for every cylinder type in the mtcars dataset. You can avoid code duplication and do it as follows:</p>
<pre><code># Split mtcars dataset by cylinder values and then fit a simple lm
models &lt;- mtcars %>% 
  split(.$cyl) %>% # Split by cylinder into 3 lists
  map(function(df) lm(mpg ~ wt, data = df)) # Fit linear model for each list</code></pre>
<p>2) Say we are using a function, such as <code>sqrt</code> (calculate square root), on a list that contains a non-numeric element. The base R function <code>lapply</code> throws an error and execution stops without knowing what caused the error. The <code>safely</code> function of <code>purrr</code> completes execution and the user can identify what caused the error.</p>
<pre><code>x &lt;- list(1, 2, 3, &quot;e&quot;, 5)

# Base R
lapply(x, sqrt)

# purrr package
safe_sqrt &lt;- safely(sqrt)
safe_result_list &lt;- map(x, safe_sqrt) %>% transpose
safe_result_list$result</code></pre>
<h3>Conclusion</h3>
<p>Overall, I think it is fair to say that using higher order functions in R is a great way to improve ones code. With that in mind, my closing remark for this blog post is to simply re-iterate the benefits of using the <code>purrr</code> package. That is:</p>
<ul>
<li>The output is consistent.</li>
<li>The code is easier to read and write.</li>
</ul>
<p>If you enjoyed learning about <code>purrr</code>, then you can join us at our <code>purrr</code> workshop at this years EARL London – <a rel="nofollow" href="https://earlconf.com/2018/london/" target="_blank">early bird tickets</a> are available now! </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://www.mango-solutions.com/blog/to-purrr-or-not-to-purrr"> Mango Solutions</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/8Bm9o8_OD1U" height="1"></a></div>