title: How to assign conditions to paired rows in dplyr in R
body: <p>I have a long list of paired interactions between people. I need to assign an initiator to each pair at random. Therefore, for each pair there will be one person assigned a <code>1</code> and the other will be assigned a <code>0</code>.</p>
  
  <p>I have tried to find an answer to this, since I believe it must have been solved somewhere. Unfortunately, I do not think I am managing to hit the right keywords. The following answer was the most useful:</p>
  
  <p><a href="https://stackoverflow.com/questions/12925063/numbering-rows-within-groups-in-a-data-frame">Numbering rows within groups in a data frame</a></p>
  
  <p>Here is some dummy code and where I am up to (real data has many more columns and rows):</p>
  
  <pre><code>dummy_data &lt;- tibble(
    ID = c(1, 1, 2, 2, 3, 3),
    Sex = c("M", "F", "F", "M", "F", "M")
  )
  
  dummy_data &lt;- dummy_data %&gt;%
    group_by(ID) %&gt;%
    mutate(initiator = ifelse(row_number() == 1,
                              sample(0:1, 1),
                              (1 - row_number()[1])
                             )
          )
  </code></pre>
  
  <p>The idea being here is that I will assign a random value to the first person of each pair, and then take the opposite value for the second person.</p>
  
  <p>The issue I am having lies in the "false" part of the <code>ifelse()</code> function - I cannot seem to get the value of the previous row out.</p>
  
  <p>The desired output is a new column consisting of <code>0</code>s and <code>1</code>s only, where each pair has only one <code>0</code> and one <code>1</code>.</p>