title: Using mutate_at to insert relative values after each column (relative to the second column of the tibble)
body: <p>I have a dataframe (tibble) with multiple columns, and for each column after the first two I'd like to keep the absolute value, but also insert the value relative to the second column.
  For example, I start with the following data frame (the column names might vary!):</p>
  
  <pre><code>df = tibble(val1 = 5:10, val2 = 10:15, val3 = 15:20); df
  # A tibble: 6 x 3
     val1  val2  val3
    &lt;int&gt; &lt;int&gt; &lt;int&gt;
  1     5    10    15
  2     6    11    16
  3     7    12    17
  4     8    13    18
  5     9    14    19
  6    10    15    20
  </code></pre>
  
  <p>Now, for the <code>val2</code> and <code>val3</code> columns, I also want to insert a column right after <code>val2</code> and after <code>val3</code> showing the value relative to <code>val1</code>. How can I do that???</p>
  
  <p>The resulting tibble should look like:</p>
  
  <pre><code>dfrel = tibble(val1 = 5:10, val2 = 10:15, rel2 = val2/val1, val3 = 15:20, rel3 = val3/val1)
  dfrel
  # A tibble: 6 x 5
     val1  val2  rel2  val3  rel3
    &lt;int&gt; &lt;int&gt; &lt;dbl&gt; &lt;int&gt; &lt;dbl&gt;
  1     5    10  2.00    15  3.00
  2     6    11  1.83    16  2.67
  3     7    12  1.71    17  2.43
  4     8    13  1.62    18  2.25
  5     9    14  1.56    19  2.11
  6    10    15  1.50    20  2.00
  </code></pre>
  
  <p>Unfortunately, I'm unable to write the proper mutate_at call to insert that relative column right after each value column. In fact, I'm unable to write a mutate_at using funs() that modifies a column by accessing other columns (by position rather than name). </p>
  
  <p>Replacing val2 and val3 by the relative values works (using a lambda function instead of funs), but does not preserve the original val2 and val3 columns as required:</p>
  
  <pre><code>df %&gt;%
       mutate_at(vars(-1), function(v) v/.[[1]])
  # A tibble: 6 x 3
     val1  val2  val3
    &lt;int&gt; &lt;dbl&gt; &lt;dbl&gt;
  1     5  2.00  3.00
  2     6  1.83  2.67
  3     7  1.71  2.43
  4     8  1.62  2.25
  5     9  1.56  2.11
  6    10  1.50  2.00
  </code></pre>
  
  <p>All my attempts to use <code>funs()</code> fail:</p>
  
  <pre><code>df %&gt;%
       mutate_at(vars(-1), funs(./.tbl[[1]]))
  Error in mutate_impl(.data, dots) : 
    Evaluation error: object '.tbl' not found.
  
  df %&gt;%
       mutate_at(vars(-1), funs(function(v) v/.[[1]]))
  Error in mutate_impl(.data, dots) : 
    Column `val2` is of unsupported type function
  </code></pre>
  
  <p>One complication compared to <a href="https://stackoverflow.com/questions/39209987/using-functions-of-multiple-columns-in-a-dplyr-mutate-at-call">Using functions of multiple columns in a dplyr mutate_at call</a> is that my val1 column does not have a fixed name (i.e. it is not always called <code>val1</code>), so I cannot use it by name in the funs arguments. The other complication is that the tibble is created on the fly (using lots of pipe operators) and is typically not stored in a variable, so I cannot simply divide by df[[1]]...</p>
  
  <p>So, what is the proper dplyr approach to insert relative columns (i.e. percent of the first column) after each column?</p>