select: Select/rename variables by name.

Description Usage Arguments Value Special functions See Also Examples

View source: R/manip.r

Description

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

Usage

1
2
3
4
5
6
7
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)))

sctyner/dplyr050 documentation built on May 17, 2019, 2:22 p.m.