Sort a dataframe with arrange()

We’ll use the msleep dataset that comes from tidyverse. msleep contains some data about how much different kinds of mammals sleep. Here’s what the first few rows of msleep look like.

msleep |> head()
# A tibble: 6 × 11
  name    genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
  <chr>   <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
1 Cheetah Acin… carni Carn… lc                  12.1      NA        NA      11.9
2 Owl mo… Aotus omni  Prim… <NA>                17         1.8      NA       7  
3 Mounta… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6
4 Greate… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1
5 Cow     Bos   herbi Arti… domesticated         4         0.7       0.667  20  
6 Three-… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

Sort values ascending

If we have a list of numbers [8, 5, 2, 13, 3], and we sort those numbers ascending (i.e., in ascending order), we get [2, 3, 5, 8, 13]. The smallest values come first.

Let’s sort the sleep_total column ascending, using the tidyverse function arrange(). The top of the data frame will show us the mammals who sleep the least.

The default behaviour of arrange() is to sort the given column ascending.

msleep |>
  arrange(sleep_total)
# A tibble: 83 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Giraf… Gira… herbi Arti… cd                   1.9       0.4      NA      22.1
 2 Pilot… Glob… carni Ceta… cd                   2.7       0.1      NA      21.4
 3 Horse  Equus herbi Peri… domesticated         2.9       0.6       1      21.1
 4 Roe d… Capr… herbi Arti… lc                   3        NA        NA      21  
 5 Donkey Equus herbi Peri… domesticated         3.1       0.4      NA      20.9
 6 Afric… Loxo… herbi Prob… vu                   3.3      NA        NA      20.7
 7 Caspi… Phoca carni Carn… vu                   3.5       0.4      NA      20.5
 8 Sheep  Ovis  herbi Arti… domesticated         3.8       0.6      NA      20.2
 9 Asian… Elep… herbi Prob… en                   3.9      NA        NA      20.1
10 Cow    Bos   herbi Arti… domesticated         4         0.7       0.667  20  
# ℹ 73 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

(TIL that giraffes sleep less than two hours a day??)

Sort values descending

If we have a list of numbers [8, 5, 2, 13, 3] and we sort those numbers descending (i.e., in descending order), we get [13, 8, 5, 3, 2]. The largest values come first.

Let’s sort the sleep_total column descending. To tell arrange() that we want to sort this column in descending order, we need to first wrap the column name in the function desc() and then wrap that in arrange(), as below:

msleep |>
  arrange(desc(sleep_total))
# A tibble: 83 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Littl… Myot… inse… Chir… <NA>                19.9       2         0.2     4.1
 2 Big b… Epte… inse… Chir… lc                  19.7       3.9       0.117   4.3
 3 Thick… Lutr… carni Dide… lc                  19.4       6.6      NA       4.6
 4 Giant… Prio… inse… Cing… en                  18.1       6.1      NA       5.9
 5 North… Dide… omni  Dide… lc                  18         4.9       0.333   6  
 6 Long-… Dasy… carni Cing… lc                  17.4       3.1       0.383   6.6
 7 Owl m… Aotus omni  Prim… <NA>                17         1.8      NA       7  
 8 Arcti… Sper… herbi Rode… lc                  16.6      NA        NA       7.4
 9 Golde… Sper… herbi Rode… lc                  15.9       3        NA       8.1
10 Tiger  Pant… carni Carn… en                  15.8      NA        NA       8.2
# ℹ 73 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

Turns out bats, opossums, and armadillos sleep a lot!

Sorting works for numbers and letters

If you use arrange() on a numeric column, as above, it’ll sort the values in numeric order.

If you use arrange() on an alphabetical column, it’ll sort the values in alphabetical order!

msleep |>
  arrange(name)
# A tibble: 83 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Afric… Loxo… herbi Prob… vu                   3.3      NA        NA      20.7
 2 Afric… Cric… omni  Rode… <NA>                 8.3       2        NA      15.7
 3 Afric… Rhab… omni  Rode… <NA>                 8.7      NA        NA      15.3
 4 Arcti… Vulp… carni Carn… <NA>                12.5      NA        NA      11.5
 5 Arcti… Sper… herbi Rode… lc                  16.6      NA        NA       7.4
 6 Asian… Elep… herbi Prob… en                   3.9      NA        NA      20.1
 7 Baboon Papio omni  Prim… <NA>                 9.4       1         0.667  14.6
 8 Big b… Epte… inse… Chir… lc                  19.7       3.9       0.117   4.3
 9 Bottl… Turs… carni Ceta… <NA>                 5.2      NA        NA      18.8
10 Brazi… Tapi… herbi Peri… vu                   4.4       1         0.9    19.6
# ℹ 73 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>
msleep |>
  arrange(desc(name))
# A tibble: 83 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 "West… Euta… herbi Rode… <NA>                14.9      NA        NA       9.1
 2 "Vole… Micr… herbi Rode… <NA>                12.8      NA        NA      11.2
 3 "Vesp… Calo… <NA>  Rode… <NA>                 7        NA        NA      17  
 4 "Tree… Tupa… omni  Scan… <NA>                 8.9       2.6       0.233  15.1
 5 "Tree… Dend… herbi Hyra… lc                   5.3       0.5      NA      18.7
 6 "Tige… Pant… carni Carn… en                  15.8      NA        NA       8.2
 7 "Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6
 8 "Thir… Sper… herbi Rode… lc                  13.8       3.4       0.217  10.2
 9 "Thic… Lutr… carni Dide… lc                  19.4       6.6      NA       4.6
10 "Tenr… Tenr… omni  Afro… <NA>                15.6       2.3      NA       8.4
# ℹ 73 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>