List based ordering column followed by sorting another column in the dataframe

I have the following dataframe:

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")

      

as follows:

                                                  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

      

What I want to do is order shortgo

with a predefined list

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

      

and then for each group shortgo

internally sort by PosCount

. Let's say this:

                                                 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

      

I tried this but couldn't:

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

      

What's the correct way to do this?

+3


source to share


1 answer


Use factor

variable views to overlay what you want order

:

In dplyr

just do:

tdf %>% arrange(factor(shortgo,levels=desired_order), desc(PosCount) )

      



In R base, just use:

tdf[order(factor(tdf$shortgo,levels=desired_order), -tdf$PosCount),]

      

+6


source







All Articles