4

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?

0

1 Answer 1

6

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),]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.