当我们使用' betweenness‘函数的(g,weights=NULL,directed = FALSE)时,如果图有weight属性,即使我们写了weights=NULL,该函数仍然会使用weight属性计算。但是我想在没有权重属性的情况下计算介数。所以我觉得这个函数看起来很奇怪。为什么当我们编写weights=NULL时,它仍然使用weight属性?
function (graph, v = V(graph), directed = TRUE, weights = NULL,
nobigint = TRUE, normalized = FALSE)
{
if (!is.igraph(graph)) {
stop("Not a graph object")
}
v <- as.igraph.vs(graph, v)
if (is.null(weights) && "weight" %in% list.edge.attributes(graph)) {
weights <- E(graph)$weight
}
if (!is.null(weights) && any(!is.na(weights))) {
weights <- as.numeric(weights)
}
else {
weights <- NULL
}
on.exit(.Call("R_igraph_finalizer", PACKAGE = "igraph"))
res <- .Call("R_igraph_betweenness", graph, v - 1, as.logical(directed),
weights, as.logical(nobigint), PACKAGE = "igraph")
if (normalized) {
vc <- vcount(graph)
res <- 2 * res/(vc * vc - 3 * vc + 2)
}
if (getIgraphOpt("add.vertex.names") && is.named(graph)) {
names(res) <- V(graph)$name[v]
}
res
}发布于 2013-04-21 17:58:12
权重选项与忽略和不使用权重无关。这是一个关于为用户提供他们自己的权重向量的选项。
从doc
权重-可选的正权重向量,用于计算加权的介数。如果图形具有权重边属性,则默认情况下使用此属性。
因此,如果为weights=NULL,则该函数将默认使用E(g)$weight。
要自己这样做,可以去掉权重或将其设置为1。
E(g)$weight <- 1https://stackoverflow.com/questions/16129233
复制相似问题