简体   繁体   English

通过匹配数据框y中的第1列并插入第3列来重命名矩阵x的行名/名称

[英]Renaming Rownames/Colnames of matrix `x` by matching column 1 in dataframe `y` and inserting column 3

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 ? 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM