Matching these matrices in R I have two matrices; I want to convert the row names of first matrix to gene symbol from matched ensemble=gene symbol from second matrix

&gt; head(mat1[,1:2])
                TCGA-L5-A4OG-11A-12R-A260-31
ENSG00000000003                         1818
ENSG00000000005                            0
ENSG00000000419                         1436
ENSG00000000457                         1175
ENSG00000000460                          242
ENSG00000000938                          536
                TCGA-IC-A6RE-11A-12R-A336-31
ENSG00000000003                         4596
ENSG00000000005                            3
ENSG00000000419                          751
ENSG00000000457                          840
ENSG00000000460                          205
ENSG00000000938                          253
&gt; 

&gt; dim(mat1)
[1] 56925    11
&gt; 

&gt; head(mat2)
                ensembl              symbol
ENSG00000274572 ENSG00000274572       ZYXP1
ENSG00000159840 ENSG00000159840         ZYX
ENSG00000162378 ENSG00000162378      ZYG11B
ENSG00000232242 ENSG00000232242    ZYG11AP1
ENSG00000203995 ENSG00000203995      ZYG11A
ENSG00000070476 ENSG00000070476        ZXDC
&gt; 

&gt; dim(mat2)
[1] 36848     2
&gt; 


How I can do that in R?

Thank you
Since they share the ensembl_ID column, you can merge them, then assign the symbol column to the rownames, then delete the symbol column.

something like:

merged &lt;- merge(mat1, mat2, by = 0) 
rownames(merge) &lt;- merged[,'symbol'] 
merged[,symbol] &lt;- NULL


https://stat.ethz.ch/R-manual/R-devel/library/base/html/merge.html