title: Fill in missing variables of family relationship matrix
body: <p>I have a dataframe of family relationships (parent, child, spouse, etc.) which is partially filled as per example below. I am trying to use R to fill in the missing variables <code>&lt;NA&gt;</code>, but not sure where to begin. I've tried using <code>ifelse()</code> but the code becomes so unwieldy I'm sure there must be a more efficient way.</p>
  <p><strong>Example dataframe</strong></p>
  <pre><code>   family person  R01    R02          R03         R04         R05         R06
  1       A      1    X Spouse        Child      Parent      Parent      Parent
  2       A      2 &lt;NA&gt;      X Child-in-law      Parent      Parent      Parent
  3       A      3 &lt;NA&gt;   &lt;NA&gt;            X GrandParent GrandParent GrandParent
  4       A      4 &lt;NA&gt;   &lt;NA&gt;         &lt;NA&gt;           X     Sibling     Sibling
  5       A      5 &lt;NA&gt;   &lt;NA&gt;         &lt;NA&gt;        &lt;NA&gt;           X     Sibling
  6       A      6 &lt;NA&gt;   &lt;NA&gt;         &lt;NA&gt;        &lt;NA&gt;        &lt;NA&gt;           X
  7       B      1    X Spouse       Parent      Parent        &lt;NA&gt;        &lt;NA&gt;
  8       B      2 &lt;NA&gt;      X       Parent      Parent        &lt;NA&gt;        &lt;NA&gt;
  9       B      3 &lt;NA&gt;   &lt;NA&gt;            X     Sibling        &lt;NA&gt;        &lt;NA&gt;
  10      B      4 &lt;NA&gt;   &lt;NA&gt;         &lt;NA&gt;           X        &lt;NA&gt;        &lt;NA&gt;
  11      C      1    X Parent         &lt;NA&gt;        &lt;NA&gt;        &lt;NA&gt;        &lt;NA&gt;
  12      C      2 &lt;NA&gt;      X         &lt;NA&gt;        &lt;NA&gt;        &lt;NA&gt;        &lt;NA&gt;
  </code></pre>
  <p>where R01 is the relationship of <code>person x</code> to <code>person 1</code>. For the second row of the dataframe above I would need <code>R01</code> to be <code>Spouse</code> as that matches with <code>R02</code> in the first row. The relationships would match as per the df below.</p>
  <p><strong>Relationship Matches</strong></p>
  <pre><code>     [,1]            [,2]           
  [1,] &quot;Spouse&quot;        &quot;Spouse&quot;       
  [2,] &quot;Parent&quot;        &quot;Child&quot;        
  [3,] &quot;Child&quot;         &quot;Parent&quot;       
  [4,] &quot;GrandParent&quot;   &quot;GrandChild&quot;   
  [5,] &quot;GrandChild&quot;    &quot;GrandParent&quot;  
  [6,] &quot;Parent-in-Law&quot; &quot;Child-in-law&quot; 
  [7,] &quot;Child-in-Law&quot;  &quot;Parent-in-law&quot;
  </code></pre>
  <p><strong>Code to replicate Example</strong></p>
  <pre><code>df1 &lt;- data.frame(family = c(rep(&quot;A&quot;, 6), rep(&quot;B&quot;, 4), rep(&quot;C&quot;,2)),
                  person = c(1:6, 1:4, 1:2),
                  R01 = c(&quot;X&quot;, rep(NA,5),&quot;X&quot;, rep(NA,3),&quot;X&quot;,NA),
                  R02 = c(&quot;Spouse&quot;, &quot;X&quot;, rep(NA,4), &quot;Spouse&quot;, &quot;X&quot;, NA, NA, &quot;Parent&quot;, &quot;X&quot;),
                  R03 = c(&quot;Child&quot;, &quot;Child-in-law&quot;, &quot;X&quot;, NA, NA, NA, &quot;Parent&quot;, &quot;Parent&quot;, &quot;X&quot;, rep(NA,3)),
                  R04 = c(rep(&quot;Parent&quot;,2), &quot;GrandParent&quot;, &quot;X&quot;, NA, NA, rep(&quot;Parent&quot;,2), &quot;Sibling&quot;, &quot;X&quot;, NA, NA),
                  R05 = c(rep(&quot;Parent&quot;,2), &quot;GrandParent&quot;, &quot;Sibling&quot;, &quot;X&quot;, rep(NA,7)),
                  R06 = c(rep(&quot;Parent&quot;,2), &quot;GrandParent&quot;, rep(&quot;Sibling&quot;,2), &quot;X&quot;, rep(NA,6)))
  
  relationshipmatch &lt;- matrix(c(&quot;Spouse&quot;, &quot;Parent&quot;, &quot;Child&quot;, &quot;GrandParent&quot;, &quot;GrandChild&quot;, &quot;Parent-in-law&quot;, &quot;Child-in-law&quot;, &quot;Spouse&quot;, &quot;Child&quot;, &quot;Parent&quot;, &quot;GrandChild&quot;, &quot;GrandParent&quot;, &quot;Child-in-law&quot;, &quot;Parent-in-law&quot;), ncol = 2)
  </code></pre>