title: Extend a row with rowise() %>% do() when do() returns a vector
body: <p>How do I make this code more tidy?</p>
  
  <pre><code>as_tibble(t(mapply(pmf, x=lhs$mu, y=lhs$sigma)))
  </code></pre>
  
  <p>Here <code>pmf()</code> is a helper function.</p>
  
  <pre><code>pmf &lt;- function(x, y) diff(pnorm(c(-Inf, -2, 0, 2, Inf), x, y))
  </code></pre>
  
  <p>This code takes the mean and standard deviation from each row,
  and returns the probability mass function (PMF) for each bin.</p>
  
  <p>So this</p>
  
  <pre><code># A tibble: 6 x 4
  i     j       mu    sigma
  &lt;int&gt; &lt;int&gt;    &lt;dbl&gt;    &lt;dbl&gt;
    1     1     1 1.890402 2.879148
  2     1     2 1.202357 2.655255
  3     1     3 1.186224 2.127293
  4     1     4 1.222278 2.228819
  5     1     5 1.760214 2.324657
  6     2     1 1.726712 2.574771
  </code></pre>
  
  <p>becomes this</p>
  
  <pre><code># A tibble: 6 x 8
  i     j       mu    sigma         V1        V2        V3        V4
  &lt;int&gt; &lt;int&gt;    &lt;dbl&gt;    &lt;dbl&gt;      &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;
    1     1     1 1.890402 2.879148 0.08831027 0.1674140 0.2594582 0.4848175
  2     1     2 1.202357 2.655255 0.11390009 0.2114385 0.2927260 0.3819354
  3     1     3 1.186224 2.127293 0.06709483 0.2214568 0.3604183 0.3510301
  4     1     4 1.222278 2.228819 0.07412598 0.2175836 0.3447228 0.3635676
  5     1     5 1.760214 2.324657 0.05288144 0.1715857 0.3166106 0.4589223
  6     2     1 1.726712 2.574771 0.07389306 0.1773358 0.2910357 0.4577354
  </code></pre>
  
  <p>after I use <code>bind_cols()</code> to add back the output.</p>
  
  <p>I tried the following (using the same <code>pmf()</code> helper function).</p>
  
  <pre><code>pmf &lt;- function(x, y) diff(pnorm(c(-Inf, -2, 0, 2, Inf), x, y))
  lhs %&gt;% rowwise() %&gt;% do(k = pmf(.$mu, .$sigma))
  </code></pre>
  
  <p>But it returns the PMF as a list.
  I would like the PMF as a row.
  This doesn't work for me because downstream I need to apply a function across the rows on each column <code>V#</code>.</p>
  
  <p>Is it possible to make this calculation more tidy?</p>
  
  <p>FWIW, here is the full calculation.</p>
  
  <pre><code>library(tidyverse)
  
  # find PMF from mu and sigma
  pmf &lt;- function(x, y) diff(pnorm(c(-Inf, -2, 0, 2, Inf), x, y))
  
  # find mean*variance for each column (i.e., PMF bin) __across rows___
  mean_var &lt;- function(x) mean(x)*var(x)
  
  # here are my data
  lhs &lt;- tibble(i = rep(1:2, each=5),
                j = rep(1:5, times=2),
                mu = 1 + runif(2*5),
                sigma = 2 + runif(2*5))
  
  # find PMF from mu and sigma
  rhs &lt;- as_tibble(t(mapply(pmf, x=lhs$mu, y=lhs$sigma)))
  
  # downstream calculations
  ans &lt;- 
    lhs %&gt;% 
    bind_cols(rhs) %&gt;% 
    group_by(i) %&gt;% 
    summarise_at(vars(starts_with('V')), funs(mean_var)) %&gt;% 
    gather(bin, mean_var, -i) %&gt;% 
    group_by(i) %&gt;% 
    summarise(ambg = sum(mean_var))
  
  # this finds PMF from mu and sigma, but as a list; I need a row
  lhs %&gt;% rowwise() %&gt;% do(k = pmf(.$mu, .$sigma))
  </code></pre>