Simon Ricketts
Simon Ricketts

Reputation: 45

Converting a data frame into adjacency matrix/edge list for network analysis

I am trying to convert a data frame from an online forum into a social network, however I don't know how to transform the data into an adjacency matrix/edge list needed for network analysis.

My code is as follows:

library(igraph)  
graph.data.2002 <- as.matrix(data.2002[,2:3])  
g.2002 <- graph.data.frame(graph.data.2002, directed=FALSE)  
plot(g.2002, vertex.size = 1, vertex.label=NA)  

I am using R for analysis. The current problem is that authors are linked to each other through the ThreadID, however when doing a network analysis, it includes the ThreadID as a node. Ideally i'd like an adjacency matrix / edge list that shows a 1 if an author interacts with all author on the same thread.

(First time posting, so let me know if there's anything that is missing/not proper)

Currently the data is as follows:

ThreadID    AuthorID
659289  193537
432269  136196
572531  170305
230003  32359
459059  47875
635953  181593
235116  51993

Upvotes: 4

Views: 5585

Answers (1)

jac
jac

Reputation: 630

You could use an inner_join to get something like an edge list (just some mild reformatting needed).

If I'm understanding correctly, test 1 should only have one connection, between author 193537 and 32359 who were on thread 659289.

test1 <- data.frame(ThreadID = c(659289, 432269, 572531, 659289),
                 AuthorID = c(193537, 136196, 170305, 32359))
test2 <- dplyr::inner_join(test1, test1, by = "ThreadID")[,-1]
test3 <- apply(test2, 2, as.character) #AuthorID as character will become vertex ID

Check that you get what you expected:

library(network)
test.network <- network(test3, directed = FALSE)
as.sociomatrix(test.network)
as.edgelist(test.network)
plot(test.network, label = test.network%v%"vertex.names")

Upvotes: 6

Related Questions