计算 R 中 tidygraph 对象列表中相同列的频率?

Count the frequency of identical columns in a list of tidygraph objects in R?

我有一些包含在列表中的 tidygraph 对象。我正在尝试计算相同列(在 tidygraph 节点数据中)的频率。

例如,

如果我创建一些节点和边缘数据,将它们变成 tidygraph 对象,并将它们放入列表中,如下所示:

library(tidygraph)

# create some node and edge data for the tbl_graph
nodes <- data.frame(name = c("x4", NA, NA),
                    val = c(1, 5, 2))
nodes2 <- data.frame(name = c("x4", NA, NA),
                    val = c(3, 2, 2))
nodes3 <- data.frame(name = c("x4", NA, NA),
                     val = c(5, 6, 7))
nodes4 <- data.frame(name = c("x4", "x2", NA, NA, "x1", NA, NA),
                     val = c(3, 2, 2, 1, 1, 2, 7))
nodes5 <- data.frame(name= c("x1", "x2", NA),
                     val = c(7, 4, 2))
nodes6 <- data.frame(name = c("x1", "x2", NA),
                     val = c(2, 1, 3))

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))

# create the tbl_graphs
tg   <- tbl_graph(nodes = nodes,  edges = edges)
tg_1 <- tbl_graph(nodes = nodes2, edges = edges)
tg_2 <- tbl_graph(nodes = nodes2, edges = edges)
tg_3 <- tbl_graph(nodes = nodes4, edges = edges1)
tg_4 <- tbl_graph(nodes = nodes5, edges = edges)
tg_5 <- tbl_graph(nodes = nodes6, edges = edges)


# put into list
myList <- list(tg, tg_1, tg_2, tg_3, tg_4, tg_5)

我们可以看到 tgtg_1tg_2 都有相同的 name 列。同样,tg_4tg_5 在节点数据中具有相同的 name 列。

我正在尝试想出一种方法来计算具有相同 name 列的 tidygraph 对象的频率。我希望能够 return 一个 tidygraph 对象的列表,也许添加了另一列来显示频率。 在我的例子中,val 列并不重要,所以我想要的输出看起来像这样:

 [[1]]
# A tbl_graph: 3 nodes and 2 edges
#
# A rooted tree
#
# Node Data: 3 × 2 (active)
  name  frequency
  <chr>     <dbl>
1 x4            3
2 NA            3
3 NA            3
#
# Edge Data: 2 × 2
   from    to
  <int> <int>
1     1     2
2     1     3

[[2]]
# A tbl_graph: 3 nodes and 2 edges
#
# A rooted tree
#
# Node Data: 3 × 2 (active)
  name  frequency
  <chr>     <dbl>
1 x1            2
2 x2            2
3 NA            2
#
# Edge Data: 2 × 2
   from    to
  <int> <int>
1     1     2
2     1     3

[[3]]
# A tbl_graph: 7 nodes and 6 edges
#
# A rooted tree
#
# Node Data: 7 × 2 (active)
  name  frequency
  <chr>     <dbl>
1 x4            1
2 x2            1
3 NA            1
4 NA            1
5 x1            1
6 NA            1
# … with 1 more row
#
# Edge Data: 6 × 2
   from    to
  <int> <int>
1     1     2
2     2     3
3     2     4
# … with 3 more rows

需要说明的是,在我上面的示例中,包含 x4, NA, NAname 列在我的原始对象列表中出现了 3 次。因此频率计数为 3。类似地,等于 x1, x2, NAname 列在 myList 中出现 2 次,因此它的频率为 2... 等等

但是,我愿意接受任何关于 return 频率信息的最佳方式的巧妙建议。

由于 tidygraphtidyverse 配合得很好,我们可以直接使用 dplyr 语法来操作元素。要生成频率(可能不是正确的术语)或一系列递减的事件,可以使用 group_by() 后跟 n()。然后我们可以依靠向量回收来为列表元素的列赋值,这取决于它的索引 .y.

freqs <- lapply(myList, function(x){
  x %>% 
     pull(name) %>%
     replace_na("..") %>%
     paste0(collapse = "")
}) %>%
  unlist(use.names = F) %>%
  as_tibble() %>%
  group_by(value) %>%
  mutate(val = n():1) %>%
  pull(val)

purrr::imap(l, ~.x %>% 
              mutate(frequency = freqs[.y]) %>% 
              select(name, frequency))
[[1]]
# Node Data: 3 x 2 (active)
  name  frequency
1 x4            3
2 NA            3
3 NA            3

# Edge Data: 2 x 2
   from    to
  <int> <int>
1     1     2
2     1     3

[[2]]
# Node Data: 3 x 2 (active)
  name  frequency
  <chr>     <int>
1 x4            2
2 NA            2
3 NA            2

# Edge Data: 2 x 2
   from    to
  <int> <int>
1     1     2
2     1     3

[[3]]
# Node Data: 3 x 2 (active)
  name  frequency
  <chr>     <int>
1 x4            1
2 NA            1
3 NA            1