我正在处理的数据是一个矩阵,其中矩阵中的每一列都是一个包含 2 个元素的列表。我要做的是计算有多少列是相同的。
我正在从tidygraph
对象中提取列表矩阵。我下面的例子应该更好地解释我的问题。首先,我创建一些数据,将它们转换为tidygraph
对象并将它们全部放入一个列表中,如下所示:
library(ggraph)
library(tidygraph)
# create some nodes and edges data
nodes <- data.frame(name = c("x4", NA, NA))
nodes1 <- data.frame(name = c("x4", "x2", NA, NA, "x1", NA, NA))
nodes2 <- data.frame(name = c("x1", NA, NA))
nodes3 <- data.frame(name = c("x6", NA, NA))
nodes4 <- data.frame(name = c("x10", "x3", NA, NA, NA))
nodes5 <- data.frame(name = c("x1", "x2", NA, NA, "x7", NA, NA))
edges <- data.frame(from = c(1,1), to = c(2,3))
edges1 <- data.frame(from = c(1, 2, 2, 1, 5, 5), to = c(2, 3, 4, 5, 6, 7))
edges2 <- data.frame(from = c(1,1), to = c(2,3))
edges3 <- data.frame(from = c(1,1), to = c(2,3))
edges4 <- data.frame(from = c(1,2,2,1), to = c(2,3,4,5))
edges5 <- data.frame(from = c(1, 2, 2, 1, 5, 5), to = c(2, 3, 4, 5, 6, 7))
# create the tbl_graphs
tg <- tbl_graph(nodes = nodes, edges = edges)
tg_1 <- tbl_graph(nodes = nodes1, edges = edges1)
tg_2 <- tbl_graph(nodes = nodes2, edges = edges2)
tg_3 <- tbl_graph(nodes = nodes3, edges = edges3)
tg_4 <- tbl_graph(nodes = nodes4, edges = edges4)
tg_5 <- tbl_graph(nodes = nodes5, edges = edges5)
# put into list
myList <- list(tg, tg_1, tg_2, tg_3, tg_4, tg_5)
为清楚起见,查看以下元素之一myList
:
myList[1]
[[1]]
# A tbl_graph: 3 nodes and 2 edges
#
# A rooted tree
#
# Node Data: 3 × 1 (active)
name
<chr>
1 x4
2 NA
3 NA
#
# Edge Data: 2 × 2
from to
<int> <int>
1 1 2
2 1 3
本质上,我想做的是遍历列表的每个元素,查看边缘数据,看看有多少是相同的。我确信有多种方法可以做到这一点......但我尝试通过使用一个tidygraph
函数来提取边缘数据并返回列表矩阵来做到这一点:
# extract just the edges data
resEdges <- sapply(myList, function(x) {
nodes <- tidygraph::activate(x, edges) %>%
tibble::as_tibble()
})
同样,为了清楚起见,查看第 1 列resEdges
如下所示:
> resEdges[,1]
$from
[1] 1 1
$to
[1] 2 3
所以,我想做的是遍历resEdges
's 列并计算相同列的频率。
在我的示例中,只有 3 个唯一列。所以,我想要的输出看起来像这样:
> edgeFreq
# A tibble: 3 × 3
from to frequency
1 1 2 3 3
1 2 2 1 5 5 2 3 4 5 6 7 2
1 2 2 1 2 3 4 5 1