However, when I want to create a network with hundreds of vertices, this method seems to be very clumsy since we need to type so many vertices and edges. Thus, we can import network data from excel data (e.g., edge list, adjacency list, adjacency matrices). However, the three lists seem only work in unweighted networks. For example, a “edge list” data set contains only two columns, without information of edge strength.
So, I wonder how do we create a large weighted network with igraph? How should I prepare the Excel data? Thank you!
library(igraph)
## calculate dataframe
df <- read.csv(header = TRUE, sep=",", strip.white=TRUE,
text = "from, to, weight
1, 2, 1
1, 3, 1
1, 4, 2
1, 5, 2
")
## You can create the data with Excel or Calc whenever you prefer,
## and read from csv text file.
## See also this post in this forum:
## https://igraph.discourse.group/t/largest-weighted-cliques-leads-to-r-crash/1662
df
## convert to graph
g <- graph_from_data_frame(df, vertices=seq(5) )
g <- set_edge_attr(g, "weight", value = df$weight)
strength(g)
## You also could try a random graph, requires no typing.
set.seed(230913)
n = 5
g <- make_star(5, mode="out")
g <- set_edge_attr(g, "weight", value = runif(ecount(g) ))
g <- set_edge_attr(g, name="label", value=E(g)$weight)
strength(g)
plot(g)
Thank you for your great idea! I wonder whether the following (in italics) means that we need to type all the weighted ties in R, If so, will that be too complicated? Or, is the following exactly the way we prepare data in excel? text = "from, to, weight
I’m so glad to tell you that I have successfully made it! Now I know how to calculate the index of weighted network! Thank you soooooo much for your help, which is of great importance to me!