title: applying a function to combinations of groups, holding 1 group fixed
body: <p>I have some data which looks like:</p>
  <pre><code>   grp    date                id              Y
     &lt;chr&gt;  &lt;dttm&gt;              &lt;chr&gt;       &lt;dbl&gt;
   1 group1 2020-09-01 00:00:00 04003      17039.
   2 group1 2020-09-01 00:00:00 04006      13233.
   3 group1 2020-09-01 00:00:00 04011_AM    7918.
   4 group1 2020-09-01 00:00:00 0401301_AD 22586.
   5 group1 2020-09-01 00:00:00 0401303    20527.
   6 group1 2020-09-01 00:00:00 0401305    29422.
   7 group2 2020-09-01 00:00:00 22017_AM    7088.
   8 group2 2020-09-01 00:00:00 22021_AM    8134.
   9 group2 2020-09-01 00:00:00 22039_AM   15842.
  10 group2 2020-09-01 00:00:00 22048      16142.
  </code></pre>
  <p>Which has different groups. I also have a function:</p>
  <pre><code>normaliseData &lt;-function(m){
    (m - min(m)) / (max(m) - min(m))
  }
  </code></pre>
  <p>I want to normalise the groups by the min and max of the pairwise values, holding <code>group1</code> fixed. That is, I want to normalise the data fixing <code>group1</code> so it will have the following combinations.</p>
  <ul>
  <li><code>group1</code> &amp; <code>group2</code></li>
  <li><code>group1</code> &amp; <code>group3</code></li>
  <li><code>group1</code> &amp; <code>group4</code></li>
  </ul>
  <p>Data:</p>
  <pre><code>data &lt;- structure(list(grp = c(&quot;group1&quot;, &quot;group1&quot;, &quot;group1&quot;, &quot;group1&quot;, 
  &quot;group1&quot;, &quot;group1&quot;, &quot;group2&quot;, &quot;group2&quot;, &quot;group2&quot;, &quot;group2&quot;, &quot;group2&quot;, 
  &quot;group2&quot;, &quot;group3&quot;, &quot;group3&quot;, &quot;group3&quot;, &quot;group3&quot;, &quot;group3&quot;, &quot;group3&quot;, 
  &quot;group4&quot;, &quot;group4&quot;, &quot;group4&quot;, &quot;group4&quot;, &quot;group4&quot;, &quot;group4&quot;), 
      date = structure(c(1598918400, 1598918400, 1598918400, 1598918400, 
      1598918400, 1598918400, 1598918400, 1598918400, 1598918400, 
      1598918400, 1598918400, 1598918400, 1598918400, 1598918400, 
      1598918400, 1598918400, 1598918400, 1598918400, 1598918400, 
      1598918400, 1598918400, 1598918400, 1598918400, 1598918400
      ), tzone = &quot;UTC&quot;, class = c(&quot;POSIXct&quot;, &quot;POSIXt&quot;)), id = c(&quot;04003&quot;, 
      &quot;04006&quot;, &quot;04011_AM&quot;, &quot;0401301_AD&quot;, &quot;0401303&quot;, &quot;0401305&quot;, 
      &quot;22017_AM&quot;, &quot;22021_AM&quot;, &quot;22039_AM&quot;, &quot;22048&quot;, &quot;22053_AM&quot;, 
      &quot;22054_AM&quot;, &quot;28002&quot;, &quot;28004&quot;, &quot;2800501&quot;, &quot;2800502&quot;, &quot;2800503&quot;, 
      &quot;2800504&quot;, &quot;31010_AM&quot;, &quot;31015_AM&quot;, &quot;31016&quot;, &quot;31019_AM&quot;, &quot;31023&quot;, 
      &quot;31029_AM&quot;), Y = c(17039.329, 13232.982, 7917.693, 22585.676, 
      20527.113, 29422.471, 7087.536, 8134.265, 15842.035, 16142.111, 
      11493.981, 6556.387, 22086.768, 11325.882, 53449.067, 83662.101, 
      78508.089, 66107.125, 5095.169, 5590.531, 17796.439, 6028.701, 
      39271.698, 3642.281)), row.names = c(NA, -24L), groups = structure(list(
      grp = c(&quot;group1&quot;, &quot;group2&quot;, &quot;group3&quot;, &quot;group4&quot;), .rows = structure(list(
          1:6, 7:12, 13:18, 19:24), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
      &quot;vctrs_vctr&quot;, &quot;list&quot;))), row.names = c(NA, 4L), class = c(&quot;tbl_df&quot;, 
  &quot;tbl&quot;, &quot;data.frame&quot;), .drop = TRUE), class = c(&quot;grouped_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  </code></pre>
  <p>EDIT:</p>
  <p>I am looking to apply the following:</p>
  <pre><code>#Min / max from group1 and group2
      data %&gt;% 
        filter(grp == &quot;group1&quot; | grp == &quot;group2&quot;) %&gt;% 
        mutate(
          normedOut = normaliseData(Y)
        )
  
  #Min / max from group1 and group3
      data %&gt;% 
        filter(grp == &quot;group1&quot; | grp == &quot;group3&quot;) %&gt;% 
        mutate(
          normedOut = normaliseData(Y)
        )
  
  #Min / max from group1 and group4
      data %&gt;% 
        filter(grp == &quot;group1&quot; | grp == &quot;group4&quot;) %&gt;% 
        mutate(
          normedOut = normaliseData(Y)
        )
  </code></pre>