Extract a list of edges between community nodes and other nodes for each community
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 |
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 |
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 '18 at 13:30
Maksym MorozMaksym Moroz
4610
4610
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '18 at 15:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 at 15:29
add a comment |
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 '18 at 15:29
add a comment |
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 '18 at 14:58
answered Nov 19 '18 at 14:44
Ben NutzerBen Nutzer
9818
9818
1
thank you for your approach !
– Maksym Moroz
Nov 19 '18 at 15:29
add a comment |
1
thank you for your approach !
– Maksym Moroz
Nov 19 '18 at 15:29
1
1
thank you for your approach !
– Maksym Moroz
Nov 19 '18 at 15:29
thank you for your approach !
– Maksym Moroz
Nov 19 '18 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.
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