title: Find immediate neighbors by group using data table or igraph
body: <p>I have a <em>data.table</em>:</p>
  
  <pre><code>groups &lt;- data.table(group = c("A", "B", "C", "D", "E", "F", "G"), 
                       code_1 = c(2,2,2,7,8,NA,5),
                       code_2 = c(NA,3,NA,3,NA,NA,2),
                       code_3 = c(4,1,1,4,4,1,8))
  
  group code_1 code_2 code_3
    A      2     NA      4
    B      2      3      1
    C      2     NA      1
    D      7      3      4
    E      8     NA      4
    F     NA     NA      1
    G      5      2      8
  </code></pre>
  
  <p>What I would like to achieve, is for each group to find the immediate neighbors based on the available codes. For example: Group A has immediate neighbors groups B, C due to code_1 (code_1 is equal to 2 in all groups) and has immediate neighbor groups D,E due to code_3 (code_3 is equal to 4 in all those groups).</p>
  
  <p>What I tried is for each code, subsetting the first column (group) based on the matches as follows:</p>
  
  <pre><code>groups$code_1_match = list()
  for (row in 1:nrow(groups)){
  
    set(groups, i=row, j="code_1_match", list(groups$group[groups$code_1[row] == groups$code_1]))
  }
  
    group code_1 code_2 code_3          code_1_match
      A      2     NA      4              A,B,C,NA
      B      2      3      1              A,B,C,NA
      C      2     NA      1              A,B,C,NA
      D      7      3      4                  D,NA
      E      8     NA      4                  E,NA
      F     NA     NA      1 NA,NA,NA,NA,NA,NA,...
      G      5      2      8                  NA,G
  </code></pre>
  
  <p>This "kinda" works but I would assume there is a more data table kind of way of doing this. I tried </p>
  
  <pre><code>groups[, code_1_match_2 := list(group[code_1 == groups$code_1])]
  </code></pre>
  
  <p>But this doesn't work.</p>
  
  <p>Am I missing some obvious data table trick to deal with it?</p>
  
  <p>My ideal case result would look like this (which currently would require using my method for all 3 columns and then concatenating the results):</p>
  
  <pre><code>group code_1 code_2 code_3    Immediate neighbors
    A      2     NA      4         B,C,D,E
    B      2      3      1         A,C,D,F
    C      2     NA      1         A,B,F
    D      7      3      4           B,A
    E      8     NA      4           A,D
    F     NA     NA      1           B,C
    G      5      2      8           
  </code></pre>