将igraph.vs转换为数据框 [英] Turning an igraph.vs into a data frame
问题描述
所以我正在使用all_shortest_paths来获取输出,如下所示:
So I am using all_shortest_paths to get an output, which looks like this:
PathsE
$res[[1]]
+ 4/990 vertices, named:
[1] Sortilin GGA1 Ubiquitin PIMT
$res[[2]]
+ 4/990 vertices, named:
[1] Sortilin TrkA PLK1 PIMT
$res[[3]]
+ 4/990 vertices, named:
[1] Sortilin APP JAB1 PIMT
我想将其转换为数据框,以便进行操作它。
作为参考,我希望数据帧看起来像这样:
I would like to turn this into a dataframe so that I can manipulate it. For reference, I would like the dataframe to look like this:
Prot1 Prot2 Prot3 Prot4
Pathway1 Sortilin GGA1 PLK1 PIMT
Pathway2 Sortilin TrkA PLK1 PIMT
Pathway3 Sortilin APP JAB1 PIMT
*我知道如何更改轴的名称
我尝试过
*I know how to change the axes names
I have tried
PathsDF<-as.data.frame(PathsE)
但我收到此错误:
as.data.frame.default(x [[i]],optional = TRUE)中的错误:
无法将类 igraph.vs强制转换为data.frame
Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""igraph.vs"" to a data.frame
我也尝试过:
PathDF <- as.data.frame(get.edgelist(PathsE))
但出现此错误
get.edgelist(PathsE)错误:不是图形对象
Error in get.edgelist(PathsE) : Not a graph object
当我使用
class(PathsEF)
它说这是一个列表。但是当我使用
it says it is a list. But when I use
str(PathsE)
它看起来像这样:
..$ :Class 'igraph.vs' atomic [1:4] 338 204 40 913
.. .. ..- attr(*, "env")=<weakref>
.. .. ..- attr(*, "graph")= chr "717e99fb-b7db-4e35-8fd3-1d8d741e6612"
etc
对我来说就像一个矩阵。
which looks like a matrix to me.
从此信息中,您是否对如何将其转换为数据框有任何想法。很抱歉,如果我缺少任何明显的东西,我对R还是很陌生!
From this information, do any of you have any ideas about how to convert this into a dataframe. I am sorry if I am missing anything obvious- I am pretty new to R!
推荐答案
首先,有几点要澄清。 all_shortest_paths
创建的对象是一个包含两个元素的列表:1) res
和2) nrgeo
。 res
对象也是一个列表,但是是 igraph.vs
对象的列表。 igraph.vs
对象是特定于 igraph
的对象,称为顶点序列。 Base R函数将不知道如何处理。因此,我们使用 as_id
函数将 igraph.vs
对象转换为ID向量。
First, a couple of clarifying points. The object created by all_shortest_paths
is a list with two elements: 1) res
and 2) nrgeo
. The res
object is also a list--but a list of igraph.vs
objects. The igraph.vs
object is an igraph
specific object known as a vertex sequence. Base R functions won't know what to do with it. So we use the as_id
function to convert an igraph.vs
object to a vector of ids.
由于 PathsE $ res
是 igraph.vs
对象的列表,因此您需要遍历列表并将其折叠到数据框中。有几种方法可以做到这一点。这是一个:
Since PathsE$res
is a list of igraph.vs
objects, you need to iterate over the list and collapse it into a data frame. There are several ways to do this. Here is one:
set.seed(6857)
g <- sample_smallworld(1, 100, 5, 0.05) #Building a random graph
sp <- all_shortest_paths(g, 5, 70)
mat <- sapply(sp$res, as_ids)
#sapply iterates the function as_ids over all elements in the list sp$res and collapses it into a matrix
这会产生一个矩阵,但请注意
This produces a matrix, but notice that it is the transpose of what you want:
> mat
[,1] [,2] [,3] [,4]
[1,] 5 5 5 5
[2,] 100 4 100 1
[3,] 95 65 65 75
[4,] 70 70 70 70
转置它并转换为数据框:
So, transpose it and convert to a data frame:
> df <- as.data.frame(t(mat))
V1 V2 V3 V4
1 5 100 95 70
2 5 4 65 70
3 5 100 65 70
4 5 1 75 70
我们可以在一行代码中进行以下操作:
Which we can do in a single line of code:
set.seed(6857)
g <- sample_smallworld(1, 100, 5, 0.05)
sp <- all_shortest_paths(g, 5, 70)
df <- as.dataframe(t(sapply(sp$res, as_ids)))
这篇关于将igraph.vs转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!