dplyr (version 0.5.0)

select: Select/rename variables by name.

Description

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

Usage

select(.data, ...)

select_(.data, ..., .dots)

rename(.data, ...)

rename_(.data, ..., .dots)

Arguments

.data

A tbl. All main verbs are S3 generics and provide methods for tbl_df, tbl_dt and tbl_sql.

...

Comma separated list of unquoted expressions. You can treat variable names like they are positions. Use positive values to select variables; use negative values to drop variables.

.dots

Use select_() to do standard evaluation. See vignette("nse") for details

Value

An object of the same class as .data.

Data frame row names are silently dropped. To preserve, convert to an explicit variable.

Special 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 -. You can rename variables with named arguments.

See Also

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

Examples

Run this code
iris <- tbl_df(iris) # so it prints a little nicer
select(iris, starts_with("Petal"))
select(iris, ends_with("Width"))
select(iris, contains("etal"))
select(iris, matches(".t."))
select(iris, Petal.Length, Petal.Width)
vars <- c("Petal.Length", "Petal.Width")
select(iris, one_of(vars))

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
select(iris, -starts_with("Petal"))
select(iris, -ends_with("Width"))
select(iris, -contains("etal"))
select(iris, -matches(".t."))
select(iris, -Petal.Length, -Petal.Width)

# Rename variables:
# * select() keeps only the variables you specify
select(iris, petal_length = Petal.Length)
# Renaming multiple variables uses a prefix:
select(iris, petal = starts_with("Petal"))

# Reorder variables: keep the variable "Species" in the front
select(iris, Species, everything())

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

# Programming with select ---------------------------------------------------
select_(iris, ~Petal.Length)
select_(iris, "Petal.Length")
select_(iris, lazyeval::interp(~matches(x), x = ".t."))
select_(iris, quote(-Petal.Length), quote(-Petal.Width))
select_(iris, .dots = list(quote(-Petal.Length), quote(-Petal.Width)))

Run the code above in your browser using DataCamp Workspace