Select/rename variables by name

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

select(.data, ...)

rename(.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.


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

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

  • select
  • rename
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 <-, 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)

# Unquoting ----------------------------------------

# Like all dplyr verbs, select() supports unquoting of symbols:
vars <- list(
  var1 = sym("cyl"),
  var2 = sym("am")
select(mtcars, !!!vars)

# For convenience it also supports strings and character
# vectors. This is unlike other verbs where strings would be
# ambiguous.
vars <- c(var1 = "cyl", var2 ="am")
select(mtcars, !!vars)
rename(mtcars, !!vars)
# }
Documentation reproduced from package dplyr, version 0.7.8, License: MIT + file LICENSE

Community examples at Jan 18, 2017 dplyr v0.5.0

## Column selection with `select()` `select()` is used to take a subset of a data frame by columns. Before you begin, take a look at the columns in the `warpbreaks` dataset, along with their types. Also note the use of [`as_data_frame()`]( to make the output prettier. ```{r} str(warpbreaks) # prints everything warpbreaks # selected output library(dplyr) (warpbreaks <- as_data_frame(warpbreaks)) ``` `select()` takes a data frame as its first argument, and the unquoted names of columns of that data frame in further arguments. Use of `as_data_frame()` is purely to reduce the output shown in the console. You do not need to call it. ```{r} library(dplyr) select(as_data_frame(warpbreaks), breaks, wool) ``` `dplyr` is more elegant if you use it with pipes. This example, is the same as the previous one, except that the data frame is piped to `select()`. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% select(breaks, wool) ``` ## Standard evaluation selection with `select_()` `select()` should only be used interactively (at the R console). For programmatic use of R (that is, most of the time), you should use the standard-evaluation version, `select_()`. This is described further in the (presumably ironically named) [Non-standard evaluation]( vignette. The preferred way of calling `select_()` is by passing formulae. That is, write the same thing as with `select()`, but prefix the names with a tilde (`~`). This example gives the same result as the previous one. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% select_(~ breaks, ~ wool) ``` You can also wrap the column names in calls to [`quote()`]( This is the same result again. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% select_(quote(breaks), quote(wool)) ``` You can also pass the column names as strings. Again, this is the same result. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% select_("breaks", "wool") ``` If you are using strings with `select_()`, it is usually easier to pass a single character vector rather than many strings in individual arguments. Pass the character vector to `.dots`. This is the same result yet again. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% select_(.dots = c("breaks", "wool")) ``` ## Renaming columns with `rename()` and `rename_()` Columns can be renamed by calling `rename()` with named arguments. All columns are kept. That is, columns that aren't mentioned are simply left untouched. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% rename() ``` As with column selection, it is better to use the standard evaluation version. Passing columns as formulae is prefered. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% rename_(number_of_breaks_per_loom = ~ breaks, type_of_wool = ~ wool) ``` `quote()`d column names are supported. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% rename_(number_of_breaks_per_loom = quote(breaks), type_of_wool = quote(wool)) ``` As are strings. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% rename_(number_of_breaks_per_loom = "breaks", type_of_wool = "wool") ``` Or a character vector passed to `.dots`. ```{r} library(dplyr) warpbreaks %>% as_data_frame %>% rename_(.dots = c(number_of_breaks_per_loom = "breaks", type_of_wool = "wool")) ``` ## Where to find help on selection helper functions They are documented on the [`?select_helpers`]( page.