Arrange

The dplyr::arrange() function allows you to arrange the rows of a tibble.

Tidyverse reference page

Dataset

For demonstration we’ll load and subset the knz_bison data from the lterdatasampler package (hyperlink includes install instructions).

#Load package
library("lterdatasampler")
#Subset of knz_bison tibble for demonstration
bison_tbl <- lterdatasampler::knz_bison |>
    #Convert to tibble
    tibble::as_tibble() |>
    #Slice to choose rows by index
    dplyr::slice(c(1,2,911,145,146,179,181,34,35)) |>
    #Select to choose columns based on column names
    dplyr::select(c(data_code,rec_year,animal_sex))
bison_tbl
# A tibble: 9 × 3
  data_code rec_year animal_sex
  <chr>        <dbl> <chr>     
1 CBH01         1994 F         
2 CBH01         1994 F         
3 CBH01         1999 F         
4 CBH01         1995 M         
5 CBH01         1995 F         
6 CBH01         1995 M         
7 CBH01         1995 M         
8 CBH01         1994 M         
9 CBH01         1994 M         

Numeric columns

Arrange the tibble by rec_year.

bison_tbl |> dplyr::arrange(rec_year)
# A tibble: 9 × 3
  data_code rec_year animal_sex
  <chr>        <dbl> <chr>     
1 CBH01         1994 F         
2 CBH01         1994 F         
3 CBH01         1994 M         
4 CBH01         1994 M         
5 CBH01         1995 M         
6 CBH01         1995 F         
7 CBH01         1995 M         
8 CBH01         1995 M         
9 CBH01         1999 F         

dplyr::arrange() will arrange the rows by the selected column in ascending fashion (lowest to highest/a->z). You can use the desc() function on columns you wan’t to be ordered in descending fashion (highest to lowest/z->a).

Arrange the tibble by rec_year in descending fashion.

bison_tbl |> dplyr::arrange(desc(rec_year))
# A tibble: 9 × 3
  data_code rec_year animal_sex
  <chr>        <dbl> <chr>     
1 CBH01         1999 F         
2 CBH01         1995 M         
3 CBH01         1995 F         
4 CBH01         1995 M         
5 CBH01         1995 M         
6 CBH01         1994 F         
7 CBH01         1994 F         
8 CBH01         1994 M         
9 CBH01         1994 M         

String columns

Strings are ordered by alphabetical order.

Default is from A-Z (ascending).

bison_tbl |> dplyr::arrange(animal_sex)
# A tibble: 9 × 3
  data_code rec_year animal_sex
  <chr>        <dbl> <chr>     
1 CBH01         1994 F         
2 CBH01         1994 F         
3 CBH01         1999 F         
4 CBH01         1995 F         
5 CBH01         1995 M         
6 CBH01         1995 M         
7 CBH01         1995 M         
8 CBH01         1994 M         
9 CBH01         1994 M         

Set it to descending will order it from Z-A.

bison_tbl |> dplyr::arrange(desc(animal_sex))
# A tibble: 9 × 3
  data_code rec_year animal_sex
  <chr>        <dbl> <chr>     
1 CBH01         1995 M         
2 CBH01         1995 M         
3 CBH01         1995 M         
4 CBH01         1994 M         
5 CBH01         1994 M         
6 CBH01         1994 F         
7 CBH01         1994 F         
8 CBH01         1999 F         
9 CBH01         1995 F         

Multiple columns

Multiple columns can be used to arrange a tibble.

The priority of ordering is based on the order of the columns, from high to low priority.

bison_tbl |> dplyr::arrange(rec_year, animal_sex)
# A tibble: 9 × 3
  data_code rec_year animal_sex
  <chr>        <dbl> <chr>     
1 CBH01         1994 F         
2 CBH01         1994 F         
3 CBH01         1994 M         
4 CBH01         1994 M         
5 CBH01         1995 F         
6 CBH01         1995 M         
7 CBH01         1995 M         
8 CBH01         1995 M         
9 CBH01         1999 F