title: Referencing a column to divide a tibble by in R
body: <p>I am attempting to divide all numeric columns in a tibble by one of the columns within the tibble. I am able to do this by explicitly writing the name of the column, however I am trying to structure it as a function so I can then apply it to a list of tibbles. I have tried to explain as clearly as possible with examples below. </p>
  
  <p>If this is one of my tibbles: </p>
  
  <pre><code>&gt; data &lt;- tibble(
           ID = sample(letters, 3),
           x = 1:3, 
           y = 9:11, 
           z = y^2-x)
  
  
  &gt; data
  
  # A tibble: 3 x 4
    ID        x     y     z
    &lt;chr&gt; &lt;int&gt; &lt;int&gt; &lt;dbl&gt;
  1 r         1     9    80
  2 k         2    10    98
  3 f         3    11   118
  </code></pre>
  
  <p>I want to divide columns x, y and z by z, and can do this by: </p>
  
  <pre><code> &gt; data %&gt;% mutate_if(is.numeric, ~ . / z) -&gt; data.new
  
  
   &gt; data.new
  
  # A tibble: 3 x 4
    ID         x      y     z
    &lt;chr&gt;  &lt;dbl&gt;  &lt;dbl&gt; &lt;dbl&gt;
  1 r     0.0125 0.112      1
  2 k     0.0204 0.102      1
  3 f     0.0254 0.0932     1
  </code></pre>
  
  <p>But I don't want to have to explicitly name column 'z'. I want to be able to pull out the name and then use it. I attempted to do this: </p>
  
  <pre><code># retrieve name of last column 
  &gt; colnames(data)[ncol(data)] -&gt; last.col
  
  # reference the above to perform the division
  &gt; data %&gt;% mutate_if(is.numeric, ~ . / last.col) -&gt; data.new2
  </code></pre>
  
  <p>But I get the following error:</p>
  
  <pre><code>Error in ./last.col : non-numeric argument to binary operator
  </code></pre>
  
  <p>I understand that this is because 'last.col' is of character class, but i'm unsure of how to fix it or move forward. </p>
  
  <p>I would greatly appreciate any suggestions!</p>
  
  <p>:) </p>