22

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?

2
  • 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. Commented Nov 6, 2017 at 19:18
  • - in dplyr generally means "excluding` something, so I think it's better to not use - for descending in arrange Commented Nov 6, 2017 at 19:37

1 Answer 1

31

From the ?arrange help page, use desc()

df %>% arrange(desc(string)) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.