title: Divide all rows by a reference row, by group
body: <p>Here is a sample table I'm working with:</p>
  
  <pre><code>n = c(rep("A",3),rep("B",3),rep("C",3))
  m = c("X", "Y", "Z", "X", "Y", "Z", "X", "Y", "Z")
  s = 1:9 
  b = 5:13
  c = 20:28
  d = c(rep("abc", 9))
  df = data.frame(d, n, m, s, b, c) 
  df
  </code></pre>
  
  <p>Below is what the table looks like:</p>
  
  <pre><code>d   n   m   s   b   c
  abc A   X   1   5   20
  abc A   Y   2   6   21
  abc A   Z   3   7   22
  abc B   X   4   8   23
  abc B   Y   5   9   24
  abc B   Z   6   10  25
  abc C   X   7   11  26
  abc C   Y   8   12  27
  abc C   Z   9   13  28
  </code></pre>
  
  <p>I'll refer to each row as a concatenation of its column n and m values (e.g. AX row, CZ row, etc.) I would like to divide each of the A rows by the AY row, each of the B rows by the BY row, and each of the C rows by the CY row (may not always be Y, sometimes X or Z). I essentially want to rebase the data (columns s, b, and c) by group (where column n is the group), using X, Y, or Z (column m) as the base. </p>
  
  <p>I need columns d, n, and m to remain untouched. If possible, I'd like to do this by referencing X, Y, or Z in the code directly to denote which row will be the base, rather than by [1], [2], or [3] (as they may not always be in the same order, and it's more intuitive to the user). I'm new to R and using dplyr but I haven't been able to figure out a good way of doing this.</p>
  
  <p>Thanks for your help.</p>