```{r, include=FALSE}
# So the second library() call doesn't show messages
library(tidyverse)
```

Here we show the usage for the basic selection operators. See the
specific help pages to learn about helpers like [starts_with()].

The selection language can be used in functions like
`dplyr::select()`. Let's first attach
the tidyverse:

```{r}
library(tidyverse)

# For better printing
iris <- as_tibble(iris)
```

Select variables by name:

```{r}
starwars |> select(height)

iris |> select(Sepal.Length)
```

Select multiple variables by separating them with commas. Note how
the order of columns is determined by the order of inputs:

```{r}
starwars |> select(homeworld, height, mass)

iris |> select(Sepal.Length, Petal.Length)
```

If you use a named vector to select columns, the output will have
its columns renamed:

```{r}
selection <- c(
  new_homeworld = "homeworld",
  new_height = "height",
  new_mass = "mass"
)
starwars |> select(all_of(selection))
```

## Operators:

The `:` operator selects a range of consecutive variables:

```{r}
starwars |> select(name:mass)
```

The `!` operator negates a selection:

```{r}
starwars |> select(!(name:mass))

iris |> select(!c(Sepal.Length, Petal.Length))

iris |> select(!ends_with("Width"))
```

`&` and `|` take the intersection or the union of two selections:

```{r}
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:

```{r}
iris |> select(starts_with("Petal") & !ends_with("Width"))
```
