从稀疏表构建网络边缘表

3

我不太清楚如何解释,但是...

我有一张稀疏表格,其中每组代表一个级别。列是有序的,也就是说,下游(左侧)列代表子节点,上游(右侧)列代表父节点。
我想要一个两列表格,第一列是父节点,第二列是子节点。如果可能的话,第三列是父节点的长度(最终节点数的总和)。

请看以下示例:

>tt <- tibble(
  ID  = letters[1:8],
  `1` = c( 1, 1, 1, 1, 2, 2, 2, 2),
  `2` = c( 3, 3, 4, 4, 5, 5, 5, 6),
  `3` = c( 7, 7, 8, 9,10,10,11,12)
)
> tt
# A tibble: 8 x 4
  ID      `1`   `2`   `3`
  <chr> <dbl> <dbl> <dbl>
1 a         1     3     7
2 b         1     3     7
3 c         1     4     8
4 d         1     4     9
5 e         2     5    10
6 f         2     5    10
7 g         2     5    11
8 h         2     6    12

>dput(tt)
structure(list(ID = c("a", "b", "c", "d", "e", "f", "g", "h"), 
    `1` = c(1, 1, 1, 1, 2, 2, 2, 2), `2` = c(3, 3, 4, 4, 5, 5, 
    5, 6), `3` = c(7, 7, 8, 9, 10, 10, 11, 12)), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))

结果应该是:
>ttt <- tibble(
  parent = c(1,1,2,2,3,4,4, 5, 5, 6, 7,7,8,9,10,10,11,12),
  child  = c(3,4,5,6,7,8,9,10,11,12, letters[1:8]       ),
  length = c(4,4,4,4,2,2,2, 3, 3, 1, 2,2,1,1, 2, 2, 1, 1)
)
>ttt
# A tibble: 18 x 3
   parent child length
    <dbl> <chr>  <dbl>
 1      1 3          4
 2      1 4          4
 3      2 5          4
 4      2 6          4
 5      3 7          2
 6      4 8          2
 7      4 9          2
 8      5 10         3
 9      5 11         3
10      6 12         1
11      7 a          2
12      7 b          2
13      8 c          1
14      9 d          1
15     10 e          2
16     10 f          2
17     11 g          1
18     12 h          1
> dput(ttt)
structure(list(parent = c(1, 1, 2, 2, 3, 4, 4, 5, 5, 6, 7, 7, 
8, 9, 10, 10, 11, 12), child = c("3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12", "a", "b", "c", "d", "e", "f", "g", "h"
), length = c(4, 4, 4, 4, 2, 2, 2, 3, 3, 1, 2, 2, 1, 1, 2, 2, 
1, 1)), row.names = c(NA, -18L), class = c("tbl_df", "tbl", "data.frame"
))

非常感谢您的帮助,提供的任何帮助都会受到赞赏。

提前感谢您的帮助。

1个回答

1
这可以让你完成90%的工作:
tt_correct <- tt[, c(2,3,4,1)]

ttt <- do.call(
  rbind,
  lapply(seq_len(length(tt)-1),
       function(i){
         DF <- tt_correct[, c(i, i+1)]
         names(DF) <- c('parent', 'child')
         DF$length <- ave(DF$parent, DF$parent, FUN = length)
         unique(DF)
       }
  )
)

ttt

# A tibble: 18 x 3
   parent child length
    <dbl> <chr>  <dbl>
 1      1 3          4
 2      1 4          4
 3      2 5          4
 4      2 6          4
 5      3 7          2
 6      4 8          2
 7      4 9          2
 8      5 10         3
 9      5 11         3
10      6 12         1
11      7 a          2
12      7 b          2
13      8 c          1
14      9 d          1
15     10 e          2
16     10 f          2
17     11 g          1
18     12 h          1

第一部分是更正顺序。你期望的输出表明第1列是第4列的子级。 lapply() 语句主要沿着数据框行走并堆叠数据。
这是90%的进展,因为答案与长度的预期输出不符。我认为这是正确的,但我可能错了。
最后,我对igraph不是很擅长,但你可以通过以下方式找到其他信息:
library(igraph)
plot(graph_from_data_frame(ttt[, 1:2]))

igraph plot


它对我起作用了,只是我不明白为什么只解决了100%的90% :D - Aureliano Guedes
太棒了!我的输出与您期望的输出不匹配。具体来说,当父母是字母时长度不同。 - Cole
实际上,我的输出示例是错误的。你的是正确的。所以,我已经修正了我的错误。 - Aureliano Guedes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接