簡體   English   中英

如何通過根據另一個數據幀的行名的順序映射一個數據幀的列名來對 R 中的數據幀進行排序?

[英]How to sort a dataframe in R by mapping column names of one dataframe based on the order of row names of another dataframe?

我希望通過根據另一個數據幀的行名的順序映射一個數據幀的列名來對 R 中的數據幀進行排序。 在此之后,確定為繪制熱圖而正確排序和映射的行和列名稱。 同樣,我有很多這樣的數據框,使用 MS Excel 手動執行相同的操作非常乏味。 請幫我解決這個問題。 下面給出了兩個數據幀(df1 和 df2)和輸出數據幀 (df3) 的示例:

dput(df1)
structure(list(Condition = c("Pos", "Pos", "Pos", "Pos", "Neg", 
"Neg", "Neg", "Neutral", "Neutral", "Neutral", "Neutral"), State = c("A", 
"A", "A", "A", "A", "A", "B", "B", "B", "B", "B")), class = "data.frame", row.names = c("GSM533312", 
"GSM533313", "GSM533321", "GSM533318", "GSM533315", "GSM533317", 
"GSM533314", "GSM533319", "GSM533320", "GSM533316", "GSM533322"
))



> dput(df2)
structure(list(GSM533312 = c(126.9026154, 417.8250699, 10, 10, 
10, 23.52525494, 10, 39.17102201, 10, 18.67273627), GSM533313 = c(69.54673614, 
462.6758022, 10, 10, 10, 26.73450534, 10, 27.07827269, 10, 16.24450811
), GSM533314 = c(60.80383818, 520.416152, 10, 10, 10, 24.43279709, 
10, 35.73886139, 10, 14.23653399), GSM533315 = c(55.13260829, 
295.2346963, 10, 10, 10, 14.06799091, 10, 20.01497351, 10, 22.59825164
), GSM533316 = c(45.89227444, 381.1542662, 10, 10, 10, 23.09791483, 
10, 29.2303841, 10, 19.17842478), GSM533317 = c(70.44756764, 
270.053056, 10, 10, 10, 11.72493597, 10, 29.75588034, 10, 20.24234006
), GSM533318 = c(137.5149798, 399.8257215, 10, 10, 10, 25.61676988, 
10, 17.51122374, 10, 26.61600151), GSM533319 = c(149.9062073, 
392.0936192, 10, 10, 10, 34.50483435, 10, 17.70500769, 10, 25.42173724
), GSM533320 = c(47.45800668, 308.1893821, 10, 10, 10, 19.99262754, 
10, 22.51401429, 10, 17.11589963), GSM533321 = c(58.67205043, 
334.0037624, 10, 10, 10, 10, 10, 17.07492728, 10, 16.99331094
), GSM533322 = c(47.9377578, 346.2849982, 10, 10, 10, 11.17797599, 
10, 19.35690144, 10, 18.52996342)), class = "data.frame", row.names = c("Gene_A", 
"Gene_B", "Gene_C", "Gene_D", "Gene_E", "Gene_F", "Gene_G", "Gene_H", 
"Gene_AA", "Gene_AB"))


> dput(df3)
structure(list(GSM533312 = c(126.9026154, 417.8250699, 10, 10, 
10, 23.52525494, 10, 39.17102201, 10, 18.67273627), GSM533313 = c(69.54673614, 
462.6758022, 10, 10, 10, 26.73450534, 10, 27.07827269, 10, 16.24450811
), GSM533321 = c(58.67205043, 334.0037624, 10, 10, 10, 10, 10, 
17.07492728, 10, 16.99331094), GSM533318 = c(137.5149798, 399.8257215, 
10, 10, 10, 25.61676988, 10, 17.51122374, 10, 26.61600151), GSM533315 = c(55.13260829, 
295.2346963, 10, 10, 10, 14.06799091, 10, 20.01497351, 10, 22.59825164
), GSM533317 = c(70.44756764, 270.053056, 10, 10, 10, 11.72493597, 
10, 29.75588034, 10, 20.24234006), GSM533314 = c(60.80383818, 
520.416152, 10, 10, 10, 24.43279709, 10, 35.73886139, 10, 14.23653399
), GSM533319 = c(149.9062073, 392.0936192, 10, 10, 10, 34.50483435, 
10, 17.70500769, 10, 25.42173724), GSM533320 = c(47.45800668, 
308.1893821, 10, 10, 10, 19.99262754, 10, 22.51401429, 10, 17.11589963
), GSM533316 = c(45.89227444, 381.1542662, 10, 10, 10, 23.09791483, 
10, 29.2303841, 10, 19.17842478), GSM533322 = c(47.9377578, 346.2849982, 
10, 10, 10, 11.17797599, 10, 19.35690144, 10, 18.52996342)), class = "data.frame", row.names = c("Gene_A", 
"Gene_B", "Gene_C", "Gene_D", "Gene_E", "Gene_F", "Gene_G", "Gene_H", 
"Gene_AA", "Gene_AB"))

確定名稱的等效性:

colnames(df3)==rownames(df1)

謝謝,

圖菲克

我們可以使用row.names作為列名

dfn <- df2[row.names(df1)]
identical(df3, dfn)
#[1] TRUE

如果有很多數據集,把它放在一個list ,一步一步地做,然后把它存儲在一個list

lst1 <- lapply(list(df2, df4, df5), function(dat) dat[row.names(df1)])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM