title: How to identify groups of interindividual revisitation?
body: <p>I have an output data frame from the &quot;recurse&quot; package calculating the revisitation rate of several individuals based on GPS points. In the data frame I have 18 columns, including &quot;site&quot; and &quot;id&quot;, and &gt; 43,000 rows.</p>
  <p>I have two questions: (1) What sites are used by multiple individuals and (2) which individuals share the same site.</p>
  <p>I grouped the data frame by site and id to then filter only the sites with more than one connected id:</p>
  <pre><code>library(tidyverse)
  sites &lt;- tab %&gt;% group_by(site, id) %&gt;% summarise(n = n()) # gives me all sites and connected ids
  sites2 &lt;- sites %&gt;% group_by(site) %&gt;% summarise(n = n()) # gives me all sites and the count of connected ids
  sites3 &lt;- subset(sites2, n &gt; 1) # gives me only sites with more than one connected id
  #filter original data frame to only have sites that are connected to more that one id
  filtered &lt;- left_join(sites3, df, by = &quot;site&quot;)
  #group again by site and id
  filtered2 &lt;- filtered %&gt;% group_by(site, id) %&gt;% summarise(n = n())
  </code></pre>
  <p>I'm not an expert in R so I guess there would've been an easier or cleaner way to do this, but it worked with my R knowledge. With this I know which sites are visited by different individuals.
  Now I have something that looks like this:</p>
  <pre><code># A tibble: 3,041 x 3
     site         id     n
     &lt;chr&gt;     &lt;int&gt; &lt;int&gt;
   1 site 1      152     2
   2 site 1      160    13
   3 site 1000   164     4
   4 site 1000   166     1
   5 site 1001   164     2
   6 site 1001   166     1
   7 site 1002   164     4
   8 site 1002   166     1
   9 site 1003   164     3
  10 site 1003   166     3
  # ... with 3,031 more rows
  </code></pre>
  <p>Now I'm stuck. I would like to assign &quot;groups&quot; to the individuals using the same site. For example you can see that id 152 and 160 are both using site 1, and 164 and 166 use the sites 1000, 1001, 1002, and so on. In this case, &quot;group1&quot; would be assigned to id 152 and 160, and &quot;group2&quot; to 164 and 166.</p>
  <p>Is there a way to do that in R? There are 37 individuals and still &gt; 3,000 rows of data, so it's a lot to go through by hand. Some sites are used by 3 or 4 individuals, and I'm not sure if there are always the same combinations of id connected to a site, so I can't define the groups beforehand.</p>
  <p>Here is a snippet of the grouped data frame:</p>
  <pre><code>df &lt;- structure(list(site = c(&quot;site 1&quot;, &quot;site 1&quot;, &quot;site 1000&quot;, &quot;site 1000&quot;, 
  &quot;site 1001&quot;, &quot;site 1001&quot;, &quot;site 1002&quot;, &quot;site 1002&quot;, &quot;site 1003&quot;, 
  &quot;site 1003&quot;, &quot;site 1007&quot;, &quot;site 1007&quot;, &quot;site 1008&quot;, &quot;site 1008&quot;, 
  &quot;site 1009&quot;, &quot;site 1009&quot;, &quot;site 1015&quot;, &quot;site 1015&quot;, &quot;site 1019&quot;, 
  &quot;site 1019&quot;, &quot;site 1020&quot;, &quot;site 1020&quot;, &quot;site 1022&quot;, &quot;site 1022&quot;, 
  &quot;site 1024&quot;, &quot;site 1024&quot;, &quot;site 1034&quot;, &quot;site 1034&quot;, &quot;site 1035&quot;, 
  &quot;site 1035&quot;, &quot;site 1036&quot;, &quot;site 1036&quot;, &quot;site 107&quot;, &quot;site 107&quot;, 
  &quot;site 108&quot;, &quot;site 108&quot;, &quot;site 111&quot;, &quot;site 111&quot;, &quot;site 131&quot;, &quot;site 131&quot;, 
  &quot;site 132&quot;, &quot;site 132&quot;, &quot;site 133&quot;, &quot;site 133&quot;, &quot;site 134&quot;, &quot;site 134&quot;, 
  &quot;site 135&quot;, &quot;site 135&quot;, &quot;site 136&quot;, &quot;site 136&quot;), id = c(152L, 
  160L, 164L, 166L, 164L, 166L, 164L, 166L, 164L, 166L, 164L, 166L, 
  164L, 166L, 164L, 166L, 164L, 166L, 164L, 166L, 164L, 166L, 164L, 
  166L, 164L, 166L, 164L, 166L, 164L, 166L, 164L, 166L, 155L, 161L, 
  155L, 161L, 155L, 161L, 155L, 161L, 155L, 161L, 155L, 161L, 155L, 
  161L, 155L, 161L, 155L, 161L), n = c(2L, 13L, 4L, 1L, 2L, 1L, 
  4L, 1L, 3L, 3L, 5L, 8L, 4L, 6L, 5L, 17L, 1L, 1L, 3L, 1L, 3L, 
  2L, 3L, 1L, 3L, 1L, 3L, 1L, 1L, 5L, 1L, 4L, 5L, 3L, 5L, 3L, 2L, 
  1L, 5L, 3L, 5L, 3L, 5L, 3L, 5L, 3L, 5L, 3L, 4L, 2L)), class = c(&quot;grouped_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -50L), groups = structure(list(
      site = c(&quot;site 1&quot;, &quot;site 1000&quot;, &quot;site 1001&quot;, &quot;site 1002&quot;, 
      &quot;site 1003&quot;, &quot;site 1007&quot;, &quot;site 1008&quot;, &quot;site 1009&quot;, &quot;site 1015&quot;, 
      &quot;site 1019&quot;, &quot;site 1020&quot;, &quot;site 1022&quot;, &quot;site 1024&quot;, &quot;site 1034&quot;, 
      &quot;site 1035&quot;, &quot;site 1036&quot;, &quot;site 107&quot;, &quot;site 108&quot;, &quot;site 111&quot;, 
      &quot;site 131&quot;, &quot;site 132&quot;, &quot;site 133&quot;, &quot;site 134&quot;, &quot;site 135&quot;, 
      &quot;site 136&quot;), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10, 
          11:12, 13:14, 15:16, 17:18, 19:20, 21:22, 23:24, 25:26, 
          27:28, 29:30, 31:32, 33:34, 35:36, 37:38, 39:40, 41:42, 
          43:44, 45:46, 47:48, 49:50), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
      &quot;vctrs_vctr&quot;, &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
  ), row.names = c(NA, -25L), .drop = TRUE))
  </code></pre>
  <p>Thank you!</p>