2

我有一些包含在列表中的 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_4并且在节点数据中tg_5具有相同name的列。

我试图想出一种方法来计算具有相同name列的 tidygraph 对象的频率。我希望能够返回 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

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

但是,对于返回频率信息的最佳方式,我愿意接受任何聪明的建议。

4

1 回答 1

2

由于tidygraph可以很好地使用tidyverse我们可以dplyr直接使用语法来操作元素。可以使用频率(可能不是正确的术语)或一系列递减事件,group_by()后跟 a 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
于 2021-11-17T05:36:05.200 回答