2014-10-09 72 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