尝试将集中度的通用度量应用于像这样的非定向简单数据集:
出现错误:
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)
答案 0 :(得分:1)
您的代码有些神秘,暗示是否可能使用了其他软件包? 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