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

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.

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

To drop variables, use -.

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"))
#> # A tibble: 150 × 2 #> Petal.Length Petal.Width #> <dbl> <dbl> #> 1 1.4 0.2 #> 2 1.4 0.2 #> 3 1.3 0.2 #> 4 1.5 0.2 #> 5 1.4 0.2 #> 6 1.7 0.4 #> 7 1.4 0.3 #> 8 1.5 0.2 #> 9 1.4 0.2 #> 10 1.5 0.1 #> # ... with 140 more rows
select(iris, ends_with("Width"))
#> # A tibble: 150 × 2 #> Sepal.Width Petal.Width #> <dbl> <dbl> #> 1 3.5 0.2 #> 2 3.0 0.2 #> 3 3.2 0.2 #> 4 3.1 0.2 #> 5 3.6 0.2 #> 6 3.9 0.4 #> 7 3.4 0.3 #> 8 3.4 0.2 #> 9 2.9 0.2 #> 10 3.1 0.1 #> # ... with 140 more rows
# Move Species variable to the front select(iris, Species, everything())
#> # A tibble: 150 × 5 #> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> <fctr> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.1 3.5 1.4 0.2 #> 2 setosa 4.9 3.0 1.4 0.2 #> 3 setosa 4.7 3.2 1.3 0.2 #> 4 setosa 4.6 3.1 1.5 0.2 #> 5 setosa 5.0 3.6 1.4 0.2 #> 6 setosa 5.4 3.9 1.7 0.4 #> 7 setosa 4.6 3.4 1.4 0.3 #> 8 setosa 5.0 3.4 1.5 0.2 #> 9 setosa 4.4 2.9 1.4 0.2 #> 10 setosa 4.9 3.1 1.5 0.1 #> # ... with 140 more rows
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)
#> # A tibble: 10 × 8 #> V4 V7 V1 V9 V8 V5 V2 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 0.1849738 0.006950771 0.7169860 0.789947051 0.00250323 0.87959719 0.6477019 #> 2 0.5624441 0.390183412 0.8387365 0.004801874 0.84770109 0.37491998 0.0647610 #> 3 0.7307234 0.238652540 0.4678020 0.252908190 0.66004509 0.43416063 0.3374612 #> 4 0.6607431 0.384401472 0.7641271 0.563169963 0.29450061 0.80684989 0.5698746 #> 5 0.7317454 0.073094374 0.3994330 0.952278358 0.57010194 0.63560582 0.3383233 #> 6 0.6314629 0.766595044 0.6787983 0.685113066 0.83903090 0.06871643 0.4992534 #> 7 0.3519942 0.687511492 0.6900191 0.470041074 0.63062556 0.14484133 0.3084174 #> 8 0.4643854 0.841570623 0.6464228 0.289851467 0.49672970 0.29457026 0.8404281 #> 9 0.8910764 0.105449455 0.5148363 0.510124689 0.38952535 0.04144781 0.0651790 #> 10 0.5368121 0.340091229 0.9619246 0.750690085 0.73144890 0.11654699 0.4399482 #> # ... with 1 more variables: V6 <dbl>
select(df, num_range("V", 4:6))
#> # A tibble: 10 × 3 #> V4 V5 V6 #> <dbl> <dbl> <dbl> #> 1 0.1849738 0.87959719 0.26714756 #> 2 0.5624441 0.37491998 0.60840493 #> 3 0.7307234 0.43416063 0.77196657 #> 4 0.6607431 0.80684989 0.51975041 #> 5 0.7317454 0.63560582 0.86047988 #> 6 0.6314629 0.06871643 0.79302051 #> 7 0.3519942 0.14484133 0.78391104 #> 8 0.4643854 0.29457026 0.42372128 #> 9 0.8910764 0.04144781 0.01551256 #> 10 0.5368121 0.11654699 0.41620118
# Drop variables with - select(iris, -starts_with("Petal"))
#> # A tibble: 150 × 3 #> Sepal.Length Sepal.Width Species #> <dbl> <dbl> <fctr> #> 1 5.1 3.5 setosa #> 2 4.9 3.0 setosa #> 3 4.7 3.2 setosa #> 4 4.6 3.1 setosa #> 5 5.0 3.6 setosa #> 6 5.4 3.9 setosa #> 7 4.6 3.4 setosa #> 8 5.0 3.4 setosa #> 9 4.4 2.9 setosa #> 10 4.9 3.1 setosa #> # ... with 140 more rows
# Renaming ----------------------------------------- # * select() keeps only the variables you specify select(iris, petal_length = Petal.Length)
#> # A tibble: 150 × 1 #> petal_length #> <dbl> #> 1 1.4 #> 2 1.4 #> 3 1.3 #> 4 1.5 #> 5 1.4 #> 6 1.7 #> 7 1.4 #> 8 1.5 #> 9 1.4 #> 10 1.5 #> # ... with 140 more rows
# * rename() keeps all variables rename(iris, petal_length = Petal.Length)
#> # A tibble: 150 × 5 #> Sepal.Length Sepal.Width petal_length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <fctr> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> 7 4.6 3.4 1.4 0.3 setosa #> 8 5.0 3.4 1.5 0.2 setosa #> 9 4.4 2.9 1.4 0.2 setosa #> 10 4.9 3.1 1.5 0.1 setosa #> # ... with 140 more rows