title: calling a function in dplyr pipes - best approach
body: <p>I am reading the book <strong>R for data science</strong> (<a href="https://r4ds.had.co.nz/model-basics.html" rel="nofollow noreferrer">https://r4ds.had.co.nz/model-basics.html</a>). In the chapter on models I see the following code:</p>
  <pre><code>    models &lt;- tibble(
        a1 = runif(250, -20, 40),
        a2 = runif(250, -5, 5)
      )
      
      model1 &lt;- function(a, data) {
        a[1] + data$x * a[2]
      }
  
  
      sim1 &lt;- structure(list(x = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 
  4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 
  9L, 10L, 10L, 10L), y = c(4.2, 7.51, 2.13, 8.99, 10.24, 11.3, 
  7.36, 10.51, 10.51, 12.43, 11.89, 14.26, 19.13, 11.74, 16.02, 
  13.27, 15.96, 16.89, 20.09, 17.17, 19.94, 21.73, 18.39, 22.48, 
  26.78, 22.81, 21.13, 24.97, 23.35, 21.98)), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -30L))   # rounded values from modelr::sim1 from modelr 0.1.8
  
  
  
      measure_distance &lt;- function(mod, data) {
        diff &lt;- data$y - model1(mod, data)
        sqrt(mean(diff^2))
      }
      
      sim1_dist &lt;- function(a1, a2) {
        measure_distance(c(a1,a2), sim1)
      }
  </code></pre>
  <p>Here sim1 is a dataset of x, y values which is close to being linearly related.
  In order to compute the distance between models and actual data, the book proposes:</p>
  <pre><code>    models %&gt;%
        mutate(dist = purrr::map2_dbl(a1, a2, sim1_dist))
  </code></pre>
  <p>I want to know how is it different from (which gives the same result).</p>
  <pre><code>    models %&gt;%
        rowwise() %&gt;%
        mutate(dist = sim1_dist(a1, a2))
  </code></pre>
  <p>There are different approaches to solve the same problem. Just want to know why we would prefer one over the other.</p>
  <p>Also, even if I don't use rowwise, the answer does not change. Why? Shouldn't the arguments (a1, a2) sent to sim1_dist be row by row?</p>
  <p>Thank you.</p>