# Here we show the usage for the basic selection operators. See the
# specific help pages to learn about helpers like [starts_with()].
# Select variables by name:
mtcars %>% select(mpg)
# Select multiple variables by separating them with commas. Note
# how the order of columns is determined by the order of inputs:
mtcars %>% select(disp, gear, am)
# Rename variables:
mtcars %>% select(MilesPerGallon = mpg, everything())
# The `:` operator selects a range of consecutive variables:
select(mtcars, mpg:cyl)
# The `!` operator negates a selection:
mtcars %>% select(!(mpg:qsec))
mtcars %>% select(!ends_with("p"))
# `&` and `|` take the intersection or the union of two selections:
iris %>% select(starts_with("Petal") & ends_with("Width"))
iris %>% select(starts_with("Petal") | ends_with("Width"))
# To take the difference between two selections, combine the `&` and
# `!` operators:
iris %>% select(starts_with("Petal") & !ends_with("Width"))
Run the code above in your browser using DataLab