Extract a list of edges between community nodes and other nodes for each community
up vote
0
down vote
favorite
Suppose we have a simple weighted network on which we perform some sort of community detection. Next we extract particular community and the final task is to extract all edges between nodes of this community and all other nodes.
Below I pasted the toy code.
# Create toy graph
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
# Add weights to edges
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
# Run community detection
cl <- cluster_louvain(g)
There are 5 nodes which belong to community #1, 12 nodes which belong to community #2, etc.
> table(membership(cl))
1 2 3 4
5 12 2 15
Now we extract community #1:
g1 <- induced_subgraph(g, which(cl$membership == 1))
Question: how to find edges which connect nodes in community #1 with all other nodes (excluding edges which define community #1)?
There is an answer related to certain community below
You start by getting all edges based in your community:
all_edges <- E(g)[inc(V(g)[membership(cl) == 1])]
all_edges
+ 10/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11 5-- 7 5--11 6-- 7 6--11 6--17 7--17
Then, filter out the ones that are completely internal (both vertices are in the community):
all_edges_m <- get.edges(g, all_edges) #matrix representation
all_edges[!(
all_edges_m[, 1] %in% V(g)[membership(cl) == 1] &
all_edges_m[, 2] %in% V(g)[membership(cl) == 1]
)] # filter where in col1 and col2
+ 4/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11
But for me its necessary to get the whole list containing those nodes for each community. not only for the one. Are the any suggestions to create this loop? It would be superb if yes :)
r cluster-analysis igraph
add a comment |
up vote
0
down vote
favorite
Suppose we have a simple weighted network on which we perform some sort of community detection. Next we extract particular community and the final task is to extract all edges between nodes of this community and all other nodes.
Below I pasted the toy code.
# Create toy graph
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
# Add weights to edges
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
# Run community detection
cl <- cluster_louvain(g)
There are 5 nodes which belong to community #1, 12 nodes which belong to community #2, etc.
> table(membership(cl))
1 2 3 4
5 12 2 15
Now we extract community #1:
g1 <- induced_subgraph(g, which(cl$membership == 1))
Question: how to find edges which connect nodes in community #1 with all other nodes (excluding edges which define community #1)?
There is an answer related to certain community below
You start by getting all edges based in your community:
all_edges <- E(g)[inc(V(g)[membership(cl) == 1])]
all_edges
+ 10/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11 5-- 7 5--11 6-- 7 6--11 6--17 7--17
Then, filter out the ones that are completely internal (both vertices are in the community):
all_edges_m <- get.edges(g, all_edges) #matrix representation
all_edges[!(
all_edges_m[, 1] %in% V(g)[membership(cl) == 1] &
all_edges_m[, 2] %in% V(g)[membership(cl) == 1]
)] # filter where in col1 and col2
+ 4/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11
But for me its necessary to get the whole list containing those nodes for each community. not only for the one. Are the any suggestions to create this loop? It would be superb if yes :)
r cluster-analysis igraph
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Suppose we have a simple weighted network on which we perform some sort of community detection. Next we extract particular community and the final task is to extract all edges between nodes of this community and all other nodes.
Below I pasted the toy code.
# Create toy graph
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
# Add weights to edges
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
# Run community detection
cl <- cluster_louvain(g)
There are 5 nodes which belong to community #1, 12 nodes which belong to community #2, etc.
> table(membership(cl))
1 2 3 4
5 12 2 15
Now we extract community #1:
g1 <- induced_subgraph(g, which(cl$membership == 1))
Question: how to find edges which connect nodes in community #1 with all other nodes (excluding edges which define community #1)?
There is an answer related to certain community below
You start by getting all edges based in your community:
all_edges <- E(g)[inc(V(g)[membership(cl) == 1])]
all_edges
+ 10/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11 5-- 7 5--11 6-- 7 6--11 6--17 7--17
Then, filter out the ones that are completely internal (both vertices are in the community):
all_edges_m <- get.edges(g, all_edges) #matrix representation
all_edges[!(
all_edges_m[, 1] %in% V(g)[membership(cl) == 1] &
all_edges_m[, 2] %in% V(g)[membership(cl) == 1]
)] # filter where in col1 and col2
+ 4/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11
But for me its necessary to get the whole list containing those nodes for each community. not only for the one. Are the any suggestions to create this loop? It would be superb if yes :)
r cluster-analysis igraph
Suppose we have a simple weighted network on which we perform some sort of community detection. Next we extract particular community and the final task is to extract all edges between nodes of this community and all other nodes.
Below I pasted the toy code.
# Create toy graph
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
# Add weights to edges
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
# Run community detection
cl <- cluster_louvain(g)
There are 5 nodes which belong to community #1, 12 nodes which belong to community #2, etc.
> table(membership(cl))
1 2 3 4
5 12 2 15
Now we extract community #1:
g1 <- induced_subgraph(g, which(cl$membership == 1))
Question: how to find edges which connect nodes in community #1 with all other nodes (excluding edges which define community #1)?
There is an answer related to certain community below
You start by getting all edges based in your community:
all_edges <- E(g)[inc(V(g)[membership(cl) == 1])]
all_edges
+ 10/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11 5-- 7 5--11 6-- 7 6--11 6--17 7--17
Then, filter out the ones that are completely internal (both vertices are in the community):
all_edges_m <- get.edges(g, all_edges) #matrix representation
all_edges[!(
all_edges_m[, 1] %in% V(g)[membership(cl) == 1] &
all_edges_m[, 2] %in% V(g)[membership(cl) == 1]
)] # filter where in col1 and col2
+ 4/78 edges:
[1] 1-- 5 1-- 6 1-- 7 1--11
But for me its necessary to get the whole list containing those nodes for each community. not only for the one. Are the any suggestions to create this loop? It would be superb if yes :)
r cluster-analysis igraph
r cluster-analysis igraph
asked Nov 19 at 13:30
Maksym Moroz
249
249
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Here is my take on your problem:
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
cl <- cluster_louvain(g)
add membership as a vertex attribute
V(g)$name <- membership(cl)
get edgelist
x <- as_edgelist(g, names = T)
here are all edges that connect vertices of different communities
V(g)$name <- 1:vcount(g)
E(g)[x[,1] != x[,2]]
optional check
E(g)$color <- ifelse(x[,1] != x[,2], "red", "blue")
plot(g, edge.color = E(g)$color)
plot(cl, g)
1
thank you for your approach !
– Maksym Moroz
Nov 19 at 15:29
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Here is my take on your problem:
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
cl <- cluster_louvain(g)
add membership as a vertex attribute
V(g)$name <- membership(cl)
get edgelist
x <- as_edgelist(g, names = T)
here are all edges that connect vertices of different communities
V(g)$name <- 1:vcount(g)
E(g)[x[,1] != x[,2]]
optional check
E(g)$color <- ifelse(x[,1] != x[,2], "red", "blue")
plot(g, edge.color = E(g)$color)
plot(cl, g)
1
thank you for your approach !
– Maksym Moroz
Nov 19 at 15:29
add a comment |
up vote
0
down vote
accepted
Here is my take on your problem:
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
cl <- cluster_louvain(g)
add membership as a vertex attribute
V(g)$name <- membership(cl)
get edgelist
x <- as_edgelist(g, names = T)
here are all edges that connect vertices of different communities
V(g)$name <- 1:vcount(g)
E(g)[x[,1] != x[,2]]
optional check
E(g)$color <- ifelse(x[,1] != x[,2], "red", "blue")
plot(g, edge.color = E(g)$color)
plot(cl, g)
1
thank you for your approach !
– Maksym Moroz
Nov 19 at 15:29
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Here is my take on your problem:
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
cl <- cluster_louvain(g)
add membership as a vertex attribute
V(g)$name <- membership(cl)
get edgelist
x <- as_edgelist(g, names = T)
here are all edges that connect vertices of different communities
V(g)$name <- 1:vcount(g)
E(g)[x[,1] != x[,2]]
optional check
E(g)$color <- ifelse(x[,1] != x[,2], "red", "blue")
plot(g, edge.color = E(g)$color)
plot(cl, g)
Here is my take on your problem:
library(igraph)
set.seed(12345)
g <- make_graph("Zachary")
E(g)$weight <- sample(x = 1:10, size = ecount(g), replace = TRUE)
cl <- cluster_louvain(g)
add membership as a vertex attribute
V(g)$name <- membership(cl)
get edgelist
x <- as_edgelist(g, names = T)
here are all edges that connect vertices of different communities
V(g)$name <- 1:vcount(g)
E(g)[x[,1] != x[,2]]
optional check
E(g)$color <- ifelse(x[,1] != x[,2], "red", "blue")
plot(g, edge.color = E(g)$color)
plot(cl, g)
edited Nov 19 at 14:58
answered Nov 19 at 14:44
Ben Nutzer
9316
9316
1
thank you for your approach !
– Maksym Moroz
Nov 19 at 15:29
add a comment |
1
thank you for your approach !
– Maksym Moroz
Nov 19 at 15:29
1
1
thank you for your approach !
– Maksym Moroz
Nov 19 at 15:29
thank you for your approach !
– Maksym Moroz
Nov 19 at 15:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375719%2fextract-a-list-of-edges-between-community-nodes-and-other-nodes-for-each-communi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown