2
df <- as.data.frame(c("15-25", "0-4", "5-10", "11-14", "100+"))

How do I sort is to 0-4 comes first and 100+ last? Dplyr solution would be nice, arrange does not work for me.

MLEN
  • 2,162
  • 2
  • 20
  • 36
  • i went through the links shared, to reorder the factor, we had to explicitly specify the levels in `factor()` right? another approach could be `mixedsort` from `gtools` package where it can sort directly. Just a thought. Do correct if I'm wrong. @Cath Sotos, Imo – joel.wilson Jan 24 '17 at 13:19
  • something like `factor(df$x, levels = mixedsort(df$x)` – joel.wilson Jan 24 '17 at 13:20
  • @Sotos mind sharing your views on my comments. Since here the levels are numeric though stored as strings, we can reorder them without explicitly writing them in `levels = ` – joel.wilson Jan 24 '17 at 13:34
  • was that what you were looking for? – joel.wilson Jan 24 '17 at 14:42
  • Is there any smart way to do without typing in every single one? The above was just one exactly, I actually have 20 groups, where two of them is not in correct order. One is first, when it should be last and the other is around the middle, but should be number 2 – MLEN Jan 24 '17 at 19:23
  • my code didn't require to type every single. You might have to raise a new question or read through the links shared above – joel.wilson Jan 24 '17 at 19:29

0 Answers0