Turning igraph.vs into dataframe

So, I am using all_shortest_paths to get an output that 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 axis names
I've tried

PathsDF<-as.data.frame(PathsE)

      

but I am getting this error:

Error in as.data.frame.default (x [[i]], optional = TRUE): cannot force class "igraph.vs" "to data.frame

I also tried this:

PathDF <- as.data.frame(get.edgelist(PathsE))

      

but i am getting this error

Error in get.edgelist (PathsE): not a graph object

When I loop through a row of data with

class(PathsEF)

      

he says it's a list. But when I use

str(PathsE)

      

it looks like this:

..$ :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.

From this information, any of you have ideas on how to convert this to a dataframe. I'm sorry if I'm missing anything obvious - I'm pretty new to R!

+3


source to share


2 answers


First, a couple of clarifying points. The object created all_shortest_paths

is a list with two elements: 1) res

and 2) nrgeo

. An object is res

also a list, but a list of objects igraph.vs

. An object igraph.vs

is a igraph

specific object known as a vertex sequence. Basic R functions won't know what to do with it. Therefore, we use a function as_id

to convert an object igraph.vs

to an ID vector.

Since it PathsE$res

is a list of objects igraph.vs

, you need to iterate over the list and collapse it into a data frame. There are several ways to do this. Here is one of them:

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 creates a matrix, but note that this is a transposition 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, transfer it and transform it into a dataframe:

> 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

      

What can we do in one 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)))

      

+2


source


Actually, it's simple:

data.frame <- get.data.frame(g, what= c("vertices", "edges", "both") )

      

Keep in mind the choice among the "what" options.



You can also save it in .csv format using this script:

write.csv(data.frame, "data.frame.csv")

      

0


source







All Articles