2014-10-09 37 views
0

数字,然后图形我有一个的Symmetrix距离矩阵(X):强迫从矩阵中的R

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'_ 

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

在此先感谢。

回答

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 
+0

唉。 > x_mat < - data.matrix(X) > is.matrix(x_mat) [1] TRUE > is.numeric(x_mat) [1] FALSE > – user1038055 2014-10-09 14:55:19

+0

除,当我读取文件中分开,而与其他文件使用lapply,它的作品。去搞清楚。谢谢。 – user1038055 2014-10-09 14:58:03

+0

@ user1038055,你的'lapply'中'x'的结构必须是'data.matrix'的一个数据框才能正常工作的。很高兴你找到了解决方案。如果您满意,请接受答案。 – cdeterman 2014-10-09 15:00:06