<p></p>
<p><code>purrr</code> is package that extends R’s functional programming capabilities. It brings a lot of new stuff to the table and in this post I show you some of the most useful (at least to me) functions included in <code>purrr</code>.</p>
<div>
<h2>Getting rid of loops with <code>map()</code></h2>
<pre><code>library(purrr)

numbers &lt;- list(11, 12, 13, 14)

map_dbl(numbers, sqrt)</code></pre>
<pre><code>## [1] 3.316625 3.464102 3.605551 3.741657</code></pre>
<p>You might wonder why this might be preferred to a for loop? It’s a lot less verbose, and you do not need to initialise any kind of structure to hold the result. If you google “create empty list in R” you will see that this is very common. However, with the <code>map()</code> family of functions, there is no need for an initial structure. <code>map_dbl()</code> returns an atomic list of real numbers, but if you use <code>map()</code> you will get a list back. Try them all out!</p>
</div>
<div>
<h2>Map conditionally</h2>
<div>
<h4>map_if()</h4>
<pre><code># Create a helper function that returns TRUE if a number is even
is_even &lt;- function(x){
  !as.logical(x %% 2)
}

map_if(numbers, is_even, sqrt)</code></pre>
<pre><code>## [[1]]
## [1] 11
## 
## [[2]]
## [1] 3.464102
## 
## [[3]]
## [1] 13
## 
## [[4]]
## [1] 3.741657</code></pre>
</div>
<div>
<h4>map_at()</h4>
<pre><code>map_at(numbers, c(1,3), sqrt)</code></pre>
<pre><code>## [[1]]
## [1] 3.316625
## 
## [[2]]
## [1] 12
## 
## [[3]]
## [1] 3.605551
## 
## [[4]]
## [1] 14</code></pre>
<p><code>map_if()</code> and <code>map_at()</code> have a further argument than <code>map()</code>; in the case of <code>map_if()</code>, a predicate function ( a function that returns <code>TRUE</code> or <code>FALSE</code>) and a vector of positions for <code>map_at()</code>. This allows you to map your function only when certain conditions are met, which is also something that a lot of people google for.</p>
</div>
</div>
<div>
<h2>Map a function with multiple arguments</h2>
<pre><code>numbers2 &lt;- list(1, 2, 3, 4)

map2(numbers, numbers2, `+`)</code></pre>
<pre><code>## [[1]]
## [1] 12
## 
## [[2]]
## [1] 14
## 
## [[3]]
## [1] 16
## 
## [[4]]
## [1] 18</code></pre>
<p>You can map two lists to a function which takes two arguments using <code>map_2()</code>. You can even map an arbitrary number of lists to any function using <code>pmap()</code>.</p>
<p>By the way, try this in: <code>`+`(1,3)</code> and see what happens.</p>
</div>
<div>
<h2>Don’t stop execution of your function if something goes wrong</h2>
<pre><code>possible_sqrt &lt;- possibly(sqrt, otherwise = NA_real_)

numbers_with_error &lt;- list(1, 2, 3, &quot;spam&quot;, 4)

map(numbers_with_error, possible_sqrt)</code></pre>
<pre><code>## [[1]]
## [1] 1
## 
## [[2]]
## [1] 1.414214
## 
## [[3]]
## [1] 1.732051
## 
## [[4]]
## [1] NA
## 
## [[5]]
## [1] 2</code></pre>
<p>Another very common issue is to keep running your loop even when something goes wrong. In most cases the loop simply stops at the error, but you would like it to continue and see where it failed. Try to google “skip error in a loop” or some variation of it and you’ll see that a lot of people really just want that. This is possible by combining <code>map()</code> and <code>possibly()</code>. Most solutions involve the use of <code>tryCatch()</code> which I personally do not find very easy to use.</p>
</div>
<div>
<h2>Don’t stop execution of your function if something goes wrong and capture the error</h2>
<pre><code>safe_sqrt &lt;- safely(sqrt, otherwise = NA_real_)

map(numbers_with_error, safe_sqrt)</code></pre>
<pre><code>## [[1]]
## [[1]]$result
## [1] 1
## 
## [[1]]$error
## NULL
## 
## 
## [[2]]
## [[2]]$result
## [1] 1.414214
## 
## [[2]]$error
## NULL
## 
## 
## [[3]]
## [[3]]$result
## [1] 1.732051
## 
## [[3]]$error
## NULL
## 
## 
## [[4]]
## [[4]]$result
## [1] NA
## 
## [[4]]$error
## &lt;simpleError in .f(...): non-numeric argument to mathematical function>
## 
## 
## [[5]]
## [[5]]$result
## [1] 2
## 
## [[5]]$error
## NULL</code></pre>
<p><code>safely()</code> is very similar to <code>possibly()</code> but it returns a list of lists. An element is thus a list of the result and the accompagnying error message. If there is no error, the error component is <code>NULL</code> if there is an error, it returns the error message.</p>
</div>
<div>
<h2>Transpose a list</h2>
<pre><code>safe_result_list &lt;- map(numbers_with_error, safe_sqrt)

transpose(safe_result_list)</code></pre>
<pre><code>## $result
## $result[[1]]
## [1] 1
## 
## $result[[2]]
## [1] 1.414214
## 
## $result[[3]]
## [1] 1.732051
## 
## $result[[4]]
## [1] NA
## 
## $result[[5]]
## [1] 2
## 
## 
## $error
## $error[[1]]
## NULL
## 
## $error[[2]]
## NULL
## 
## $error[[3]]
## NULL
## 
## $error[[4]]
## &lt;simpleError in .f(...): non-numeric argument to mathematical function>
## 
## $error[[5]]
## NULL</code></pre>
<p>Here we transposed the above list. This means that we still have a list of lists, but where the first list holds all the results (which you can then access with <code>safe_result_list$result</code>) and the second list holds all the errors (which you can access with <code>safe_result_list$error</code>). This can be quite useful!</p>
</div>
<div>
<h2>Apply a function to a lower depth of a list</h2>
<pre><code>transposed_list &lt;- transpose(safe_result_list)

transposed_list %>% 
    at_depth(2, is_null)</code></pre>
<pre><code>## $result
## $result[[1]]
## [1] FALSE
## 
## $result[[2]]
## [1] FALSE
## 
## $result[[3]]
## [1] FALSE
## 
## $result[[4]]
## [1] FALSE
## 
## $result[[5]]
## [1] FALSE
## 
## 
## $error
## $error[[1]]
## [1] TRUE
## 
## $error[[2]]
## [1] TRUE
## 
## $error[[3]]
## [1] TRUE
## 
## $error[[4]]
## [1] FALSE
## 
## $error[[5]]
## [1] TRUE</code></pre>
<p>Sometimes working with lists of lists can be tricky, especially when we want to apply a function to the sub-lists. This is easily done with <code>at_depth()</code>!</p>
</div>
<div>
<h2>Set names of list elements</h2>
<pre><code>name_element &lt;- c(&quot;sqrt()&quot;, &quot;ok?&quot;)

set_names(transposed_list, name_element)</code></pre>
<pre><code>## $`sqrt()`
## $`sqrt()`[[1]]
## [1] 1
## 
## $`sqrt()`[[2]]
## [1] 1.414214
## 
## $`sqrt()`[[3]]
## [1] 1.732051
## 
## $`sqrt()`[[4]]
## [1] NA
## 
## $`sqrt()`[[5]]
## [1] 2
## 
## 
## $`ok?`
## $`ok?`[[1]]
## NULL
## 
## $`ok?`[[2]]
## NULL
## 
## $`ok?`[[3]]
## NULL
## 
## $`ok?`[[4]]
## &lt;simpleError in .f(...): non-numeric argument to mathematical function>
## 
## $`ok?`[[5]]
## NULL</code></pre>
</div>
<div>
<h2>Reduce a list to a single value</h2>
<pre><code>reduce(numbers, `*`)</code></pre>
<pre><code>## [1] 24024</code></pre>
<p><code>reduce()</code> applies the function <code>*</code> iteratively to the list of numbers. There’s also <code>accumulate()</code>:</p>
<pre><code>accumulate(numbers, `*`)</code></pre>
<pre><code>## [1]    11   132  1716 24024</code></pre>
<p>which keeps the intermediary results.</p>
<p>This function is very general, and you can reduce anything:</p>
<p>Matrices:</p>
<pre><code>mat1 &lt;- matrix(rnorm(10), nrow = 2)
mat2 &lt;- matrix(rnorm(10), nrow = 2)
mat3 &lt;- matrix(rnorm(10), nrow = 2)

list_mat &lt;- list(mat1, mat2, mat3)

reduce(list_mat, `+`)</code></pre>
<pre><code>##            [,1]       [,2]       [,3]       [,4]      [,5]
## [1,] -0.5228188  0.4813357  0.3808749 -1.1678164 0.3080001
## [2,] -3.8330509 -0.1061853 -3.8315768  0.3052248 0.3486929</code></pre>
<p>even data frames:</p>
<pre><code>df1 &lt;- as.data.frame(mat1)
df2 &lt;- as.data.frame(mat2)
df3 &lt;- as.data.frame(mat3)

list_df &lt;- list(df1, df2, df3)

reduce(list_df, dplyr::full_join)</code></pre>
<pre><code>## Joining, by = c(&quot;V1&quot;, &quot;V2&quot;, &quot;V3&quot;, &quot;V4&quot;, &quot;V5&quot;)
## Joining, by = c(&quot;V1&quot;, &quot;V2&quot;, &quot;V3&quot;, &quot;V4&quot;, &quot;V5&quot;)</code></pre>
<pre><code>##            V1         V2          V3         V4         V5
## 1  0.01587062  0.8570925  1.04330594 -0.5354500  0.7557203
## 2 -0.46872345  0.3742191 -1.88322431  1.4983888 -1.2691007
## 3 -0.60675851 -0.7402364 -0.49269182 -0.4884616 -1.0127531
## 4 -1.49619518  1.0714251  0.06748534  0.6650679  1.1709317
## 5  0.06806907  0.3644795 -0.16973919 -0.1439047  0.5650329
## 6 -1.86813223 -1.5518295 -2.01583786 -1.8582319  0.4468619</code></pre>
<p>Hope you enjoyed this list of useful functions! If you enjoy the content of my blog, you can follow me on <a target="_blank" rel="nofollow" href="https://www.twitter.com/brodriguesco">twitter</a>.</p>
</div>
<p></p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ADLfRsz_2rc">