select {dplyr} | R Documentation

## Select/rename variables by name

### Description

`select()` keeps only the variables you mention; `rename()` keeps all variables.

### Usage

    select(.data, ...)

    rename(.data, ...)

### Arguments

`.data` | A tbl. All main verbs are S3 generics and provide methods for `tbl_df()`, `dtplyr::tbl_dt()` and `dbplyr::tbl_dbi()`.

`...`

One or more unquoted expressions separated by commas. You can treat variable names like they are positions.

Positive values select variables; negative values to drop variables. If the first expression is negative, `select()` will automatically start with all variables.

Use named arguments to rename selected variables.

These arguments are automatically quoted and evaluated in a context where column names represent column positions. They support unquoting and splicing. See `vignette("programming")` for an introduction to these concepts.

### Value

An object of the same class as `.data`.

### Useful functions

As well as using existing functions like `:` and `c()`, there are a number of special functions that only work inside `select`

- `starts_with()`, `ends_with()`, `contains()`

- `matches()`

- `num_range()`

To drop variables, use `-`.

Note that except for `:`, `-` and `c()`, all complex expressions are evaluated outside the data frame context. This is to prevent accidental matching of data frame variables when you refer to variables from the calling context.

### Scoped selection and renaming

The three scoped variants of `select()` (`select_all()`, `select_if()` and `select_at()`) and the three variants of `rename()` (`rename_all()`, `rename_if()`, `rename_at()`) make it easy to apply a renaming function to a selection of variables.

### Tidy data

When applied to a data frame, row names are silently dropped. To preserve, convert to an explicit variable with `tibble::rownames_to_column()`.

### See Also

Other single table verbs: `arrange`, `filter`, `mutate`, `slice`, `summarise`

### Examples

    iris <- as_tibble(iris) # so it prints a little nicer
    select(iris, starts_with("Petal"))
    select(iris, ends_with("Width"))

    # Move Species variable to the front
    select(iris, Species, everything())

    df <- as.data.frame(matrix(runif(100), nrow = 10))
    df <- tbl_df(df[c(3, 4, 7, 1, 9, 8, 5, 2, 6, 10)])
    select(df, V4:V6)
    select(df, num_range("V", 4:6))

    # Drop variables with -
    select(iris, -starts_with("Petal"))

    # The .data pronoun is available:
    select(mtcars, .data$cyl)
    select(mtcars, .data$mpg : .data$disp)

    # However it isn't available within calls since those are evaluated
    # outside of the data context. This would fail if run:
    # select(mtcars, identical(.data$cyl))

    # Renaming -----------------------------------------
    # * select() keeps only the variables you specify
    select(iris, petal_length = Petal.Length)

    # * rename() keeps all variables
    rename(iris, petal_length = Petal.Length)

[Package dplyr version 0.7.4 Index]