R、Igraph:将边转换为小标题或数据框

R, Igraph: convert edges to tibble or data frame

我一定是沉浸在一杯茶中,但与此同时我已经用头撞墙好几个小时了。 请看下面的 reprex,我在其中生成了一个 igraph 图 object g,并将源自某些顶点的边收集到一个名为 dd 的 object 中。 我的目标是将 dd 转换为数据帧或 tibble,但最明显的方法(as_data_frame 和 as_tibble)完全失败。 谁能帮帮我?

非常感谢

library(tidyverse)
library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:dplyr':
#> 
#>     as_data_frame, groups, union
#> The following objects are masked from 'package:purrr':
#> 
#>     compose, simplify
#> The following object is masked from 'package:tidyr':
#> 
#>     crossing
#> The following object is masked from 'package:tibble':
#> 
#>     as_data_frame
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union

g <- make_ring(10)


dd <- E(g)[from(1:4)]

## this does not work

dd%>%as_tibble
#> Error in as.data.frame.default(value, stringsAsFactors = FALSE): cannot coerce class '"igraph.es"' to a data.frame

## neither this

dd%>%as_data_frame
#> Error in as_data_frame(.): Not a graph object

## so what to do?

reprex package (v2.0.0)

于 2021-07-31 创建

您已从图形对象中提取边标识符。现在您需要了解图形和边的表示方式。因此,使用 plotstr 命令检查 gdd 对象的详细信息:

> g <- make_ring(10)
> plot(g)
> dd <- E(g)[from(1:4)]
> dd
+ 5/10 edges from 96250aa:
[1] 1-- 2 2-- 3 3-- 4 4-- 5 1--10
> str(dd)
 'igraph.es' int [1:5] 1 2 3 4 10   # so it's really just a numeric vector
 - attr(*, "env")=<weakref> 
 - attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
> help(pack=igraph)
> as_edgelist(g)
      [,1] [,2]     # whereas the original graph (list) converts to two columns
 [1,]    1    2
 [2,]    2    3
 [3,]    3    4
 [4,]    4    5
 [5,]    5    6
 [6,]    6    7
 [7,]    7    8
 [8,]    8    9
 [9,]    9   10
[10,]    1   10
> as_edgelist(dd)
Error in as_edgelist(dd) : Not a graph object

查看结果后,我打开了 igraph 帮助索引并注意到名为 as_ids 的函数被描述为:“将顶点或边序列转换为普通向量”

> as_ids(dd)
[1]  1  2  3  4 10
> str(g)
List of 10
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 2 10
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 1 3
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 2 4
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 3 5
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 4 6
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 5 7
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 6 8
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 7 9
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 8 10
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 $ :List of 1
  ..$ : 'igraph.vs' int [1:2] 1 9
  .. ..- attr(*, "env")=<weakref> 
  .. ..- attr(*, "graph")= chr "96250aab-d2ab-43aa-882d-6ef695323b0a"
 - attr(*, "class")= chr "igraph"

所以如果你想要一个 data.frame 来保存边缘标识符,你可以得到一个只有一列的数据框,只需这样做:

 dd_df <- data.frame( ids <- as_ids(dd) )

但是,如果您想要一个两列结果,该结果使用 as_data_frame 方法作为图形对象,然后 select 图形的前 4 行:

> as_data_frame(g)[1:4, ]
  from to
1    1  2
2    2  3
3    3  4
4    4  5