title: How to match pairs of values and then subtract value of a column?
body: <p>I am currently working with a dataset containing all grand slam tennis matches between 2010 and 2019. The data frame contains two rows per match, one row with containing info about one player (the winner) and another containing info about the other player (the loser). The commonality between each of these pairs is the <code>match_ID</code> variable.</p>
  <p>I would like to create a new variable called rank difference. The idea would be to have for each row the difference in ATP rank between the winner and the loser.</p>
  <p>Here is what a subset of the data frame I am working with looks like:</p>
  <pre><code># A tibble: 9,290 x 5
  # Groups:   player_id [444]
     match_id    player_id   rank winner full_name           
     &lt;chr&gt;       &lt;chr&gt;      &lt;dbl&gt; &lt;fct&gt;  &lt;chr&gt;               
   1 m_2019_A_0  atp_104731     6 True   Kevin Anderson      
   2 m_2019_A_1  atp_105932    20 True   Nikoloz Basilashvili
   3 m_2019_A_2  atp_105430    98 True   Radu Albot          
   4 m_2019_A_3  atp_105882   137 True   Stefano Travaglia   
   5 m_2019_A_4  atp_104269    28 True   Fernando Verdasco   
   6 m_2019_A_5  atp_104655    94 True   Pablo Cuevas        
   7 m_2019_A_7  atp_126774    15 True   Stefanos Tsitsipas  
   8 m_2019_A_8  atp_105777    21 True   Grigor Dimitrov     
   9 m_2019_A_9  atp_126207    39 True   Frances Tiafoe      
  10 m_2019_A_10 atp_104745     2 True   Rafael Nadal        
  # ... with 9,280 more rows
  </code></pre>
  <p>Here is what I tried but did not work:</p>
  <pre><code>final_match_with_player %&gt;%
  group_by(match_id) %&gt;%
  mutate(diff_rank = rank[winner == 'True'] - rank[winner == 'False'])
  </code></pre>
  <p>Do you have any idea of how I could do that ?</p>
  <p>Thank you very much in advance !</p>