rdatamatrixreorderlist

reorder the rows and the columns simultaneously for a n*n matrix


I'm trying to reorder the rows and columns of an N*N matrix using a custom order in R.

The data matrix is:

structure(c(1, 0.43604, 0.09338, 0.4223, 0.18212, 0.50444, 0.00412, 
-0.08552, 0.01674, 0.43604, 1, 0.36665, 0.32652, 0.00188, 0.39786, 
-0.0802, -0.31283, -0.18666, 0.09338, 0.36665, 1, 0.58933, 0.09977, 
0.39127, 0.03601, 0.00587, -0.44474, 0.4223, 0.32652, 0.58933, 
1, 0.67113, 0.70805, 0.05161, 0.22738, -0.39247, 0.18212, 0.00188, 
0.09977, 0.67113, 1, 0.73176, -0.04518, 0.30921, -0.08352, 0.50444, 
0.39786, 0.39127, 0.70805, 0.73176, 1, -0.22003, -0.03522, -0.10815, 
0.00412, -0.0802, 0.03601, 0.05161, -0.04518, -0.22003, 1, -0.01579, 
-0.2002, -0.08552, -0.31283, 0.00587, 0.22738, 0.30921, -0.03522, 
-0.01579, 1, 0.25466, 0.01674, -0.18666, -0.44474, -0.39247, 
-0.08352, -0.10815, -0.2002, 0.25466, 1), .Dim = c(9L, 9L), .Dimnames = list(
    c("MEOX2", "SNAI2", "TGIF1", "TCF3", "SOX4", "ZNF22", "RARA", 
    "ASCL1", "LHX2"), c("MEOX2", "SNAI2", "TGIF1", "TCF3", "SOX4", 
    "ZNF22", "RARA", "ASCL1", "LHX2")))

The code which I'm trying to use is:

order <- c("SNAI2","MEOX2","TGIF1","ASCL1","SOX4","ZNF22","LHX2","RARA","TCF2")

cormat_t_agg_GSE13041_GPL570_mapped<-reorder_mat(mat=data.matrix(cormat_t_agg_GSE13041_GPL570_mapped),order = order)

But I'm getting the same error even after making the necessary changes:

Error in reorder_mat(mat = data.matrix(cormat_t_agg_GSE13041_GPL570_mapped),  : 
  The column names of the matrix you want to reorder must
         be present in the vector 'order'

Although I've added a vector of the order this hasn't been working out well.


Solution

  • The error message describes it. Two possibilities:

    Once you fix this name issue, you can do

    cormat_t_agg_GSE13041_GPL570_mapped[order, order]