title: Grouping similar elements together
body: <p>I am trying to group similar entities together and can't find an easy way to do so.</p>
  <p>For example, here is a table:</p>
  <pre><code>                  Names Initial_Group Final_Group
  1          James,Gordon             6           A
  2          James,Gordon             6           A
  3          James,Gordon             6           A
  4          James,Gordon             6           A
  5          James,Gordon             6           A
  6          James,Gordon             6           A
  7                Amanda             1           A
  8                Amanda             1           A
  9                Amanda             1           A
  10        Gordon,Amanda             5           A
  11        Gordon,Amanda             5           A
  12        Gordon,Amanda             5           A
  13        Gordon,Amanda             5           A
  14        Gordon,Amanda             5           A
  15        Gordon,Amanda             5           A
  16        Gordon,Amanda             5           A
  17        Gordon,Amanda             5           A
  18 Edward,Gordon,Amanda             4           A
  19 Edward,Gordon,Amanda             4           A
  20 Edward,Gordon,Amanda             4           A
  21                 Anna             2           B
  22                 Anna             2           B
  23                 Anna             2           B
  24         Anna,Leonard             3           B
  25         Anna,Leonard             3           B
  26         Anna,Leonard             3           B
  </code></pre>
  <p>I am unsure how to get the '<strong>Final_Group</strong>' field, in the table above.</p>
  <p>For that, I need to assign any element that has any connections to another element, and group them together:</p>
  <ul>
  <li><p>For example, rows 1 to 20 needs to be grouped together because they are all connected by at least one or more elements.</p>
  </li>
  <li><p>So for rows 1 to 6, 'James, Gordon' appear, and since &quot;Gordon&quot; is  in rows 10:20, they all have to be grouped.  Likewise, since 'Amanda' appears in rows 7:9, these have to be grouped with &quot;James,Gordon&quot;, &quot;Gordon, Amanda&quot;, and &quot;Edward, Gordon, Amanda&quot;.</p>
  </li>
  </ul>
  <p>Below is code to generate the initial data:</p>
  <pre><code># Manually generating data
  Names &lt;- c(rep('James,Gordon',6)
            ,rep('Amanda',3)
            ,rep('Gordon,Amanda',8)
            ,rep('Edward,Gordon,Amanda',3)
            ,rep('Anna',3)
            ,rep('Anna,Leonard',3))
  Initial_Group &lt;- rep(1:6,c(6,3,8,3,3,3))
  Final_Group &lt;- rep(c('A','B'),c(20,6))
  data &lt;- data.frame(Names,Initial_Group,Final_Group)
  
  # Grouping
  data %&gt;%
    select(Names) %&gt;%
    mutate(Initial_Group=group_indices(.,Names))
  </code></pre>
  <p>Does anyone know of anyway to do this in R?</p>