pdubois
pdubois

Reputation: 7800

Ordering column based on a list followed by sorting another column in a data frame

I have the following data frame:

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

that looks like this:

                                                  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 then is to order the shortgo with a predefined list

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

and then for each shortgo group internally sort by PosCount. Yielding 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 failed:

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

What's the right way to do it?

Upvotes: 4

Views: 68

Answers (1)

thelatemail
thelatemail

Reputation: 93938

Use factor representations of a variable to impose your desired order:

In dplyr, just do:

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

In base R, just use:

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

Upvotes: 6

Related Questions