20

I'm curious why the arrange function won't will work for alphabetical order but not reverse alphabetical order.

df <- data.frame(string = as.character(c("b", "a", "c")), stringsAsFactors = F) 

df %>% arrange(string) #works

df %>% arrange(-string) #does not work

Am I just using the completely wrong method for what I'm trying to accomplish?

cylondude
  • 1,816
  • 1
  • 22
  • 55
  • In `?arrange`, there's a `desc()` function you can use. Fyi, if you have a data.table, it does work with your attempt: `library(data.table); setDT(df); df %>% arrange(-string)`, though this is probably a dtplyr bug. – Frank Nov 06 '17 at 19:18
  • `-` in `dplyr` generally means "excluding` something, so I think it's better to not use `-` for descending in `arrange` – acylam Nov 06 '17 at 19:37

1 Answers1

29

From the ?arrange help page, use desc()

df %>% arrange(desc(string))
MrFlick
  • 195,160
  • 17
  • 277
  • 295