title: Accessing relational data with dplyr
body: <p>Given the following dataframe <code>data</code>:</p>
  <pre><code>set.seed(123)
  data &lt;- data.frame(ID = c(1,1,1,2,2,2),
                     Loc = rep(paste(&quot;Loc&quot;, seq(1:3), sep = &quot;&quot;)),
                     Var  = rnorm(6))
  </code></pre>
  <p>we can use <code>dplyr::group_by</code> and <code>dplyr::mutate</code> to create a new column showing the max value of <code>Var</code> for each <code>ID</code>:</p>
  <pre><code>library(dplyr)
  data%&gt;%
    group_by(ID)%&gt;%
    mutate(MaxVar = max(Var))
  </code></pre>
  <p>What if we want to create another new column (we will call it <code>MaxVarLoc</code>) to show the <code>Loc</code> associated with the <code>MaxVar</code> for each <code>ID</code>?</p>
  <p>The result would look like this:</p>
  <pre class="lang-r prettyprint-override"><code>ID Loc     Var           MaxVar      MaxVarLoc
  
  1   Loc1    -0.56047565 1.558708    LOC3
  1   Loc2    -0.23017749 1.558708    LOC3    
  1   Loc3    1.55870831  1.558708    LOC3    
  2   Loc1    0.07050839  1.715065    LOC3    
  2   Loc2    0.12928774  1.715065    LOC3    
  2   Loc3    1.71506499  1.715065    LOC3    
  #The result just happens to be `Loc3` in both cases here
  </code></pre>
  <p>How can we access relational (i.e., specify which <code>Loc</code> goes with the max value of <code>Var</code> for each <code>ID</code>) data this way in a <code>dplyr</code> framework?</p>