根据列表对列进行排序,然后对数据框中的另一列进行排序

时间:2015-05-05 03:58:43

标签: r dplyr

我有以下数据框:

tdf <- structure(list(GO = c("Cytokine-cytokine receptor interaction", 
"Cytokine-cytokine receptor interaction|Endocytosis", "I-kappaB kinase/NF-kappaB signaling", 
"NF-kappa B signaling pathway", "NF-kappaB import into nucleus", 
"T cell chemotaxis"), PosCount = c(17, 18, 4, 5, 1, 2), shortgo = structure(c(1L, 
1L, 2L, 2L, 2L, 3L), .Label = c("z", "X", "y"), class = "factor")), .Names = c("GO", 
"PosCount", "shortgo"), row.names = c(NA, 6L), class = "data.frame")
desired_order <- c("y", "X", "z")

看起来像这样:

                                                  GO PosCount shortgo
1             Cytokine-cytokine receptor interaction       17       z
2 Cytokine-cytokine receptor interaction|Endocytosis       18       z
3                I-kappaB kinase/NF-kappaB signaling        4       X
4                       NF-kappa B signaling pathway        5       X
5                      NF-kappaB import into nucleus        1       X
6                                  T cell chemotaxis        2       y

我想要做的是订购带有预定义列表的shortgo

desired_order <- c("y", "X", "z")

然后为每个shortgo组内部按PosCount排序。产生这个:

                                                 GO PosCount shortgo
                                  T cell chemotaxis        2       y
                       NF-kappa B signaling pathway        5       X
                I-kappaB kinase/NF-kappaB signaling        4       X
                      NF-kappaB import into nucleus        1       X
 Cytokine-cytokine receptor interaction|Endocytosis       18       z
             Cytokine-cytokine receptor interaction       17       z

我尝试了但失败了:

library(dplyr)
#tdf %>% arrange(as.character(shortgo), desc(PosCount))
tdf %>% arrange(desired_order, desc(PosCount))

做正确的方法是什么?

0 个答案:

没有答案