Distinct

The dplyr::distinct() function extracts unique/distinct rows from 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). This time we will ensure to duplicate rows.

#Load package
library("lterdatasampler")
#Subset of knz_bison tibble for demonstration
bison_tbl <- lterdatasampler::knz_bison |>
    #Convert to tibble
    tibble::as_tibble() |>
    #Slice rows to get 1:5 duplicated once
    dplyr::slice(c(1,1,2,2,3,3,4,4,5,5))
bison_tbl
# A tibble: 10 × 8
   data_code rec_year rec_month rec_day animal_code animal_sex animal_weight
   <chr>        <dbl>     <dbl>   <dbl> <chr>       <chr>              <dbl>
 1 CBH01         1994        11       8 813         F                    890
 2 CBH01         1994        11       8 813         F                    890
 3 CBH01         1994        11       8 834         F                   1074
 4 CBH01         1994        11       8 834         F                   1074
 5 CBH01         1994        11       8 B-301       F                   1060
 6 CBH01         1994        11       8 B-301       F                   1060
 7 CBH01         1994        11       8 B-402       F                    989
 8 CBH01         1994        11       8 B-402       F                    989
 9 CBH01         1994        11       8 B-403       F                   1062
10 CBH01         1994        11       8 B-403       F                   1062
# ℹ 1 more variable: animal_yob <dbl>

Extract unique rows

Extract the unique rows.

dplyr::distinct(bison_tbl)
# A tibble: 5 × 8
  data_code rec_year rec_month rec_day animal_code animal_sex animal_weight
  <chr>        <dbl>     <dbl>   <dbl> <chr>       <chr>              <dbl>
1 CBH01         1994        11       8 813         F                    890
2 CBH01         1994        11       8 834         F                   1074
3 CBH01         1994        11       8 B-301       F                   1060
4 CBH01         1994        11       8 B-402       F                    989
5 CBH01         1994        11       8 B-403       F                   1062
# ℹ 1 more variable: animal_yob <dbl>