试图将中心性的通用度量方法应用于这样一组非定向的简单数据:
它会产生错误:
Error in closeness(net, gmode = "graph") : unused argument (gmode = "graph")
当我移除参数(gmode =“图”)时,它给出了:
Error in degree(W) : Not a graph object
我尝试过使用这一行来转换它们,但仍然无法工作:
W <- graph_from_adjacency_matrix(df)
W <- graph_from_data_frame(df)
我怎样才能改正呢?谢谢。
以下是台词:
Bob <- c(0,1,0,0,0)
Kate <- c(0,0,0,1,0)
David <- c(0,0,0,1,0)
Jack <- c(0,0,1,0,0)
Peter <- c(0,1,0,0,1)
df <- data.frame(Bob, Kate, David, Jack, Peter)
library(igraph)
W <- data.frame(df)
net <- network(W)
net %v% 'vertex.names'
degree(W, gmode="graph")
closeness(net, gmode="graph")
betweenness(net, gmode="graph")
在回答了这个问题之后,如果它可以帮助某人--将Excel格式转换为adjacency_matrix,请使用下面的行。
df <- readxl::read_excel("spreadsheet.xlsx", sheet = "Sheet1")
W <- as.matrix(df)
W <- graph_from_adjacency_matrix(W)
发布于 2018-10-28 02:56:20
您的代码有点神秘,建议您使用其他包吗?在network
中没有这样的函数igraph
,函数degree
、closeness
和betweenness
没有参数gmode
。我相信以下就是你所追求的目标:
library(igraph)
# We are going to use graph_from_adjacency_matrix, so we need a matrix
# rather than a data frame
df <- cbind(Bob, Kate, David, Jack, Peter)
W <- graph_from_adjacency_matrix(df)
V(W)$name
# [1] "Bob" "Kate" "David" "Jack" "Peter"
degree(W)
# Bob Kate David Jack Peter
# 1 3 2 3 3
closeness(W)
# Bob Kate David Jack Peter
# 0.05000000 0.08333333 0.11111111 0.16666667 0.05000000
# Warning message:
# In closeness(W) :
# At centrality.c:2784 :closeness centrality is not well-defined for disconnected graphs
betweenness(W)
# Bob Kate David Jack Peter
# 0 4 0 3 0
发布于 2022-07-16 13:44:16
功能度既存在于in中,也存在于sna包中。但是,gmode参数只存在于它的sna包版本中。一个基本的解决方案可以是使用sna::degree(net, gmode="graph")
来解决这个问题。
来源:
度{sna} R文档
计算网络职位的程度集中度分数
描述度采用一个或多个图(dat),并返回g所指示的图中的位置集中度(按节点选择)。根据指定的模式,将返回索引度、度或总度(Freeman)度;该函数与集中化兼容,并将返回理论上的最大绝对偏差(与最大偏差),条件为大小(集中用于规范化所观察到的集中度分数)。
用法
degree(dat, g=1, nodes=NULL, gmode="digraph", diag=FALSE,
tmaxdev=FALSE, cmode="freeman", rescale=FALSE, ignore.eval=FALSE)
https://stackoverflow.com/questions/53030129
复制