Can we specify the order for factor/ character type variables when using 'arrange'

dplyr

#1

When using arrange to order rows, if the type of the variable to be ordered is ‘factor’ or ‘character’, is it possible to specify the order instead of following alphabetical order?

I am now playing with the ‘crime’ data set, which is used in a ggmap example. I would like to order the dataframe using arrange, by offense type, giving it the order robbery > aggravated assault >rape >murder

Thanks!


#2

By default, a character vector will be sort in alphabetical order. You can use factor type to help you order a character vector. The order of the factor depends of its levels. You’ll need to precise the order at the creation of the factor or after by reordering the factor.

In your case, I use forcats::fct_relevel from tidyverse to reorder the factor’s levels so that arrange sort in the desired order.

data('crime', package = "ggmap")

library(tidyverse)
crime <- as_tibble(crime)

# By default it is alphetical order for factor and character
crime %>%
  distinct(offense) %>%
  arrange(offense)
#> # A tibble: 7 x 1
#>              offense
#>               <fctr>
#> 1 aggravated assault
#> 2         auto theft
#> 3           burglary
#> 4             murder
#> 5               rape
#> 6            robbery
#> 7              theft

# You need to reorder levels in factor
 crime %>%
  distinct(offense) %>%
  mutate(offense = fct_relevel(offense, c("theft", "auto theft", "robbery", "burglary", "aggravated assault", "rape", "murder"))) %>%
  arrange(offense)
#> # A tibble: 7 x 1
#>              offense
#>               <fctr>
#> 1              theft
#> 2         auto theft
#> 3            robbery
#> 4           burglary
#> 5 aggravated assault
#> 6               rape
#> 7             murder

Created on 2017-12-03 by the reprex package (v0.1.1.9000).

To know more about forcats and how to play with factos, you can go to


#3

Thank you cderv for you very detailed response. This is the 1st question that I post in this community.
Actually I have a hard copy of book and now in my 2nd review. I will jump into the Chapter on factor today.


#4

Just do it by using an alternative factor function from the ‘forcats’ package

=============================================
library(forcats)

crime %>%
distinct(offense) %>%
mutate(offense = factor(offense, levels= c(“theft”, “auto theft”, “robbery”, “burglary”, “aggravated assault”, “rape”, “murder”))) %>%
arrange(offense)