title: Dividing rows by rows using dplyr and tidyr
body: <p>I'm trying to process a (rather large) data set using dplyr, and I believe my issue stems from some poor use of the &quot;summarise_if&quot; function. Here's some code to generate dummy data that looks like mine:</p>
  <pre><code>df &lt;- data.frame(Block = c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4),
                   Treatment = c(rep(&quot;Control&quot;, 4), rep(&quot;CF&quot;, 4), rep(&quot;LR&quot;, 4)),
                   var1 = c(9, 12, 15, 16, 11, 9, 13, 11, 5, 11, 5, 11), 
                   var2 = c(0, 4, 9, 3, 6, 0, 0, 10, 15, 25, 0, 0))
  </code></pre>
  <p>I want to group the data by Block and Treatment and then, for each variable (var1, var2, ...), I want to divide the &quot;treatment&quot; value by the &quot;control&quot; value for each block. The resulting data frame would look something like this (I haven't done all the math for my dummy data, so I've just put the example formula for what should be calculated in each cell):</p>
  <pre><code>    Treatment    Block              var1              var2
      CF               1        CF/control        CF/control
      CF               2        CF/control        CF/control
      CF               3        CF/control        CF/control
      CF               4        CF/control        CF/control
      LR               1        LR/control        LR/control
      LR               2        LR/control        LR/control
      LR               3        LR/control        LR/control
      LR               4        LR/control        LR/control
  </code></pre>
  <p>Some of the values will be NaN or infinity because I'm dividing by zero for some treatments, but that's okay.</p>
  <p>I am able to produce what I want for single variables and treatments at a time using this code:</p>
  <pre><code>df %&gt;% 
    dplyr::group_by(Block) %&gt;%
    dplyr::summarise(value = var1[Treatment==&quot;CF&quot;] / var1[Treatment==&quot;Control&quot;])
  </code></pre>
  <p>But that becomes tedious over lots of variables and treatments. However, when I try and do it for the entire data frame, I'm stumped with all sorts of errors. My best guess looks something like this:</p>
  <pre><code>df %&gt;% 
    dplyr::group_by(Block, Treatment) %&gt;%
    dplyr::summarise_if(is.numeric, value = .[Treatment==&quot;CF&quot;] / .[Treatment==&quot;Control&quot;])
  </code></pre>
  <p>This gives me the error &quot;object 'Treatment' not found&quot; and also still forces me to go treatment-by-treatment.</p>
  <p>Any help is appreciated!</p>