Consider a data frame that correlated each variable from with each other:
iris_cor <- structure(list(x = c("Sepal.Length", "Sepal.Length", "Sepal.Length",
"Sepal.Length", "Sepal.Width", "Sepal.Width", "Sepal.Width",
"Sepal.Width", "Petal.Length", "Petal.Length", "Petal.Length",
"Petal.Length", "Petal.Width", "Petal.Width", "Petal.Width",
"Petal.Width"), y = c("Sepal.Length", "Sepal.Width", "Petal.Length",
"Petal.Width", "Sepal.Length", "Sepal.Width", "Petal.Length",
"Petal.Width", "Sepal.Length", "Sepal.Width", "Petal.Length",
"Petal.Width", "Sepal.Length", "Sepal.Width", "Petal.Length",
"Petal.Width"), r = c(NA, -0.117569784133002, 0.871753775886583,
0.817941126271576, -0.117569784133002, NA, -0.42844010433054,
-0.366125932536439, 0.871753775886583, -0.42844010433054, NA,
0.962865431402796, 0.817941126271576, -0.366125932536439, 0.962865431402796,
NA)), row.names = c(NA, -16L), class = c("tbl_df", "tbl", "data.frame"
), .Names = c("x", "y", "r"))
If I wanted to make a network plot of this I would do the following:
library(igraph)
iris_igraph <- graph_from_data_frame(iris_cor)
Now if I were only interested in the correlations that involved with Sepal.Length
I am unclear on how to efficiently extract that information. Trying both of these approaches is not successful. The first because of the error and the second because I only get the first instance of Sepal.Length
not all of them:
> subgraph.edges(iris_igraph, E(iris_igraph)[name == "Sepal.Length"])
Error in eval(x$expr, data, x$env) : object 'name' not found
>
> subgraph.edges(iris_igraph, V(iris_igraph)[name == "Sepal.Length"])
IGRAPH dc7408b DN-- 1 1 --
+ attr: name (v/c), r (e/n)
+ edge from dc7408b (vertex names):
[1] Sepal.Length->Sepal.Length
This results in the desired output:
subgraph.edges(iris_igraph, E(iris_igraph)[1:4])
However a numbered index is not practical when the desired subsets of the igraph object aren't in order or there are many nodes/edges.
Can anyone recommend a way to subset an igraph object for the name of the edge?
According to indexing edge sequences , you can use the special function inc
for edges subsetting, which:
takes a vertex sequence, and selects all edges that have at least one incident vertex in the vertex sequence.
E(iris_igraph)[inc('Sepal.Length')]
#+ 7/16 edges (vertex names):
#[1] Sepal.Length->Sepal.Length Sepal.Length->Sepal.Width Sepal.Length->Petal.Length
#[4] Sepal.Length->Petal.Width Sepal.Width ->Sepal.Length Petal.Length->Sepal.Length
#[7] Petal.Width ->Sepal.Length
Select edges from Sepal.Length
:
E(iris_igraph)[from('Sepal.Length')]
#+ 4/16 edges (vertex names):
#[1] Sepal.Length->Sepal.Length Sepal.Length->Sepal.Width Sepal.Length->Petal.Length
#[4] Sepal.Length->Petal.Width
Select edges to Sepal.Length
:
E(iris_igraph)[to('Sepal.Length')]
#+ 4/16 edges (vertex names):
#[1] Sepal.Length->Sepal.Length Sepal.Width ->Sepal.Length Petal.Length->Sepal.Length
#[4] Petal.Width ->Sepal.Length
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.