R,求度、亲近度和介数的误差

R, error in finding degree, closeness and betweenness

尝试将常见的中心性度量应用于这样一个简单的非定向数据集:

它给出错误:

Error in closeness(net, gmode = "graph") : unused argument (gmode = "graph")

当我删除参数 (gmode = "graph) 时,它给出:

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)

您的代码有点神秘,暗示可能使用了其他一些包? igraph 中没有这样的函数 network,函数 degreeclosenessbetweenness 没有参数 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