I am currently working with the 'igraph' package on R.
I have created two functions that create a statistical table of graph object that work pretty well if used directly on a single graph object (here is an example of what they look like) :
Sfn <- function(x) # Give a table of statistics for nodes
{
Name <- deparse(substitute(x))
Nodes <- V(x)$name
Dtotal <- degree(x, mode="all")
Eigenvector <- eigen_centrality(x)
statistics_table <- data.frame(Nodes,
Dtotal,
Eigenvector)
colnames(statistics_table) <- c("Nodes","Total Degrees",
"Eigenvector centrality")
write.table(statistics_table,
file = paste0("Table_of_",Name,"_nodes.csv"),
sep=",",
row.names = F)
print("Success.")
}
As I am using several graph objects, I would like not to have to write one line per command, such as :
Sfn(g)
Sfn(g2)
Sfn(g3)
# etc...
Sfn(n)
I would thus like to create a vector of lists in which I could collect all my graph objects. I created something like that :
G <- c(
list(CC1),list(CC2),list(CC3),
list(CC4),list(CC5),list(CC6),
list(CC7),list(CC8),list(CC9),
list(CC10),list(CC11),list(CC12))
Yet, this solution is not optimal. First, it is too long to write if I have, for example, 100 graph objects. Secondly, if I write my script with an for()
loop, the name of the variable sent to my function will be the name of the parameter of for()
, thus, ruining the variable Name
of my function Sfn
. In other words, the script for(i in G) {Sfn(G)}
does not work, because the variable Name
will be equal to i
:
# In my function Sfn, Name <- deparse(substitute(i)),
for(i in G) {print(deparse(substitute(i)))}
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
[1] "i"
Also, the solution there : (Change variable name in for loop using R) does not work because I have, in my graph objects, very different randomly attributed graph names (such as "CC1","g2","CT3","CC1T3", etc).
Do you have any idea on how I could possibly:
1 - achieve a better way of creating a vector of graph objects ?
2 - make the name of the parameter sent to my variable the same as the actual name of the variable ?