After having been away from R for a couple of decades (I was never a guru by any means) but just having refamiliarised myself with vector manipulation, I realise what I really need are graphs. For the Bio Population model I have in mind, I would like to automatically create (in the first case) simple graphs with a limited number of nodes and edges eg:
10 nodes
With nodes having a varied, random number of edges eg between 1 and 9
Can graphs be created like that in a somewhat similar way I can create vectors with random numbers in the elements?
# Install and load the igraph package
if (!requireNamespace("igraph", quietly = TRUE)) {
install.packages("igraph")
}
library(igraph)
#>
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#>
#> decompose, spectrum
#> The following object is masked from 'package:base':
#>
#> union
# Set the number of nodes and edges
num_nodes <- 10
num_edges <- sample(1:9, 1)
# Generate a random graph with the specified number of nodes and edges
g <- sample_gnm(n = num_nodes, m = num_edges, directed = FALSE)
# Plot the graph
plot(g)