14

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,628
  • 1
  • 19
  • 50
  • 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

22

From the ?arrange help page, use desc()

df %>% arrange(desc(string))
MrFlick
  • 163,738
  • 12
  • 226
  • 242