dplyr::arrange()
   get_help() docs


Description

The arrange() function is part of the {dplyr} package, which is part of the {tidyverse}.

We use this function to arrange tibble rows in ascending order of the given column. To arrange by descending order, you need to use desc() around your column argument.

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::arrange() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::arrange()

Conceptual Usage

# Sort in ascending order of the to given column
tibble %>% 
  arrange(column to sort by in ascending order)

# Sort in descending order of the to given column
tibble %>% 
  arrange(desc(column to sort by in descending order))

Examples

The examples below use the carnivores dataset. Learn more about this dataset with get_help("carnivores").

# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Cheetah           Acinonyx      11.9 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Northern fur seal Callorhinus   15.3 NA     
## 8 Red fox           Vulpes        14.2  0.0504
## 9 Tiger             Panthera       8.2 NA


# Arrange in alphebetal order of genus
carnivores %>% 
  arrange(genus)
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Cheetah           Acinonyx      11.9 NA     
## 2 Northern fur seal Callorhinus   15.3 NA     
## 3 Dog               Canis         13.9  0.07  
## 4 Gray seal         Haliochoerus  17.8  0.325 
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Lion              Panthera      10.5 NA     
## 7 Tiger             Panthera       8.2 NA     
## 8 Arctic fox        Vulpes        11.5  0.0445
## 9 Red fox           Vulpes        14.2  0.0504


# Arrange in reverse alphabetical order of genus
carnivores %>%
  arrange(desc(genus))
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Arctic fox        Vulpes        11.5  0.0445
## 2 Red fox           Vulpes        14.2  0.0504
## 3 Jaguar            Panthera      13.6  0.157 
## 4 Lion              Panthera      10.5 NA     
## 5 Tiger             Panthera       8.2 NA     
## 6 Gray seal         Haliochoerus  17.8  0.325 
## 7 Dog               Canis         13.9  0.07  
## 8 Northern fur seal Callorhinus   15.3 NA     
## 9 Cheetah           Acinonyx      11.9 NA


# Arrange in ascending order of awake
carnivores %>%
  arrange(awake)
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Tiger             Panthera       8.2 NA     
## 2 Lion              Panthera      10.5 NA     
## 3 Arctic fox        Vulpes        11.5  0.0445
## 4 Cheetah           Acinonyx      11.9 NA     
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Dog               Canis         13.9  0.07  
## 7 Red fox           Vulpes        14.2  0.0504
## 8 Northern fur seal Callorhinus   15.3 NA     
## 9 Gray seal         Haliochoerus  17.8  0.325


# Arrange in descending order of awake.
# Since awake is a _numeric_ column, there are two strategies to achieve this

# First, using `desc()`
carnivores %>%
  arrange(desc(awake)) 
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Gray seal         Haliochoerus  17.8  0.325 
## 2 Northern fur seal Callorhinus   15.3 NA     
## 3 Red fox           Vulpes        14.2  0.0504
## 4 Dog               Canis         13.9  0.07  
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Cheetah           Acinonyx      11.9 NA     
## 7 Arctic fox        Vulpes        11.5  0.0445
## 8 Lion              Panthera      10.5 NA     
## 9 Tiger             Panthera       8.2 NA

# Second, using a minus sign as in "arrange in order of _negative awake_"
carnivores %>%
  arrange(-awake) 
## # A tibble: 9 × 4
##   name              genus        awake brainwt
##   <chr>             <fct>        <dbl>   <dbl>
## 1 Gray seal         Haliochoerus  17.8  0.325 
## 2 Northern fur seal Callorhinus   15.3 NA     
## 3 Red fox           Vulpes        14.2  0.0504
## 4 Dog               Canis         13.9  0.07  
## 5 Jaguar            Panthera      13.6  0.157 
## 6 Cheetah           Acinonyx      11.9 NA     
## 7 Arctic fox        Vulpes        11.5  0.0445
## 8 Lion              Panthera      10.5 NA     
## 9 Tiger             Panthera       8.2 NA