actors <- data.frame(name=c("Alice", "Bob", "Cecil", "David",
"Esmeralda", "Ben", "Fritz", "Jon", "Anna",
"Julia"),
age=c(48,33,45,34,21, 12,33,44,66,99),
gender=c("F","M","F","M","F", "F","M","F","M","F"))
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda", "Cecil", "David",
"Esmeralda", "Jon", "Anna",
"Julia", "Bob", "Cecil", "Cecil", "David"),
to=c("Alice", "Bob", "David", "Esmeralda", "Cecil", "David", "Alice", "Alice", "Bob", "Alice", "Fritz", "Jon", "Anna", "Alice", "Bob", "Cecil"),
same.dept=c(FALSE,FALSE,TRUE,FALSE,FALSE,TRUE, TRUE,FALSE,FALSE,TRUE,FALSE,FALSE, FALSE,TRUE,FALSE,FALSE),
friendship=c(4,5,5,2,1,1,1,3,5,7,9,1, 7,8,2,4), advice=c(4,5,5,4,2,3,1,5,7,8,2,4, 7,8,2,4))
g <- graph.data.frame(relations, directed=TRUE, vertices=actors)
V(g)$name <- as.character(vertex_attr(g, "gender"))
edges <- get.data.frame(g)
external <- length(which(edges$from != edges$to))
internal <- length(which(edges$from == edges$to))
ei_index = (external - internal) / nrow(edges)
new.graph <- permute(g, sample(vcount(g)))
答案 0 :(得分:1)
igraph文档说permute keeps all graph, vertex and edge attributes of the graph.
,这意味着您只是在更改节点列表的顺序。以下是“顶点”列表如何不同但边缘列表保持不变的示例。
像rewire
这样的函数对您有用吗?
ei_index <- function(g){
edges <- get.data.frame(g)
external <- length(which(edges$from != edges$to))
internal <- length(which(edges$from == edges$to))
ei_index = (external - internal) / nrow(edges)
ei_index
}
set.seed(4321)
sapply(1:20, function(x){
temp_g <- rewire(g, each_edge(p = 1, loops = F))
ei_index(temp_g)
})