Reputation: 883
I have some data that I am going to be using in igraph. There are two dataframes for each sample: one with a list of links and the other with a list of nodes. Igraph uses these dataframes to produce a net eg.:
net1 <- graph_from_data_frame(d=sample1links, vertices=sample1nodes)
where sample1links
and sample1nodes
are different dataframes.
I have lots of different samples so it would be good if I could automate this process. Is there a method that I can use that will do the process above to produce all of the nets for the samples (i.e. net1-netn, where n is the number of samples)?
Thanks a lot to anyone who can help!!
Upvotes: 0
Views: 41
Reputation: 54247
Here's an example:
library(igraph)
# 2 example graphs as data frames
sample1nodes <- read.table(text = "a\nb\n\nc\nd\ne")
sample1links <- read.table(text = "a c\n\nb c\nc d")
sample2nodes <- read.table(text = "A\nB\n\nc\nd\ne")
sample2links <- read.table(text = "A c\n\nB c\nc d")
# create igraph objects from data frames in a list
glst <- Map(graph_from_data_frame, d=mget(ls(pattern = "sample\\d+links")), vertices=mget(ls(pattern = "sample\\d+nodes")))
names(glst) <- paste0("net", sub("sample(\\d+)links", "\\1", names(glst))) # rename list objects
# Store list elements in the global environment under their names
list2env(glst, envir = .GlobalEnv)
# plot
par(mfrow = c(1,2))
plot(net1)
plot(net2)
Upvotes: 2