【问题标题】:R, Igraph: convert edges to tibble or data frameR,Igraph:将边缘转换为 tibble 或数据框
【发布时间】:2021-10-06 17:20:45
【问题描述】:

我一定是被一杯茶淹死了,但与此同时,我已经把头撞在墙上好几个小时了。 请看下面的reprex,其中我生成了一个igraph 图形对象g,并将源自某些顶点的边收集到一个名为dd 的对象中。 我的目标是将 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 年 7 月 31 日创建

【问题讨论】:

  • 您提供的代码没有描述基本问题和目标。
  • 我完全不同意。我想将收集到 dd 的一堆边转换为数据框或小标题,以便进一步操作。请阅读整篇文章。
  • 发布失败的代码通常对理解目标没有用处。您现在应该使用 SO 的 edit 设施来接受您评论中的材料并改进问题。

标签: r dataframe igraph tibble


【解决方案1】:

您已从图形对象中提取边缘标识符。现在您需要了解图形和边是如何表示的。所以使用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 方法作为图形对象的两列结果,然后选择图形的前 4 行:

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

【讨论】:

  • 谢谢。 as_data_frame(g)[1:4, ] 是我要找的。​​span>
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
相关资源
最近更新 更多