简体   繁体   English

匹配两个数据框的行名和列名

[英]Match rownames and colnames of two dataframes

I have two dataframes.我有两个数据框。 The row names for one of them ( metadata ) is the same as the column names for the second ( TPM ).其中一个( metadata )的行名与第二个( TPM )的列名相同。

I reordered the rownames for metadata in a certain way, now I want to reorder the colnames for TPM accordingly.我以某种方式重新排序了metadata的行名,现在我想相应地重新排序TPM的列名。 Both of them should be the same order.他们两个应该是相同的顺序。

First, I make sure that the rownames in metadata are really the same as the colnames in TPM, any uncommon row or column between the two, will be deleted:首先,我确保metadata中的行名与 TPM 中的列名完全相同,两者之间的任何不常见的行或列都将被删除:

INDEX = intersect(colnames(TPM),rownames(metadata))
metadata = metadata[INDEX,]
TPM = TPM[,INDEX]

Then, I make the reorderring of the metadata rownames (it's according to a feature in the metadata, doesn't really matter):然后,我对metadata行名进行重新排序(它根据元数据中的一个特性,并不重要):

metadata = metadata %>% arrange(Benefit)

And now comes the problem, I want the TPM colnames order to be the same as the metadata rownames.现在问题来了,我希望TPM列名顺序与metadata行名相同。 I want to change the TPM colnames order, I tired this:我想更改 TPM colnames 顺序,我厌倦了这个:

index = order(colnames(TPM),rownames(metadata))
TPM = TPM[,index]

but TPM didn't change at all, I also tried this code with match and intersect .TPM根本没有改变,我也用matchintersect尝试了这段代码。 Still TPM didn't change at all. TPM 仍然没有改变。 I don't want to change metadata's rownames, it's already in the order I want.我不想更改元数据的行名,它已经按照我想要的顺序。

Why TPM's colnames aren't changing?为什么 TPM 的 colnames 没有改变?

Sample of TPM: TPM 样本:

structure(list(Pt1 = c(12.1089467388298, 0, 18.0385362276576, 
1.92790576596844, 94.2409551672849, 16.8882703013677, 23.1934795882213
), Pt10 = c(5.31468107381049, 0, 19.5665754959778, 6.08224068432115, 
188.508358461147, 8.48446380342082, 9.79919096042849), Pt8 = c(9.07821549589067, 
0, 14.5403817716173, 9.291006716028, 101.817341286849, 12.4642830001982, 
23.666833737613), Pt101 = c(6.62551158552007, 0, 44.4975176830514, 
5.6097199560158, 29.3442077622205, 4.47109939839761, 22.5645567722583
), Pt103 = c(18.1736419550473, 0, 19.8694720219646, 0.385296099264051, 
10.7494896282499, 5.30680045140835, 6.25389746004431), Pt106 = c(7.28900047884896, 
0.25821427874803, 23.1149486669295, 80.936069669191, 117.783929643365, 
10.167975612355, 23.7821668347939), Pt11 = c(2.81580497213944, 
0, 12.8578712768363, 1.8638846278409, 66.1843554522191, 12.0730665529163, 
18.8979998503006)), row.names = c("A1BG", "NAT2", "ADA", "CDH2", 
"AKT3", "ZBTB11-AS1", "MED6"), class = "data.frame")

Sample of metadata:元数据示例:

structure(list(Cohort = c("NIV3-PROG", "NIV3-NAIVE", "NIV3-NAIVE", 
"NIV3-PROG", "NIV3-PROG", "NIV3-NAIVE", "NIV3-PROG"), Response = c("PD", 
"SD", "PD", "PD", "PD", "PD", "SD"), `Dead/Alive
(Dead = True)` = c(TRUE, 
TRUE, TRUE, FALSE, TRUE, TRUE, TRUE), `Time to Death
(weeks)` = c(22.85714286, 
36.57142857, 37, 69.14285714, 13, 119.5714286, 61.28571429), 
    Subtype = c("CUTANEOUS", "CUTANEOUS", "MUCOSAL", "CUTANEOUS", 
    "MUCOSAL", "CUTANEOUS", "OCULAR/UVEAL"), `Mutational
 Subtype` = c("NA", 
    "NF1", "TripleWt", "TripleWt", "BRAF", "BRAF", "TripleWt"
    ), `M Stage` = c("M1C", "M1A", "M0", "M1B", "M1C", "NA", 
    "M1C"), `Mutation Load` = c("NA", "75", "87", "21", "700", 
    "106", "33"), `Neo-antigen Load` = c("NA", "33", "44", "5", 
    "219", "67", "14"), `Neo-peptide Load` = c("NA", "56", "69", 
    "11", "273", "187", "25"), `Cytolytic Score` = c("977.86911190000001", 
    "65.840716889999996", "457.43633440000002", "1108.8620289999999", 
    "645.54163300000005", "602.6740413", "228.61321050000001"
    ), Benefit = c("NoResponse", "NoResponse", "NoResponse", 
    "NoResponse", "NoResponse", "NoResponse", "NoResponse")), row.names = c("Pt1", 
"Pt10", "Pt8", "Pt103", "Pt106", "Pt11", "Pt82"), class = "data.frame")

I think you can just sort the INDEX vector:我认为您可以对INDEX向量进行排序:

INDEX=sort(intersect(colnames(TPM),rownames(metadata)))
metadata=metadata[INDEX,]
TPM=TPM[,INDEX]

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

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