I have quite extensive network data in the form
df <- data.frame(name= c("Sue", "Rob", "Jacob", "Terry"),
f1 = c("Rob", "Sue", "Rob", "Jacob"),
f2 = c(NA, "Terry", "Terry", NA),
f3 = c(NA, "Jacob", NA, NA),
bf1 = c(1,1,1,0),
bf2 = c(NA, 1, 0, NA),
bf3 = c(NA, 0, NA, NA) )
where the variables f1
to f3
refer to the friends Sue, Rob, Jacob and Terry have. The variables bf1
to bf3
refer to whether they see them as a best friend.
I would now like to create a graph item with R igraph
based on this dataframe and include all information. So far I have:
adj_list <- subset(df, select = c(name, f1:f3))
m <- as.matrix(adj_list)
el1 <- cbind(m[, 1], c(m[, -1]))
el1 <- el1[complete.cases(el1),]
el1[2,] <- as.numeric(factor(c(el1[1,])))
g1 <- graph_from_edgelist(el1)
But I don't know how to get the secondary information into the graph object. Any ideas?
One approach is to reshape your data to long format.
Reshape
Data can now be read in to
igraph
The
bf
attribute is stored as in the edge attributeE(g)$bf1
. You can then add edge colours or weights when plotting based on this attribute.