Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dplyr arrange by reverse alphabetical order [duplicate]

Tags:

r

dplyr

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?

like image 509
cylondude Avatar asked Nov 06 '17 19:11

cylondude


People also ask

How do I reverse alphabetical order in R?

Minus signs can be used in conjunction with character vectors in order to sort in reverse alphabetical order. If c represents a character variable, then sortFrame(x,c) sorts in alphabetical order, whereas sortFrame(x,-c) sorts in reverse alphabetical order.

How do I arrange data in alphabetical order in R?

If we have string data stored in R data frame columns then we might want to sort the data frame rows in alphabetical order. This can be done with the help of apply and sort function inside transpose function.

Which dplyr command is used to rearrange the order of columns in a data set?

order() is used to rearrange the dataframe columns in alphabetical order. colnames() is the function to get the columns in the dataframe.

How do you sort in descending order dplyr?

Use desc() to sort a variable in descending order.


1 Answers

From the ?arrange help page, use desc()

df %>% arrange(desc(string))
like image 191
MrFlick Avatar answered Sep 16 '22 17:09

MrFlick