R 从稀疏表构建网络边缘表

R 从稀疏表构建网络边缘表,r,networking,reshape,tidyverse,R,Networking,Reshape,Tidyverse,我不知道该怎么解释,但是 我有一个稀疏的表,每个组代表一个级别。列是有序的,这意味着下游(左)列表示子节点,上游(右)节点表示父节点。 我想要一个两列表,其中第一列是父节点,第二列是子节点。如果可能,第三列包含父节点的长度(最终节点数的总和) 请遵循以下示例: >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

我不知道该怎么解释,但是

我有一个稀疏的表,每个组代表一个级别。列是有序的,这意味着下游(左)列表示子节点,上游(右)节点表示父节点。
我想要一个两列表,其中第一列是父节点,第二列是子节点。如果可能,第三列包含父节点的长度(最终节点数的总和)

请遵循以下示例:

>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"))
>tt
#一个tibble:8x4
ID`1``2``3`
1A137
2 b 1 3 7
3 c 1 4 8
4d149
5 e 2 5 10
6 f 2 5 10
7 g 2 5 11
8H2612
>dput(tt)
结构(列表ID=c(“a”、“b”、“c”、“d”、“e”、“f”、“g”、“h”),
`1`=c(1,1,1,1,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),类=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"
))
>ttt ttt
#一个tibble:18x3
父子长度
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小时1
>dput(ttt)
结构(列表)(父项=c(1,1,2,2,3,4,4,5,5,6,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”
),长度=c(4,4,4,4,2,2,2,3,3,1,2,2,1,1,1,2,2,
1,1),row.names=c(NA,-18L),class=c(“tbl_-df”,“tbl”,“data.frame”
))
感谢您的帮助。
提前谢谢

这可以让您实现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

这对我很有效,我只是不明白为什么只有90%,如果它解决了100%:Dlol,太好了!我的输出与您的预期输出不匹配。也就是说,当父母是字母时的长度。实际上,我的输出示例是错误的。你说得对。所以,我修好了我的。
library(igraph)
plot(graph_from_data_frame(ttt[, 1:2]))