Merging several vectors of nodes into an edgelist and convert them to an adjacency matrix



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I want to map a number of relationship circles (consisting of a list of ids) into a large adjacency matrix. My data looks like this



circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

d_mat = matrix(ncol = 2)

for (i in 1:5)
#extract id from list
dat = get(paste("circle", i, sep="_"))
#convert to edgelist, each pair is unique
dat_t = t(combn(dat, 2))
#rbind edge list together
edge_list <- rbind(d_mat, dat_t)



However, the output edge_list only returns the edge list of from the last iteration (circle_5) with the preceding three being overwritten.



Furthermore, suppose these five circles were drawn from a group of 50 persons, how can I map the values of such edge list to the corresponding cells of a 50 by 50 adjacency matrix? (I suppose make_graph and as_adjacency_matrix function in igraph should do the tricks, but I don't know how at the moment)



Also, for overlapping membership, such (1, 3) in circle_1 and circle_5, meaning that 1 and 3 are linked twice in this 50-person network. How can I aggregate this count frequency and convert the adjacency matrix into a weighted matrix?










share|improve this question
























  • Wow, that's very slick answer. Thanks a lot!

    – Chris T.
    Nov 16 '18 at 12:04

















3















I want to map a number of relationship circles (consisting of a list of ids) into a large adjacency matrix. My data looks like this



circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

d_mat = matrix(ncol = 2)

for (i in 1:5)
#extract id from list
dat = get(paste("circle", i, sep="_"))
#convert to edgelist, each pair is unique
dat_t = t(combn(dat, 2))
#rbind edge list together
edge_list <- rbind(d_mat, dat_t)



However, the output edge_list only returns the edge list of from the last iteration (circle_5) with the preceding three being overwritten.



Furthermore, suppose these five circles were drawn from a group of 50 persons, how can I map the values of such edge list to the corresponding cells of a 50 by 50 adjacency matrix? (I suppose make_graph and as_adjacency_matrix function in igraph should do the tricks, but I don't know how at the moment)



Also, for overlapping membership, such (1, 3) in circle_1 and circle_5, meaning that 1 and 3 are linked twice in this 50-person network. How can I aggregate this count frequency and convert the adjacency matrix into a weighted matrix?










share|improve this question
























  • Wow, that's very slick answer. Thanks a lot!

    – Chris T.
    Nov 16 '18 at 12:04













3












3








3








I want to map a number of relationship circles (consisting of a list of ids) into a large adjacency matrix. My data looks like this



circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

d_mat = matrix(ncol = 2)

for (i in 1:5)
#extract id from list
dat = get(paste("circle", i, sep="_"))
#convert to edgelist, each pair is unique
dat_t = t(combn(dat, 2))
#rbind edge list together
edge_list <- rbind(d_mat, dat_t)



However, the output edge_list only returns the edge list of from the last iteration (circle_5) with the preceding three being overwritten.



Furthermore, suppose these five circles were drawn from a group of 50 persons, how can I map the values of such edge list to the corresponding cells of a 50 by 50 adjacency matrix? (I suppose make_graph and as_adjacency_matrix function in igraph should do the tricks, but I don't know how at the moment)



Also, for overlapping membership, such (1, 3) in circle_1 and circle_5, meaning that 1 and 3 are linked twice in this 50-person network. How can I aggregate this count frequency and convert the adjacency matrix into a weighted matrix?










share|improve this question
















I want to map a number of relationship circles (consisting of a list of ids) into a large adjacency matrix. My data looks like this



circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

d_mat = matrix(ncol = 2)

for (i in 1:5)
#extract id from list
dat = get(paste("circle", i, sep="_"))
#convert to edgelist, each pair is unique
dat_t = t(combn(dat, 2))
#rbind edge list together
edge_list <- rbind(d_mat, dat_t)



However, the output edge_list only returns the edge list of from the last iteration (circle_5) with the preceding three being overwritten.



Furthermore, suppose these five circles were drawn from a group of 50 persons, how can I map the values of such edge list to the corresponding cells of a 50 by 50 adjacency matrix? (I suppose make_graph and as_adjacency_matrix function in igraph should do the tricks, but I don't know how at the moment)



Also, for overlapping membership, such (1, 3) in circle_1 and circle_5, meaning that 1 and 3 are linked twice in this 50-person network. How can I aggregate this count frequency and convert the adjacency matrix into a weighted matrix?







r igraph adjacency-matrix adjacency-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 15:52









Henrik

42.4k994110




42.4k994110










asked Nov 16 '18 at 11:18









Chris T.Chris T.

370419




370419












  • Wow, that's very slick answer. Thanks a lot!

    – Chris T.
    Nov 16 '18 at 12:04

















  • Wow, that's very slick answer. Thanks a lot!

    – Chris T.
    Nov 16 '18 at 12:04
















Wow, that's very slick answer. Thanks a lot!

– Chris T.
Nov 16 '18 at 12:04





Wow, that's very slick answer. Thanks a lot!

– Chris T.
Nov 16 '18 at 12:04












2 Answers
2






active

oldest

votes


















2














You can do it as you've started by combining the edge lists and then just create the matrix directly.





circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

# lets put all the circles in a list for convenience
circles <- list(circle_1, circle_2, circle_3,
circle_4, circle_5)

# we will lapply along the list, get the complete set of
# edges with combn, and then rbind all the resulting
# structures together
edge_list <- do.call(rbind, lapply(circles, function(circ)t(combn(circ, 2))))

# we convert to a data.frame and set the factor levels
# such that R knows these are nodes from a set of 50 nodes
edge_list <- data.frame(from = factor(edge_list[,1], levels=1:50),
to = factor(edge_list[,2], levels=1:50))
# take a look
head(edge_list)
#> from to
#> 1 1 3
#> 2 1 5
#> 3 3 5
#> 4 17 22
#> 5 17 35
#> 6 17 49
# we can just use table to make the adjacency matrix. R will create
# a row/column for each level of the factor. We look at the first
# 6x6 entries
table(edge_list)[1:6,1:6] # luckily entry (1,3) = 2 as we hoped
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 2 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


This adjacency matrix is upper triangular. If you want the adjacency matrix to be symmetrical as reflecting an undirected graph you can set the upper and lower triangles of the matrix to be equal using adj.mat[lower.tri(adj.mat)] <- adj.mat[upper.tri(adj.mat)].



If you want the matrix to be just binary (and not record multiple links in different circles) just run it through an ifelse statement:



# to convert to purely binary
adj.mat <- table(edge_list)
adj.mat.bin <- ifelse(adj.mat>1, 1, adj.mat)
adj.mat.bin[1:6,1:6]
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 1 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


Created on 2018-11-16 by the reprex package (v0.2.1)






share|improve this answer























  • Good to know that functions like lower.tri and upper.tri exist, thanks for clarifying this, really appreciated!

    – Chris T.
    Nov 16 '18 at 12:24


















1














I borrow the list of vertices ('circles') from @gfgm and use igraph functions.



Loop over the list of vertices with lapply. For each set of vertices, create a full graph (make_full_graph), with number of vertices n equal to length of the vector. Set the name of the vertices (V(g)$name). Convert to 'edge list' (as_edgelist). rbind the resulting matrices.



library(igraph)
m <- do.call(rbind, lapply(circles, function(vert)
g <- make_full_graph(n = length(vert))
V(g)$name <- vert
as_edgelist(g)
))


Set factor levels of 'from' and 'to' vertices, and use table (similar to @gfgm)



tt <- table(factor(m[ , 1], levels = 1:50),
factor(m[ , 2], levels = 1:50))

t[1:8, 1:16]
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 1 0 0 2 0 1 0 0 1 0 0 0 0 0 0 0 1
# 2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# 3 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1
# 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1





share|improve this answer























  • I originally wanted to do this using igraph functions, but having some problems with filling cells in matrix (rbind doesn't seem to work in my original code). Thanks for sharing.

    – Chris T.
    Nov 17 '18 at 16:45






  • 1





    Your code works fine on my data, thanks again for helping on this!

    – Chris T.
    Nov 17 '18 at 17:12












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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336813%2fmerging-several-vectors-of-nodes-into-an-edgelist-and-convert-them-to-an-adjacen%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You can do it as you've started by combining the edge lists and then just create the matrix directly.





circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

# lets put all the circles in a list for convenience
circles <- list(circle_1, circle_2, circle_3,
circle_4, circle_5)

# we will lapply along the list, get the complete set of
# edges with combn, and then rbind all the resulting
# structures together
edge_list <- do.call(rbind, lapply(circles, function(circ)t(combn(circ, 2))))

# we convert to a data.frame and set the factor levels
# such that R knows these are nodes from a set of 50 nodes
edge_list <- data.frame(from = factor(edge_list[,1], levels=1:50),
to = factor(edge_list[,2], levels=1:50))
# take a look
head(edge_list)
#> from to
#> 1 1 3
#> 2 1 5
#> 3 3 5
#> 4 17 22
#> 5 17 35
#> 6 17 49
# we can just use table to make the adjacency matrix. R will create
# a row/column for each level of the factor. We look at the first
# 6x6 entries
table(edge_list)[1:6,1:6] # luckily entry (1,3) = 2 as we hoped
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 2 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


This adjacency matrix is upper triangular. If you want the adjacency matrix to be symmetrical as reflecting an undirected graph you can set the upper and lower triangles of the matrix to be equal using adj.mat[lower.tri(adj.mat)] <- adj.mat[upper.tri(adj.mat)].



If you want the matrix to be just binary (and not record multiple links in different circles) just run it through an ifelse statement:



# to convert to purely binary
adj.mat <- table(edge_list)
adj.mat.bin <- ifelse(adj.mat>1, 1, adj.mat)
adj.mat.bin[1:6,1:6]
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 1 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


Created on 2018-11-16 by the reprex package (v0.2.1)






share|improve this answer























  • Good to know that functions like lower.tri and upper.tri exist, thanks for clarifying this, really appreciated!

    – Chris T.
    Nov 16 '18 at 12:24















2














You can do it as you've started by combining the edge lists and then just create the matrix directly.





circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

# lets put all the circles in a list for convenience
circles <- list(circle_1, circle_2, circle_3,
circle_4, circle_5)

# we will lapply along the list, get the complete set of
# edges with combn, and then rbind all the resulting
# structures together
edge_list <- do.call(rbind, lapply(circles, function(circ)t(combn(circ, 2))))

# we convert to a data.frame and set the factor levels
# such that R knows these are nodes from a set of 50 nodes
edge_list <- data.frame(from = factor(edge_list[,1], levels=1:50),
to = factor(edge_list[,2], levels=1:50))
# take a look
head(edge_list)
#> from to
#> 1 1 3
#> 2 1 5
#> 3 3 5
#> 4 17 22
#> 5 17 35
#> 6 17 49
# we can just use table to make the adjacency matrix. R will create
# a row/column for each level of the factor. We look at the first
# 6x6 entries
table(edge_list)[1:6,1:6] # luckily entry (1,3) = 2 as we hoped
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 2 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


This adjacency matrix is upper triangular. If you want the adjacency matrix to be symmetrical as reflecting an undirected graph you can set the upper and lower triangles of the matrix to be equal using adj.mat[lower.tri(adj.mat)] <- adj.mat[upper.tri(adj.mat)].



If you want the matrix to be just binary (and not record multiple links in different circles) just run it through an ifelse statement:



# to convert to purely binary
adj.mat <- table(edge_list)
adj.mat.bin <- ifelse(adj.mat>1, 1, adj.mat)
adj.mat.bin[1:6,1:6]
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 1 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


Created on 2018-11-16 by the reprex package (v0.2.1)






share|improve this answer























  • Good to know that functions like lower.tri and upper.tri exist, thanks for clarifying this, really appreciated!

    – Chris T.
    Nov 16 '18 at 12:24













2












2








2







You can do it as you've started by combining the edge lists and then just create the matrix directly.





circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

# lets put all the circles in a list for convenience
circles <- list(circle_1, circle_2, circle_3,
circle_4, circle_5)

# we will lapply along the list, get the complete set of
# edges with combn, and then rbind all the resulting
# structures together
edge_list <- do.call(rbind, lapply(circles, function(circ)t(combn(circ, 2))))

# we convert to a data.frame and set the factor levels
# such that R knows these are nodes from a set of 50 nodes
edge_list <- data.frame(from = factor(edge_list[,1], levels=1:50),
to = factor(edge_list[,2], levels=1:50))
# take a look
head(edge_list)
#> from to
#> 1 1 3
#> 2 1 5
#> 3 3 5
#> 4 17 22
#> 5 17 35
#> 6 17 49
# we can just use table to make the adjacency matrix. R will create
# a row/column for each level of the factor. We look at the first
# 6x6 entries
table(edge_list)[1:6,1:6] # luckily entry (1,3) = 2 as we hoped
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 2 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


This adjacency matrix is upper triangular. If you want the adjacency matrix to be symmetrical as reflecting an undirected graph you can set the upper and lower triangles of the matrix to be equal using adj.mat[lower.tri(adj.mat)] <- adj.mat[upper.tri(adj.mat)].



If you want the matrix to be just binary (and not record multiple links in different circles) just run it through an ifelse statement:



# to convert to purely binary
adj.mat <- table(edge_list)
adj.mat.bin <- ifelse(adj.mat>1, 1, adj.mat)
adj.mat.bin[1:6,1:6]
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 1 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


Created on 2018-11-16 by the reprex package (v0.2.1)






share|improve this answer













You can do it as you've started by combining the edge lists and then just create the matrix directly.





circle_1 = c(1, 3, 5)
circle_2 = c(17, 22, 35, 49)
circle_3 = c(2, 9)
circle_4 = c(12, 28, 33)
circle_5 = c(1, 3, 8, 16, 40)

# lets put all the circles in a list for convenience
circles <- list(circle_1, circle_2, circle_3,
circle_4, circle_5)

# we will lapply along the list, get the complete set of
# edges with combn, and then rbind all the resulting
# structures together
edge_list <- do.call(rbind, lapply(circles, function(circ)t(combn(circ, 2))))

# we convert to a data.frame and set the factor levels
# such that R knows these are nodes from a set of 50 nodes
edge_list <- data.frame(from = factor(edge_list[,1], levels=1:50),
to = factor(edge_list[,2], levels=1:50))
# take a look
head(edge_list)
#> from to
#> 1 1 3
#> 2 1 5
#> 3 3 5
#> 4 17 22
#> 5 17 35
#> 6 17 49
# we can just use table to make the adjacency matrix. R will create
# a row/column for each level of the factor. We look at the first
# 6x6 entries
table(edge_list)[1:6,1:6] # luckily entry (1,3) = 2 as we hoped
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 2 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


This adjacency matrix is upper triangular. If you want the adjacency matrix to be symmetrical as reflecting an undirected graph you can set the upper and lower triangles of the matrix to be equal using adj.mat[lower.tri(adj.mat)] <- adj.mat[upper.tri(adj.mat)].



If you want the matrix to be just binary (and not record multiple links in different circles) just run it through an ifelse statement:



# to convert to purely binary
adj.mat <- table(edge_list)
adj.mat.bin <- ifelse(adj.mat>1, 1, adj.mat)
adj.mat.bin[1:6,1:6]
#> to
#> from 1 2 3 4 5 6
#> 1 0 0 1 0 1 0
#> 2 0 0 0 0 0 0
#> 3 0 0 0 0 1 0
#> 4 0 0 0 0 0 0
#> 5 0 0 0 0 0 0
#> 6 0 0 0 0 0 0


Created on 2018-11-16 by the reprex package (v0.2.1)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 12:10









gfgmgfgm

2,703727




2,703727












  • Good to know that functions like lower.tri and upper.tri exist, thanks for clarifying this, really appreciated!

    – Chris T.
    Nov 16 '18 at 12:24

















  • Good to know that functions like lower.tri and upper.tri exist, thanks for clarifying this, really appreciated!

    – Chris T.
    Nov 16 '18 at 12:24
















Good to know that functions like lower.tri and upper.tri exist, thanks for clarifying this, really appreciated!

– Chris T.
Nov 16 '18 at 12:24





Good to know that functions like lower.tri and upper.tri exist, thanks for clarifying this, really appreciated!

– Chris T.
Nov 16 '18 at 12:24













1














I borrow the list of vertices ('circles') from @gfgm and use igraph functions.



Loop over the list of vertices with lapply. For each set of vertices, create a full graph (make_full_graph), with number of vertices n equal to length of the vector. Set the name of the vertices (V(g)$name). Convert to 'edge list' (as_edgelist). rbind the resulting matrices.



library(igraph)
m <- do.call(rbind, lapply(circles, function(vert)
g <- make_full_graph(n = length(vert))
V(g)$name <- vert
as_edgelist(g)
))


Set factor levels of 'from' and 'to' vertices, and use table (similar to @gfgm)



tt <- table(factor(m[ , 1], levels = 1:50),
factor(m[ , 2], levels = 1:50))

t[1:8, 1:16]
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 1 0 0 2 0 1 0 0 1 0 0 0 0 0 0 0 1
# 2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# 3 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1
# 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1





share|improve this answer























  • I originally wanted to do this using igraph functions, but having some problems with filling cells in matrix (rbind doesn't seem to work in my original code). Thanks for sharing.

    – Chris T.
    Nov 17 '18 at 16:45






  • 1





    Your code works fine on my data, thanks again for helping on this!

    – Chris T.
    Nov 17 '18 at 17:12
















1














I borrow the list of vertices ('circles') from @gfgm and use igraph functions.



Loop over the list of vertices with lapply. For each set of vertices, create a full graph (make_full_graph), with number of vertices n equal to length of the vector. Set the name of the vertices (V(g)$name). Convert to 'edge list' (as_edgelist). rbind the resulting matrices.



library(igraph)
m <- do.call(rbind, lapply(circles, function(vert)
g <- make_full_graph(n = length(vert))
V(g)$name <- vert
as_edgelist(g)
))


Set factor levels of 'from' and 'to' vertices, and use table (similar to @gfgm)



tt <- table(factor(m[ , 1], levels = 1:50),
factor(m[ , 2], levels = 1:50))

t[1:8, 1:16]
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 1 0 0 2 0 1 0 0 1 0 0 0 0 0 0 0 1
# 2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# 3 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1
# 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1





share|improve this answer























  • I originally wanted to do this using igraph functions, but having some problems with filling cells in matrix (rbind doesn't seem to work in my original code). Thanks for sharing.

    – Chris T.
    Nov 17 '18 at 16:45






  • 1





    Your code works fine on my data, thanks again for helping on this!

    – Chris T.
    Nov 17 '18 at 17:12














1












1








1







I borrow the list of vertices ('circles') from @gfgm and use igraph functions.



Loop over the list of vertices with lapply. For each set of vertices, create a full graph (make_full_graph), with number of vertices n equal to length of the vector. Set the name of the vertices (V(g)$name). Convert to 'edge list' (as_edgelist). rbind the resulting matrices.



library(igraph)
m <- do.call(rbind, lapply(circles, function(vert)
g <- make_full_graph(n = length(vert))
V(g)$name <- vert
as_edgelist(g)
))


Set factor levels of 'from' and 'to' vertices, and use table (similar to @gfgm)



tt <- table(factor(m[ , 1], levels = 1:50),
factor(m[ , 2], levels = 1:50))

t[1:8, 1:16]
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 1 0 0 2 0 1 0 0 1 0 0 0 0 0 0 0 1
# 2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# 3 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1
# 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1





share|improve this answer













I borrow the list of vertices ('circles') from @gfgm and use igraph functions.



Loop over the list of vertices with lapply. For each set of vertices, create a full graph (make_full_graph), with number of vertices n equal to length of the vector. Set the name of the vertices (V(g)$name). Convert to 'edge list' (as_edgelist). rbind the resulting matrices.



library(igraph)
m <- do.call(rbind, lapply(circles, function(vert)
g <- make_full_graph(n = length(vert))
V(g)$name <- vert
as_edgelist(g)
))


Set factor levels of 'from' and 'to' vertices, and use table (similar to @gfgm)



tt <- table(factor(m[ , 1], levels = 1:50),
factor(m[ , 2], levels = 1:50))

t[1:8, 1:16]
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 1 0 0 2 0 1 0 0 1 0 0 0 0 0 0 0 1
# 2 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# 3 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1
# 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 14:46









HenrikHenrik

42.4k994110




42.4k994110












  • I originally wanted to do this using igraph functions, but having some problems with filling cells in matrix (rbind doesn't seem to work in my original code). Thanks for sharing.

    – Chris T.
    Nov 17 '18 at 16:45






  • 1





    Your code works fine on my data, thanks again for helping on this!

    – Chris T.
    Nov 17 '18 at 17:12


















  • I originally wanted to do this using igraph functions, but having some problems with filling cells in matrix (rbind doesn't seem to work in my original code). Thanks for sharing.

    – Chris T.
    Nov 17 '18 at 16:45






  • 1





    Your code works fine on my data, thanks again for helping on this!

    – Chris T.
    Nov 17 '18 at 17:12

















I originally wanted to do this using igraph functions, but having some problems with filling cells in matrix (rbind doesn't seem to work in my original code). Thanks for sharing.

– Chris T.
Nov 17 '18 at 16:45





I originally wanted to do this using igraph functions, but having some problems with filling cells in matrix (rbind doesn't seem to work in my original code). Thanks for sharing.

– Chris T.
Nov 17 '18 at 16:45




1




1





Your code works fine on my data, thanks again for helping on this!

– Chris T.
Nov 17 '18 at 17:12






Your code works fine on my data, thanks again for helping on this!

– Chris T.
Nov 17 '18 at 17:12


















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53336813%2fmerging-several-vectors-of-nodes-into-an-edgelist-and-convert-them-to-an-adjacen%23new-answer', 'question_page');

);

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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin