r语言 - 通过匹配数据帧“y”中的第 1 列并插入第 3 列来重命名矩阵“x”的行名/列名



我有两个数据帧。一个是带有列和行标题的矩阵,另一个数据帧是矩阵的元数据。矩阵的当前行名和列名是入藏号,但我在数据帧中还有其他名称,我要在其中用作行/列名。问题是它们的顺序不同。我想在元数据中找到与矩阵中的行/列匹配的行,并将矩阵的行/列名称更改为与第二个数据帧中不同列匹配的名称。

矩阵:

             "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

元数据:

Accession Name
XP3943847 Tiger
XP39583574 Elephant
XP39384739 Monkey
XP01020938 Horse

期望:

          "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

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

最新更新