Démarrer avec dplyr
Sur cette page
Installation ou configuration
Pour installer dplyr
, tapez simplement dans la console R.
install.packages("dplyr")
Et puis pour charger dplyr
, tapez
library("dplyr")
Il est également possible d’installer la dernière version de développement depuis Github avec :
if (packageVersion("devtools") < 1.6) {
install.packages("devtools")
}
devtools::install_github("hadley/lazyeval")
devtools::install_github("hadley/dplyr")
Vous pouvez installer les packages de données utilisés dans la plupart des exemples : install.packages(c("nycflights13", "Lahman"))
.
Verbes de base
library(dplyr)
library(nycflights13)
Il existe plusieurs verbes les plus couramment utilisés dans dplyr
pour modifier les ensembles de données.
sélectionner
Sélectionnez les variables tailnum
, type
, model
dans le dataframe planes
:
select(planes, tailnum, type, model)
## # A tibble: 3,322 × 3
## tailnum type model
## <chr> <chr> <chr>
## 1 N10156 Fixed wing multi engine EMB-145XR
## 2 N102UW Fixed wing multi engine A320-214
## 3 N103US Fixed wing multi engine A320-214
## 4 N104UW Fixed wing multi engine A320-214
## 5 N10575 Fixed wing multi engine EMB-145LR
## 6 N105UW Fixed wing multi engine A320-214
## 7 N107US Fixed wing multi engine A320-214
## 8 N108UW Fixed wing multi engine A320-214
## 9 N109UW Fixed wing multi engine A320-214
## 10 N110UW Fixed wing multi engine A320-214
## # ... with 3,312 more rows
Réécrivez la déclaration ci-dessus avec l’opérateur forward-pipe (%>%
) du package magritr :
planes %>% select(tailnum, type, model)
## # A tibble: 3,322 × 3
## tailnum type model
## <chr> <chr> <chr>
## 1 N10156 Fixed wing multi engine EMB-145XR
## 2 N102UW Fixed wing multi engine A320-214
## 3 N103US Fixed wing multi engine A320-214
## 4 N104UW Fixed wing multi engine A320-214
## 5 N10575 Fixed wing multi engine EMB-145LR
## 6 N105UW Fixed wing multi engine A320-214
## 7 N107US Fixed wing multi engine A320-214
## 8 N108UW Fixed wing multi engine A320-214
## 9 N109UW Fixed wing multi engine A320-214
## 10 N110UW Fixed wing multi engine A320-214
## # ... with 3,312 more rows
filtre
“filtrer” les lignes en fonction des critères.
Renvoie un ensemble de données où manufacturer
est “EMBRAER”:
planes %>% filter(manufacturer == "EMBRAER")
## # A tibble: 299 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 2 N10575 2002 Fixed wing multi engine EMBRAER EMB-145LR 2
## 3 N11106 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 4 N11107 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 5 N11109 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 6 N11113 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 7 N11119 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 8 N11121 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 9 N11127 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 10 N11137 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## # ... with 289 more rows, and 3 more variables: seats <int>, speed <int>,
## # engine <chr>
Renvoie un ensemble de données où manufacturer
est “EMBRAER” et model
est “EMB-145XR”:
planes %>%
filter(manufacturer == "EMBRAER", model == "EMB-145XR")
## # A tibble: 104 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 2 N11106 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 3 N11107 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 4 N11109 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 5 N11113 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 6 N11119 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 7 N11121 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 8 N11127 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 9 N11137 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 10 N11140 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## # ... with 94 more rows, and 3 more variables: seats <int>, speed <int>,
## # engine <chr>
L’instruction ci-dessus revient à écrire une condition “ET”.
planes %>% filter(manufacturer == "EMBRAER" & model == "EMB-145XR")
## # A tibble: 104 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 2 N11106 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 3 N11107 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 4 N11109 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 5 N11113 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 6 N11119 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 7 N11121 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 8 N11127 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 9 N11137 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 10 N11140 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## # ... with 94 more rows, and 3 more variables: seats <int>, speed <int>,
## # engine <chr>
Utilisez le caractère pipe (|) pour les conditions “OR”:
planes %>% filter(manufacturer == "EMBRAER" | model == "EMB-145XR")
## # A tibble: 299 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 2 N10575 2002 Fixed wing multi engine EMBRAER EMB-145LR 2
## 3 N11106 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 4 N11107 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 5 N11109 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 6 N11113 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 7 N11119 2002 Fixed wing multi engine EMBRAER EMB-145XR 2
## 8 N11121 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 9 N11127 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## 10 N11137 2003 Fixed wing multi engine EMBRAER EMB-145XR 2
## # ... with 289 more rows, and 3 more variables: seats <int>, speed <int>,
## # engine <chr>
Utilisez grepl
en combinaison avec filter
pour les conditions de correspondance de modèle.
planes %>% filter(grepl("^172.", model))
## # A tibble: 3 × 9
## tailnum year type manufacturer model engines seats
## <chr> <int> <chr> <chr> <chr> <int> <int>
## 1 N378AA 1963 Fixed wing single engine CESSNA 172E 1 4
## 2 N621AA 1975 Fixed wing single engine CESSNA 172M 1 4
## 3 N737MQ 1977 Fixed wing single engine CESSNA 172N 1 4
## # ... with 2 more variables: speed <int>, engine <chr>
entre
Renvoie toutes les lignes où “année” est “entre” 2004 et 2005 :
planes %>% filter(between(year, 2004, 2005))
## # A tibble: 354 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 2 N11155 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 3 N11164 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 4 N11165 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 5 N11176 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 6 N11181 2005 Fixed wing multi engine EMBRAER EMB-145XR 2
## 7 N11184 2005 Fixed wing multi engine EMBRAER EMB-145XR 2
## 8 N11187 2005 Fixed wing multi engine EMBRAER EMB-145XR 2
## 9 N11189 2005 Fixed wing multi engine EMBRAER EMB-145XR 2
## 10 N11191 2005 Fixed wing multi engine EMBRAER EMB-145XR 2
## # ... with 344 more rows, and 3 more variables: seats <int>, speed <int>,
## # engine <chr>
tranche
slice
renvoie uniquement les lignes par l’index donné.
Renvoie les cinq premières lignes de données (identiques à la fonction de base “head”) :
planes %>% slice(1:5)
## # A tibble: 5 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 2 N102UW 1998 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2
## 3 N103US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2
## 4 N104UW 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2
## 5 N10575 2002 Fixed wing multi engine EMBRAER EMB-145LR 2
## # ... with 3 more variables: seats <int>, speed <int>, engine <chr>
Renvoie les 1ère, 3ème et 5ème lignes de données :
planes %>% slice(c(1, 3, 5))
## # A tibble: 3 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N10156 2004 Fixed wing multi engine EMBRAER EMB-145XR 2
## 2 N103US 1999 Fixed wing multi engine AIRBUS INDUSTRIE A320-214 2
## 3 N10575 2002 Fixed wing multi engine EMBRAER EMB-145LR 2
## # ... with 3 more variables: seats <int>, speed <int>, engine <chr>
Renvoie la première et la dernière ligne :
planes %>% slice(c(1, nrow(planes)))
## # A tibble: 2 × 9
## tailnum year type manufacturer
## <chr> <int> <chr> <chr>
## 1 N10156 2004 Fixed wing multi engine EMBRAER
## 2 N999DN 1992 Fixed wing multi engine MCDONNELL DOUGLAS CORPORATION
## # ... with 5 more variables: model <chr>, engines <int>, seats <int>,
## # speed <int>, engine <chr>
subir une mutation
mutate
peut ajouter de nouvelles variables ou modifier des variables existantes.
Ajoutez une variable factice, engine.dummy
avec une valeur par défaut de 0 :
planes %>%
mutate(engine.dummy = 0) %>%
select(engine, engine.dummy)
## # A tibble: 3,322 × 2
## engine engine.dummy
## <chr> <dbl>
## 1 Turbo-fan 0
## 2 Turbo-fan 0
## 3 Turbo-fan 0
## 4 Turbo-fan 0
## 5 Turbo-fan 0
## 6 Turbo-fan 0
## 7 Turbo-fan 0
## 8 Turbo-fan 0
## 9 Turbo-fan 0
## 10 Turbo-fan 0
## # ... with 3,312 more rows
En utilisant dplyr::if_else
, ajoutez engine.dummy
défini sur 1 si engine
== “Turbo-fan”, sinon définissez engine.dummy
sur 0 :
planes %>%
mutate(engine.dummy = if_else(engine == "Turbo-fan", 1, 0)) %>%
select(engine, engine.dummy)
## # A tibble: 3,322 × 2
## engine engine.dummy
## <chr> <dbl>
## 1 Turbo-fan 1
## 2 Turbo-fan 1
## 3 Turbo-fan 1
## 4 Turbo-fan 1
## 5 Turbo-fan 1
## 6 Turbo-fan 1
## 7 Turbo-fan 1
## 8 Turbo-fan 1
## 9 Turbo-fan 1
## 10 Turbo-fan 1
## # ... with 3,312 more rows
Convertissez planes$engine
en un facteur.
planes %>%
mutate(engine = as.factor(engine)) %>%
select(engine)
## # A tibble: 3,322 × 1
## engine
## <fctr>
## 1 Turbo-fan
## 2 Turbo-fan
## 3 Turbo-fan
## 4 Turbo-fan
## 5 Turbo-fan
## 6 Turbo-fan
## 7 Turbo-fan
## 8 Turbo-fan
## 9 Turbo-fan
## 10 Turbo-fan
## # ... with 3,312 more rows
organiser
Utilisez arrange
pour trier votre dataframe.
Organisez les “avions” par “année”:
planes %>% arrange(year)
## # A tibble: 3,322 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N381AA 1956 Fixed wing multi engine DOUGLAS DC-7BF 4
## 2 N201AA 1959 Fixed wing single engine CESSNA 150 1
## 3 N567AA 1959 Fixed wing single engine DEHAVILLAND OTTER DHC-3 1
## 4 N378AA 1963 Fixed wing single engine CESSNA 172E 1
## 5 N575AA 1963 Fixed wing single engine CESSNA 210-5(205) 1
## 6 N14629 1965 Fixed wing multi engine BOEING 737-524 2
## 7 N615AA 1967 Fixed wing multi engine BEECH 65-A90 2
## 8 N425AA 1968 Fixed wing single engine PIPER PA-28-180 1
## 9 N383AA 1972 Fixed wing multi engine BEECH E-90 2
## 10 N364AA 1973 Fixed wing multi engine CESSNA 310Q 2
## # ... with 3,312 more rows, and 3 more variables: seats <int>,
## # speed <int>, engine <chr>
arrange
planes
by year
desc
:
planes %>% arrange(desc(year))
## # A tibble: 3,322 × 9
## tailnum year type manufacturer model engines
## <chr> <int> <chr> <chr> <chr> <int>
## 1 N150UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 2 N151UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 3 N152UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 4 N153UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 5 N154UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 6 N155UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 7 N156UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 8 N157UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 9 N198UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## 10 N199UW 2013 Fixed wing multi engine AIRBUS A321-211 2
## # ... with 3,312 more rows, and 3 more variables: seats <int>,
## # speed <int>, engine <chr>
par groupe
group_by
vous permet d’effectuer des opérations sur une trame de données par sous-ensembles sans extraire le sous-ensemble.
df <- planes %>% group_by(manufacturer, model)
La trame de données renvoyée peut ne pas apparaître groupée. Cependant, la “classe” et les “attributs” de la trame de données le confirmeront.
class(df)
## [1] "grouped_df" "tbl_df" "tbl" "data.frame"
attributes(df)$vars
## [[1]]
## manufacturer
##
## [[2]]
## model
head(attributes(df)$labels, n = 5L)
## manufacturer model
## 1 AGUSTA SPA A109E
## 2 AIRBUS A319-112
## 3 AIRBUS A319-114
## 4 AIRBUS A319-115
## 5 AIRBUS A319-131
Si vous souhaitez ajouter des éléments de regroupement à la trame de données sans supprimer les éléments de regroupement existants, utilisez le paramètre “add” défini sur TRUE (défini sur FALSE par défaut) :
df <- df %>% group_by(type, year, add = TRUE)
class(df)
## [1] "grouped_df" "tbl_df" "tbl" "data.frame"
attributes(df)$vars
## [[1]]
## manufacturer
##
## [[2]]
## model
##
## [[3]]
## type
##
## [[4]]
## year
head(attributes(df)$labels, n = 5L)
## manufacturer model type year
## 1 AGUSTA SPA A109E Rotorcraft 2001
## 2 AIRBUS A319-112 Fixed wing multi engine 2002
## 3 AIRBUS A319-112 Fixed wing multi engine 2005
## 4 AIRBUS A319-112 Fixed wing multi engine 2006
## 5 AIRBUS A319-112 Fixed wing multi engine 2007
Si vous souhaitez supprimer le groupement, utilisez ungroup
.
df <- df %>% ungroup()
class(df)
## [1] "tbl_df" "tbl" "data.frame"
attributes(df)$vars
## NULL
attributes(df)$labels
## NULL
résumer
summarise
est utilisé pour effectuer des calculs sur un ensemble de données soit dans son ensemble, soit par groupes.
Trouver le nombre “moyen” de “sièges” par “fabricant” ?
planes %>%
group_by(manufacturer) %>%
summarise(Mean = mean(seats))
## # A tibble: 35 × 2
## manufacturer Mean
## <chr> <dbl>
## 1 AGUSTA SPA 8.0000
## 2 AIRBUS 221.2024
## 3 AIRBUS INDUSTRIE 187.4025
## 4 AMERICAN AIRCRAFT INC 2.0000
## 5 AVIAT AIRCRAFT INC 2.0000
## 6 AVIONS MARCEL DASSAULT 12.0000
## 7 BARKER JACK L 2.0000
## 8 BEECH 9.5000
## 9 BELL 8.0000
## 10 BOEING 175.1877
## # ... with 25 more rows
summarise
ne renverra pas les variables qui ne sont pas explicitement groupées ou incluses dans les fonctions de résumé. Si vous voulez ajouter une autre variable, vous devez la passer comme prédicat à group_by
ou summarise
.
planes %>%
group_by(year, manufacturer) %>%
summarise(Mean = mean(seats))
## Source: local data frame [164 x 3]
## Groups: year [?]
##
## year manufacturer Mean
## <int> <chr> <dbl>
## 1 1956 DOUGLAS 102
## 2 1959 CESSNA 2
## 3 1959 DEHAVILLAND 16
## 4 1963 CESSNA 5
## 5 1965 BOEING 149
## 6 1967 BEECH 9
## 7 1968 PIPER 4
## 8 1972 BEECH 10
## 9 1973 CESSNA 6
## 10 1974 CANADAIR LTD 2
## # ... with 154 more rows
Renommer
renommer
une variable :
planes %>%
rename(Mfr = manufacturer) %>%
names()
## [1] "tailnum" "year" "type" "Mfr" "model" "engines" "seats"
## [8] "speed" "engine"
Fonctions d’assistance
Les fonctions d’assistance sont utilisées conjointement avec select
pour identifier les variables à renvoyer. Sauf indication contraire, ces fonctions attendent une chaîne comme premier paramètre match
. Passer un vecteur ou un autre objet générera une erreur.
library(dplyr)
library(nycflights13)
commence avec
starts_with
nous permet d’identifier les variables dont le nom commence par une chaîne.
Renvoie toutes les variables qui commencent par la lettre “e”.
planes %>% select(starts_with("e"))
## # A tibble: 3,322 × 2
## engines engine
## <int> <chr>
## 1 2 Turbo-fan
## 2 2 Turbo-fan
## 3 2 Turbo-fan
## 4 2 Turbo-fan
## 5 2 Turbo-fan
## 6 2 Turbo-fan
## 7 2 Turbo-fan
## 8 2 Turbo-fan
## 9 2 Turbo-fan
## 10 2 Turbo-fan
## # ... with 3,312 more rows
Définissez le paramètre ignore.case
sur FALSE pour une casse stricte.
planes %>% select(starts_with("E", ignore.case = FALSE))
## # A tibble: 3,322 × 0
se termine par
Renvoie toutes les variables qui se terminent par la lettre “e”.
planes %>% select(ends_with("e"))
## # A tibble: 3,322 × 2
## type engine
## <chr> <chr>
## 1 Fixed wing multi engine Turbo-fan
## 2 Fixed wing multi engine Turbo-fan
## 3 Fixed wing multi engine Turbo-fan
## 4 Fixed wing multi engine Turbo-fan
## 5 Fixed wing multi engine Turbo-fan
## 6 Fixed wing multi engine Turbo-fan
## 7 Fixed wing multi engine Turbo-fan
## 8 Fixed wing multi engine Turbo-fan
## 9 Fixed wing multi engine Turbo-fan
## 10 Fixed wing multi engine Turbo-fan
## # ... with 3,312 more rows
Définissez le paramètre ignore.case
sur FALSE pour une casse stricte.
planes %>% select(ends_with("E", ignore.case = FALSE))
## # A tibble: 3,322 × 0
contient
contains
vous permet de trouver toutes les variables contenant une chaîne donnée.
planes %>% select(contains("ea"))
## # A tibble: 3,322 × 2
## year seats
## <int> <int>
## 1 2004 55
## 2 1998 182
## 3 1999 182
## 4 1999 182
## 5 2002 55
## 6 1999 182
## 7 1999 182
## 8 1999 182
## 9 1999 182
## 10 1999 182
## # ... with 3,312 more rows
Définissez le paramètre ignore.case
sur FALSE pour une casse stricte.
planes %>% select(contains("EA", ignore.case = FALSE))
## # A tibble: 3,322 × 0
allumettes
matches
est la seule fonction d’assistance qui permet l’utilisation d’expressions régulières.
Renvoie toutes les variables avec un nom d’au moins six caractères alpha :
planes %>% select(matches("[[:alpha:]]{6,}"))
## # A tibble: 3,322 × 4
## tailnum manufacturer engines engine
## <chr> <chr> <int> <chr>
## 1 N10156 EMBRAER 2 Turbo-fan
## 2 N102UW AIRBUS INDUSTRIE 2 Turbo-fan
## 3 N103US AIRBUS INDUSTRIE 2 Turbo-fan
## 4 N104UW AIRBUS INDUSTRIE 2 Turbo-fan
## 5 N10575 EMBRAER 2 Turbo-fan
## 6 N105UW AIRBUS INDUSTRIE 2 Turbo-fan
## 7 N107US AIRBUS INDUSTRIE 2 Turbo-fan
## 8 N108UW AIRBUS INDUSTRIE 2 Turbo-fan
## 9 N109UW AIRBUS INDUSTRIE 2 Turbo-fan
## 10 N110UW AIRBUS INDUSTRIE 2 Turbo-fan
## # ... with 3,312 more rows
Définissez le paramètre ignore.case
sur FALSE pour une casse stricte.
num_range
Pour cet exemple, je vais générer une trame de données factice avec des valeurs aléatoires et des noms de variables séquentiels.
set.seed(1)
df <- data.frame(x1 = runif(10),
x2 = runif(10),
x3 = runif(10),
x4 = runif(10),
x5 = runif(10))
num_range
peut être utilisé pour sélectionner une plage de variables avec un prefix
cohérent.
Sélectionnez les variables 2:4 de df
:
df %>% select(num_range('x', range = 2:4))
## x2 x3 x4
## 1 0.2059746 0.93470523 0.4820801
## 2 0.1765568 0.21214252 0.5995658
## 3 0.6870228 0.65167377 0.4935413
## 4 0.3841037 0.12555510 0.1862176
## 5 0.7698414 0.26722067 0.8273733
## 6 0.4976992 0.38611409 0.6684667
## 7 0.7176185 0.01339033 0.7942399
## 8 0.9919061 0.38238796 0.1079436
## 9 0.3800352 0.86969085 0.7237109
## 10 0.7774452 0.34034900 0.4112744
un des
one_of
peut prendre un vecteur comme paramètre match
et renvoie chaque variable.
planes %>% select(one_of(c("tailnum", "model")))
## # A tibble: 3,322 × 2
## tailnum model
## <chr> <chr>
## 1 N10156 EMB-145XR
## 2 N102UW A320-214
## 3 N103US A320-214
## 4 N104UW A320-214
## 5 N10575 EMB-145LR
## 6 N105UW A320-214
## 7 N107US A320-214
## 8 N108UW A320-214
## 9 N109UW A320-214
## 10 N110UW A320-214
## # ... with 3,312 more rows
tout
everything
peut être utilisé pour repositionner les variables dans le dataframe.
Faites de “fabricant” la première variable suivie de toutes les variables restantes.
planes %>% select(manufacturer, everything())
## # A tibble: 3,322 × 9
## manufacturer tailnum year type model
## <chr> <chr> <int> <chr> <chr>
## 1 EMBRAER N10156 2004 Fixed wing multi engine EMB-145XR
## 2 AIRBUS INDUSTRIE N102UW 1998 Fixed wing multi engine A320-214
## 3 AIRBUS INDUSTRIE N103US 1999 Fixed wing multi engine A320-214
## 4 AIRBUS INDUSTRIE N104UW 1999 Fixed wing multi engine A320-214
## 5 EMBRAER N10575 2002 Fixed wing multi engine EMB-145LR
## 6 AIRBUS INDUSTRIE N105UW 1999 Fixed wing multi engine A320-214
## 7 AIRBUS INDUSTRIE N107US 1999 Fixed wing multi engine A320-214
## 8 AIRBUS INDUSTRIE N108UW 1999 Fixed wing multi engine A320-214
## 9 AIRBUS INDUSTRIE N109UW 1999 Fixed wing multi engine A320-214
## 10 AIRBUS INDUSTRIE N110UW 1999 Fixed wing multi engine A320-214
## # ... with 3,312 more rows, and 4 more variables: engines <int>,
## # seats <int>, speed <int>, engine <chr>
Autres aides
Bien que les opérateurs :
et -
ne fassent pas partie du package dplyr
, nous pouvons toujours les utiliser pour identifier les variables à renvoyer.
### :
Définissez une plage inclusive de variables à renvoyer.
Renvoie toutes les variables de « année » à « fabricant » :
planes %>% select(year:manufacturer)
## # A tibble: 3,322 × 3
## year type manufacturer
## <int> <chr> <chr>
## 1 2004 Fixed wing multi engine EMBRAER
## 2 1998 Fixed wing multi engine AIRBUS INDUSTRIE
## 3 1999 Fixed wing multi engine AIRBUS INDUSTRIE
## 4 1999 Fixed wing multi engine AIRBUS INDUSTRIE
## 5 2002 Fixed wing multi engine EMBRAER
## 6 1999 Fixed wing multi engine AIRBUS INDUSTRIE
## 7 1999 Fixed wing multi engine AIRBUS INDUSTRIE
## 8 1999 Fixed wing multi engine AIRBUS INDUSTRIE
## 9 1999 Fixed wing multi engine AIRBUS INDUSTRIE
## 10 1999 Fixed wing multi engine AIRBUS INDUSTRIE
## # ... with 3,312 more rows
Renvoie plusieurs plages de variables :
planes %>% select(c(year:manufacturer, seats:engine))
## # A tibble: 3,322 × 6
## year type manufacturer seats speed engine
## <int> <chr> <chr> <int> <int> <chr>
## 1 2004 Fixed wing multi engine EMBRAER 55 NA Turbo-fan
## 2 1998 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## 3 1999 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## 4 1999 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## 5 2002 Fixed wing multi engine EMBRAER 55 NA Turbo-fan
## 6 1999 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## 7 1999 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## 8 1999 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## 9 1999 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## 10 1999 Fixed wing multi engine AIRBUS INDUSTRIE 182 NA Turbo-fan
## # ... with 3,312 more rows
-
L’opérateur -
supprimera une variable d’un jeu de résultats.
Renvoie toutes les variables à l’exception de type
:
planes %>% select(-type)
## # A tibble: 3,322 × 8
## tailnum year manufacturer model engines seats speed engine
## <chr> <int> <chr> <chr> <int> <int> <int> <chr>
## 1 N10156 2004 EMBRAER EMB-145XR 2 55 NA Turbo-fan
## 2 N102UW 1998 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## 3 N103US 1999 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## 4 N104UW 1999 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## 5 N10575 2002 EMBRAER EMB-145LR 2 55 NA Turbo-fan
## 6 N105UW 1999 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## 7 N107US 1999 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## 8 N108UW 1999 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## 9 N109UW 1999 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## 10 N110UW 1999 AIRBUS INDUSTRIE A320-214 2 182 NA Turbo-fan
## # ... with 3,312 more rows
Vous pouvez également transmettre un vecteur de noms de variables à exclure de votre jeu de résultats.
planes %>% select(-c(type, engines:engine))
## # A tibble: 3,322 × 4
## tailnum year manufacturer model
## <chr> <int> <chr> <chr>
## 1 N10156 2004 EMBRAER EMB-145XR
## 2 N102UW 1998 AIRBUS INDUSTRIE A320-214
## 3 N103US 1999 AIRBUS INDUSTRIE A320-214
## 4 N104UW 1999 AIRBUS INDUSTRIE A320-214
## 5 N10575 2002 EMBRAER EMB-145LR
## 6 N105UW 1999 AIRBUS INDUSTRIE A320-214
## 7 N107US 1999 AIRBUS INDUSTRIE A320-214
## 8 N108UW 1999 AIRBUS INDUSTRIE A320-214
## 9 N109UW 1999 AIRBUS INDUSTRIE A320-214
## 10 N110UW 1999 AIRBUS INDUSTRIE A320-214
## # ... with 3,312 more rows
Toute combinaison de fonctions d’assistance
Sélectionnez toutes les variables entre type
et speed
(inclus) et excluez manufacturer
.
planes %>% select(type:speed, -manufacturer)
## # A tibble: 3,322 × 5
## type model engines seats speed
## <chr> <chr> <int> <int> <int>
## 1 Fixed wing multi engine EMB-145XR 2 55 NA
## 2 Fixed wing multi engine A320-214 2 182 NA
## 3 Fixed wing multi engine A320-214 2 182 NA
## 4 Fixed wing multi engine A320-214 2 182 NA
## 5 Fixed wing multi engine EMB-145LR 2 55 NA
## 6 Fixed wing multi engine A320-214 2 182 NA
## 7 Fixed wing multi engine A320-214 2 182 NA
## 8 Fixed wing multi engine A320-214 2 182 NA
## 9 Fixed wing multi engine A320-214 2 182 NA
## 10 Fixed wing multi engine A320-214 2 182 NA
## # ... with 3,312 more rows
Modifiez l’instruction précédente pour exclure “fabricant” et “modèle”.
planes %>% select(type:speed, -c(manufacturer, model))
## # A tibble: 3,322 × 4
## type engines seats speed
## <chr> <int> <int> <int>
## 1 Fixed wing multi engine 2 55 NA
## 2 Fixed wing multi engine 2 182 NA
## 3 Fixed wing multi engine 2 182 NA
## 4 Fixed wing multi engine 2 182 NA
## 5 Fixed wing multi engine 2 55 NA
## 6 Fixed wing multi engine 2 182 NA
## 7 Fixed wing multi engine 2 182 NA
## 8 Fixed wing multi engine 2 182 NA
## 9 Fixed wing multi engine 2 182 NA
## 10 Fixed wing multi engine 2 182 NA
## # ... with 3,312 more rows
Vous pouvez utiliser la même fonction d’assistance plusieurs fois.
planes %>% select(starts_with("m"), starts_with("s"))
## # A tibble: 3,322 × 4
## manufacturer model seats speed
## <chr> <chr> <int> <int>
## 1 EMBRAER EMB-145XR 55 NA
## 2 AIRBUS INDUSTRIE A320-214 182 NA
## 3 AIRBUS INDUSTRIE A320-214 182 NA
## 4 AIRBUS INDUSTRIE A320-214 182 NA
## 5 EMBRAER EMB-145LR 55 NA
## 6 AIRBUS INDUSTRIE A320-214 182 NA
## 7 AIRBUS INDUSTRIE A320-214 182 NA
## 8 AIRBUS INDUSTRIE A320-214 182 NA
## 9 AIRBUS INDUSTRIE A320-214 182 NA
## 10 AIRBUS INDUSTRIE A320-214 182 NA
## # ... with 3,312 more rows
Vous pouvez utiliser plusieurs fonctions d’assistance ensemble :
planes %>% select(starts_with("m"), ends_with("l"))
## # A tibble: 3,322 × 2
## manufacturer model
## <chr> <chr>
## 1 EMBRAER EMB-145XR
## 2 AIRBUS INDUSTRIE A320-214
## 3 AIRBUS INDUSTRIE A320-214
## 4 AIRBUS INDUSTRIE A320-214
## 5 EMBRAER EMB-145LR
## 6 AIRBUS INDUSTRIE A320-214
## 7 AIRBUS INDUSTRIE A320-214
## 8 AIRBUS INDUSTRIE A320-214
## 9 AIRBUS INDUSTRIE A320-214
## 10 AIRBUS INDUSTRIE A320-214
## # ... with 3,312 more rows