如何识别群体间的个体间重访?

How to identify groups of interindividual revisitation?

我有一个来自“递归”包的输出数据框,它根据 GPS 点计算几个人的重访率。在数据框中,我有 18 列,包括“site”和“id”,以及 > 43,000 行。

我有两个问题:(1) 哪些网站被多人使用,(2) 哪些人共享同一个网站。

我按站点和 ID 将数据框分组,然后仅过滤具有多个连接 ID 的站点:

library(tidyverse)
sites <- tab %>% group_by(site, id) %>% summarise(n = n()) # gives me all sites and connected ids
sites2 <- sites %>% group_by(site) %>% summarise(n = n()) # gives me all sites and the count of connected ids
sites3 <- subset(sites2, n > 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 <- left_join(sites3, df, by = "site")
#group again by site and id
filtered2 <- filtered %>% group_by(site, id) %>% summarise(n = n())

我不是 R 方面的专家,所以我想会有更简单或更简洁的方法来执行此操作,但它适用于我的 R 知识。有了这个我就知道不同的人访问了哪些网站。 现在我有这样的东西:

# A tibble: 3,041 x 3
   site         id     n
   <chr>     <int> <int>
 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

现在我卡住了。我想将“组”分配给使用同一站点的个人。例如,您可以看到 ID 152 和 160 都在使用站点 1,而 164 和 166 使用站点 1000、1001、1002 等。在这种情况下,“group1”将分配给 id 152 和 160,“group2”将分配给 164 和 166。

有没有办法在 R 中做到这一点?有 37 个人,仍然有超过 3,000 行数据,因此需要手动完成很多工作。有些网站被 3 或 4 个人使用,我不确定是否总是有相同的 id 组合连接到一个网站,所以我无法预先定义组。

这是分组数据框的片段:

df <- structure(list(site = c("site 1", "site 1", "site 1000", "site 1000", 
"site 1001", "site 1001", "site 1002", "site 1002", "site 1003", 
"site 1003", "site 1007", "site 1007", "site 1008", "site 1008", 
"site 1009", "site 1009", "site 1015", "site 1015", "site 1019", 
"site 1019", "site 1020", "site 1020", "site 1022", "site 1022", 
"site 1024", "site 1024", "site 1034", "site 1034", "site 1035", 
"site 1035", "site 1036", "site 1036", "site 107", "site 107", 
"site 108", "site 108", "site 111", "site 111", "site 131", "site 131", 
"site 132", "site 132", "site 133", "site 133", "site 134", "site 134", 
"site 135", "site 135", "site 136", "site 136"), 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("grouped_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -50L), groups = structure(list(
    site = c("site 1", "site 1000", "site 1001", "site 1002", 
    "site 1003", "site 1007", "site 1008", "site 1009", "site 1015", 
    "site 1019", "site 1020", "site 1022", "site 1024", "site 1034", 
    "site 1035", "site 1036", "site 107", "site 108", "site 111", 
    "site 131", "site 132", "site 133", "site 134", "site 135", 
    "site 136"), .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("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -25L), .drop = TRUE))

谢谢!

好的,我找到了 work-around。可能不是最优雅的,但万一有人有同样的问题:

我再次使用 dplyr 并折叠了每个组的所有 ID,然后按新组对其进行分组:

df <- filtered2 %>% 
  group_by(site) %>% 
  mutate(groups = paste0(id, collapse = " "))
df2 <- df %>% group_by(groups) %>% summarise(n = n())

然后这给了我所需的所有 ID 组合(以及这些组合出现的频率)。