0

Lets say I have a data frame like following:

  `df$A`    `df$B`     `df$C`
   BCG        2          4
   ACD        5          1
   DFA        4          3 
   ACD        4          2
   BCG        1          4
   DFA        3          5
   BCG        5          4

Assuming that my df$A is factor, how can I group the data frame rows based on factor level in df$A. In short, I am expecting the result like following:

  `df$A`    `df$B`     `df$C`
   BCG        2          4
   BCG        1          4
   BCG        5          4
   ACD        5          1
   ACD        4          2
   DFA        4          3
   DFA        3          5 

Thank you for all the help.

Santosh M.
  • 2,356
  • 1
  • 17
  • 29
  • 1
    `df[order(df$A),]` I think is what you are looking for. If this is not working, you can rename the variables in your data.frame, `names(df) <- LETTERS[1:3]`. – lmo Aug 04 '16 at 20:25

1 Answers1

1

Consider the following example,

dfc <- data.frame(a = rep(c('a', 'b'), 5), b = 1:10, c = 2:11)

To order dfc by the factors, you can simply perform:

dfc[order(dfc$a),]
Pj_
  • 824
  • 6
  • 15