0

I have a symmetrix distance matrix (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   

which I would like to analyse as a graph, in order to compute its spectral density. In order to do that, I want to follow these steps (with 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)

But I'm having trouble from the start. I can get my matrix to be a matrix

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

But I cannot coerce it to be numeric

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

I need the adjacency matrix to be numeric in order to move along my pipeline. Any advice? Alternative methods to achieve my goal, of course, are also welcome.

Thanks in advance.

cdeterman
  • 19,630
  • 7
  • 76
  • 100

1 Answers1

1

data.matrix should provide what you need.

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
cdeterman
  • 19,630
  • 7
  • 76
  • 100
  • Alas. > x_mat <- data.matrix(x) > is.matrix(x_mat) [1] TRUE > is.numeric(x_mat) [1] FALSE > – user1038055 Oct 09 '14 at 14:55
  • Except, when I read the file in separately, rather with other files using lapply, it works. Go figure. Thanks. – user1038055 Oct 09 '14 at 14:58
  • @user1038055, the structure of `x` from your `lapply` must have been something besides a dataframe for `data.matrix` to work appropriately. Glad you found a solution. If you are satisfied, please accept the answer. – cdeterman Oct 09 '14 at 15:00
  • I'll ask the follow-up question - is there a way to read in multiple numeric matrices at once? - separately. – user1038055 Oct 09 '14 at 16:18