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?
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.
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.
order() is used to rearrange the dataframe columns in alphabetical order. colnames() is the function to get the columns in the dataframe.
Use desc() to sort a variable in descending order.
From the ?arrange
help page, use desc()
df %>% arrange(desc(string))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With