0

我有一个对称距离矩阵(x):

0   2.6096  2.3601  5.6109   
2.6096  0   1.7045  6.8441   
2.3601  1.7045  0   6.5946   
5.6109  6.8441  6.5946  0   

我想将其分析为图表,以计算其光谱密度。为了做到这一点,我想按照以下步骤(使用 igraph):

x_mat <- as.matrix(x,matrix.type="adjacency") #get adjacency matrix`   
x_graph <- graph.adjacency(x_mat) #convert to graph   
x_lap <- graph.laplacian(x_graph) #convert to laplacian graph   
x_eig <- eigen(x_lap,symmetric=TRUE,only.values=TRUE)   
(I'm not sure how to plot the spectral density, but I'm not even there yet)

但我从一开始就有麻烦。我可以让我的矩阵成为一个矩阵

x_mat <- as.matrix(x,matrix.type="adjacency")  
is.matrix(x_mat)
[1] TRUE   
x_mat      
[,1]                   
[1,] Numeric,39204

但我不能强迫它是数字

mode(x_mat) <- "numeric"   
_Error in eval(expr, envir, enclos) :    
  (list) object cannot be coerced to type 'double'_

我需要邻接矩阵是数字的,以便沿着我的管道移动。有什么建议吗?当然,也欢迎实现我的目标的替代方法。

提前致谢。

4

1 回答 1

1

data.matrix应该提供你需要的东西。

df <- read.table(header=F, text='
                     0   2.6096  2.3601  5.6109   
2.6096  0   1.7045  6.8441   
2.3601  1.7045  0   6.5946   
5.6109  6.8441  6.5946  0  
                 ')


mat <- data.matrix(df)
is.matrix(mat)
> is.matrix(mat)
[1] TRUE
is.numeric(mat)
> is.numeric(mat)
[1] TRUE
于 2014-10-09T14:51:52.817 回答