Dplyr distinct() : identifier les observations et les lignes uniques

La fonction distinct() sert à identifier les lignes (observations) uniques d'un data frame. Ce besoin se présente fréquemment, et la fonction distinct(), combinée à d'autres fonctions de dplyr, en particulier group_by(), permet de traiter pratiquement toutes les tâches de ce type.

Dans ce chapitre, nous utiliserons les données mpg du package tidyverse. Pour accéder aux données, il faut activer à la fois tidyverse et les données mpg à l'aide des commandes suivantes :

library(tidyverse)
data(mpg)

## Inspecter les 10 premières lignes
head(mpg, 10)
## A tibble: 10 × 11
   manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class
   <chr>        <chr>      <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>
 1 audi         a4           1.8  1999     4 auto(l5)   f        18    29 p     compact
 2 audi         a4           1.8  1999     4 manual(m5) f        21    29 p     compact
 3 audi         a4           2    2008     4 manual(m6) f        20    31 p     compact
 4 audi         a4           2    2008     4 auto(av)   f        21    30 p     compact
 5 audi         a4           2.8  1999     6 auto(l5)   f        16    26 p     compact
 6 audi         a4           2.8  1999     6 manual(m5) f        18    26 p     compact
 7 audi         a4           3.1  2008     6 auto(av)   f        18    27 p     compact
 8 audi         a4 quattro   1.8  1999     4 manual(m5) 4        18    26 p     compact
 9 audi         a4 quattro   1.8  1999     4 auto(l5)   4        16    25 p     compact
10 audi         a4 quattro   2    2008     4 manual(m6) 4        20    28 p     compact

Commençons par sélectionner toutes les lignes ayant des valeurs uniques pour la variable manufacturer.

mpg |>
  distinct(manufacturer, .keep_all = TRUE)
## A tibble: 15 × 11
   manufacturer model              displ  year   cyl trans      drv     cty   hwy fl    class
   <chr>        <chr>              <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>
 1 audi         a4                   1.8  1999     4 auto(l5)   f        18    29 p     compact
 2 chevrolet    c1500 suburban 2wd   5.3  2008     8 auto(l4)   r        14    20 r     suv
 3 dodge        caravan 2wd          2.4  1999     4 auto(l3)   f        18    24 r     minivan
 4 ford         expedition 2wd       4.6  1999     8 auto(l4)   r        11    17 r     suv
 5 honda        civic                1.6  1999     4 manual(m5) f        28    33 r     subcompact
 6 hyundai      sonata               2.4  1999     4 auto(l4)   f        18    26 r     midsize
 7 jeep         grand cherokee 4wd   3    2008     6 auto(l5)   4        17    22 d     suv
 8 land rover   range rover          4    1999     8 auto(l4)   4        11    15 p     suv
 9 lincoln      navigator 2wd        5.4  1999     8 auto(l4)   r        11    17 r     suv
10 mercury      mountaineer 4wd      4    1999     6 auto(l5)   4        14    17 r     suv
11 nissan       altima               2.4  1999     4 manual(m5) f        21    29 r     compact
12 pontiac      grand prix           3.1  1999     6 auto(l4)   f        18    26 r     midsize
13 subaru       forester awd         2.5  1999     4 manual(m5) 4        18    25 r     suv
14 toyota       4runner 4wd          2.7  1999     4 manual(m5) 4        15    20 r     suv
15 volkswagen   gti                  2    1999     4 manual(m5) f        21    29 r     compact

Notez l'argument .keep_all = TRUE. Cet argument indique à la fonction que toutes les colonnes des données mpg doivent être conservées après l'application de la fonction distinct(). Si cet argument n'est pas spécifié, toutes les observations uniques sont conservées et les autres variables sont éliminées (à l'exception de la variable utilisée pour identifier les lignes uniques).

Identifions maintenant toutes les lignes (observations) uniques de chaque catégorie de véhicule (class) au sein de chaque constructeur (manufacturer) :

mpg |>
  distinct(manufacturer, class, .keep_all = TRUE)
## A tibble: 32 × 11
   manufacturer model              displ  year   cyl trans      drv     cty   hwy fl    class
   <chr>        <chr>              <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>
 1 audi         a4                   1.8  1999     4 auto(l5)   f        18    29 p     compact
 2 audi         a6 quattro           2.8  1999     6 auto(l5)   4        15    24 p     midsize
 3 chevrolet    c1500 suburban 2wd   5.3  2008     8 auto(l4)   r        14    20 r     suv
 4 chevrolet    corvette             5.7  1999     8 manual(m6) r        16    26 p     2seater
 5 chevrolet    malibu               2.4  1999     4 auto(l4)   f        19    27 r     midsize
 6 dodge        caravan 2wd          2.4  1999     4 auto(l3)   f        18    24 r     minivan
 7 dodge        dakota pickup 4wd    3.7  2008     6 manual(m6) 4        15    19 r     pickup
 8 dodge        durango 4wd          3.9  1999     6 auto(l4)   4        13    17 r     suv
 9 ford         expedition 2wd       4.6  1999     8 auto(l4)   r        11    17 r     suv
10 ford         f150 pickup 4wd      4.2  1999     6 auto(l4)   4        14    17 r     pickup
11 ford         mustang              3.8  1999     6 manual(m5) r        18    26 r     subcompact
12 honda        civic                1.6  1999     4 manual(m5) f        28    33 r     subcompact
13 hyundai      sonata               2.4  1999     4 auto(l4)   f        18    26 r     midsize
14 hyundai      tiburon              2    1999     4 auto(l4)   f        19    26 r     subcompact
15 jeep         grand cherokee 4wd   3    2008     6 auto(l5)   4        17    22 d     suv
16 land rover   range rover          4    1999     8 auto(l4)   4        11    15 p     suv
17 lincoln      navigator 2wd        5.4  1999     8 auto(l4)   r        11    17 r     suv
18 mercury      mountaineer 4wd      4    1999     6 auto(l5)   4        14    17 r     suv
19 nissan       altima               2.4  1999     4 manual(m5) f        21    29 r     compact
20 nissan       altima               2.5  2008     4 auto(av)   f        23    31 r     midsize
21 nissan       pathfinder 4wd       3.3  1999     6 auto(l4)   4        14    17 r     suv
22 pontiac      grand prix           3.1  1999     6 auto(l4)   f        18    26 r     midsize
23 subaru       forester awd         2.5  1999     4 manual(m5) 4        18    25 r     suv
24 subaru       impreza awd          2.2  1999     4 auto(l4)   4        21    26 r     subcompact
25 subaru       impreza awd          2.5  2008     4 auto(s4)   4        20    25 p     compact
26 toyota       4runner 4wd          2.7  1999     4 manual(m5) 4        15    20 r     suv
27 toyota       camry                2.2  1999     4 manual(m5) f        21    29 r     midsize
28 toyota       camry solara         2.2  1999     4 auto(l4)   f        21    27 r     compact
29 toyota       toyota tacoma 4wd    2.7  1999     4 manual(m5) 4        15    20 r     pickup
30 volkswagen   gti                  2    1999     4 manual(m5) f        21    29 r     compact
31 volkswagen   new beetle           1.9  1999     4 manual(m5) f        35    44 d     subcompact
32 volkswagen   passat               1.8  1999     4 manual(m5) f        21    29 p     midsize

Vous pouvez combiner distinct() avec group_by().