过滤 table 中的定向共现

Filter directed co-occurrences in a table

我有可以用两列表示的共现数据。每列中的条目来自同一组可能性。最终我的目标是绘制一个有向网络,但首先我想将 table 分成互惠的网络(即 X->Y 和 Y->X)和仅出现在一个方向上的网络(即仅Y->Z)。这是一个例子:

library(tidyverse)

# Example data
from <-  c("A", "B", "F", "Q", "T", "S", "D", "E", "A", "T", "F")
to <- c("E", "D", "Q", "S", "F", "T", "B", "A", "D", "A", "E")
df <- data_frame(from, to)
df
# A tibble: 11 x 2
   from  to   
   <chr> <chr>
 1 A     E    
 2 B     D    
 3 F     Q    
 4 Q     S    
 5 T     F    
 6 S     T    
 7 D     B    
 8 E     A    
 9 A     D    
10 T     A    
11 F     E   

这是我想要的输出:

# Desired output 1 - reciprocal co-occurrences
df %>% 
  slice(c(1,2)) %>% 
  rename(item1 = from, item2 = to)

# A tibble: 2 x 2
  item1 item2
  <chr> <chr>
1 A     E    
2 B     D

# Desired output 2 - single occurrences
df %>% 
  slice(c(3,4,6,6,9,10,11))

# A tibble: 7 x 2
  from  to   
  <chr> <chr>
1 F     Q    
2 Q     S    
3 S     T    
4 S     T    
5 A     D    
6 T     A    
7 F     E 

如果同时出现是相互的,则条目的顺序无关紧要我只需要他们的名字同时出现而不是我需要知道方向。

这感觉像是一个图形问题,所以我尝试了一下,但不熟悉处理此类数据,而且大多数教程似乎都涵盖了无向图。查看 tidygraph 包,据我所知使用 igraph 包,我试过这个:

library(tidygraph)

df %>% 
  as_tbl_graph(directed = TRUE) %>%
  activate(edges) %>% 
  mutate(recip_occur = edge_is_mutual()) %>% 
  as_tibble() %>%
  filter(recip_occur == TRUE) 
# A tibble: 4 x 3
   from    to recip_occur
  <int> <int> <lgl>      
1     1     8 TRUE       
2     2     7 TRUE       
3     7     2 TRUE       
4     8     1 TRUE   

然而,这将边缘与节点分离并重复相互共现。有没有人对这类数据有经验?

试试这个:

数据:

from <-  c("A", "B", "F", "Q", "T", "S", "D", "E", "A", "T", "F")
to <- c("E", "D", "Q", "S", "F", "T", "B", "A", "D", "A", "E")
df <- data_frame(from, to)

代码:

recursive_IND <-
1:nrow(df) %>% 
sapply(function(x){
    if(any((as.character(df[x,]) == t(df[,c(2,1)])) %>% {.[1,] & .[2,]}))
        return(T) else return(F)
    })

df[recursive_IND,][!(df[recursive_IND,] %>% apply(1,sort) %>% t %>% duplicated(.)),]
df[!recursive_IND,]

结果:

# A tibble: 2 x 2
#  from  to   
#  <chr> <chr>
#1 A     E    
#2 B     D    

# A tibble: 7 x 2
#  from  to   
#  <chr> <chr>
#1 F     Q    
#2 Q     S    
#3 T     F    
#4 S     T    
#5 A     D    
#6 T     A    
#7 F     E