I have two dataframes. One is a matrix with column and row titles, the other dataframe is the metadata of the matrix. The current row and column names of the matrix are accession numbers, but I have other names in the dataframe that I was to use in as the row/column names. The issue is that they are in different orders. I want to find the row in the metadata that matches the row/column in the matrix and change the row/column name of the matrix to the name matching a different column in the second dataframe.
Matrix:
"XP01020938" "XP3943847" "XP39583574" "XP39384739"
"XP01020938" 1 0.5 0.25 0.1
"XP3943847" 0.5 1 0.5 0.25
"XP39583574" 0.25 0.5 1 0.1
"XP39384739" 0.1 0.25 0.1 1
Metadata:
Accession Name
XP3943847 Tiger
XP39583574 Elephant
XP39384739 Monkey
XP01020938 Horse
Desired:
"Horse" "Tiger" "Elephant" "Monkey"
"Horse" 1 0.5 0.25 0.1
"Tiger" 0.5 1 0.5 0.25
"Elephant" 0.25 0.5 1 0.1
"Monkey" 0.1 0.25 0.1 1
Something like this using match
?
colnames(mat) <- metadata$Name[match(colnames(mat), metadata$Accession)]
rownames(mat) <- metadata$Name[match(rownames(mat), metadata$Accession)]
mat
# Horse Tiger Elephant Monkey
#Horse 1.00 0.50 0.25 0.1
#Tiger 0.50 1.00 0.25 0.1
#Elephant 0.25 0.50 1.00 0.1
#Monkey 0.10 0.25 0.50 1.0