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

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"))
#> # A tibble: 150 x 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 x 2 #> Sepal.Width Petal.Width #> <dbl> <dbl> #> 1 3.5 0.2 #> 2 3 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 x 5 #> Species Sepal.Length Sepal.Width Petal.Length Petal.Width #> <fct> <dbl> <dbl> <dbl> <dbl> #> 1 setosa 5.1 3.5 1.4 0.2 #> 2 setosa 4.9 3 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 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 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
# Move Sepal.Length variable to back # first select all variables except Sepal.Length, then re select Sepal.Length select(iris, -Sepal.Length, Sepal.Length)
#> # A tibble: 150 x 5 #> Sepal.Width Petal.Length Petal.Width Species Sepal.Length #> <dbl> <dbl> <dbl> <fct> <dbl> #> 1 3.5 1.4 0.2 setosa 5.1 #> 2 3 1.4 0.2 setosa 4.9 #> 3 3.2 1.3 0.2 setosa 4.7 #> 4 3.1 1.5 0.2 setosa 4.6 #> 5 3.6 1.4 0.2 setosa 5 #> 6 3.9 1.7 0.4 setosa 5.4 #> 7 3.4 1.4 0.3 setosa 4.6 #> 8 3.4 1.5 0.2 setosa 5 #> 9 2.9 1.4 0.2 setosa 4.4 #> 10 3.1 1.5 0.1 setosa 4.9 #> # ... 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 x 8 #> V4 V7 V1 V9 V8 V5 V2 V6 #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 0.805 0.977 0.616 0.648 0.717 0.808 0.659 0.880 #> 2 0.120 0.110 0.603 0.0648 0.839 0.0111 0.758 0.253 #> 3 0.115 0.999 0.273 0.337 0.468 0.911 0.944 0.906 #> 4 0.0111 0.0962 0.575 0.570 0.764 0.926 0.839 0.0534 #> 5 0.871 0.0623 0.587 0.338 0.399 0.250 0.675 0.519 #> 6 0.771 0.807 0.765 0.499 0.679 0.921 0.884 0.924 #> 7 0.885 0.810 0.367 0.308 0.690 0.746 0.361 0.249 #> 8 0.862 0.812 0.453 0.840 0.646 0.469 0.0652 0.537 #> 9 0.647 0.202 0.850 0.0652 0.515 0.813 0.522 0.654 #> 10 0.0831 0.627 0.477 0.440 0.962 0.884 0.351 0.131
select(df, num_range("V", 4:6))
#> # A tibble: 10 x 3 #> V4 V5 V6 #> <dbl> <dbl> <dbl> #> 1 0.805 0.808 0.880 #> 2 0.120 0.0111 0.253 #> 3 0.115 0.911 0.906 #> 4 0.0111 0.926 0.0534 #> 5 0.871 0.250 0.519 #> 6 0.771 0.921 0.924 #> 7 0.885 0.746 0.249 #> 8 0.862 0.469 0.537 #> 9 0.647 0.813 0.654 #> 10 0.0831 0.884 0.131
# Drop variables with - select(iris, -starts_with("Petal"))
#> # A tibble: 150 x 3 #> Sepal.Length Sepal.Width Species #> <dbl> <dbl> <fct> #> 1 5.1 3.5 setosa #> 2 4.9 3 setosa #> 3 4.7 3.2 setosa #> 4 4.6 3.1 setosa #> 5 5 3.6 setosa #> 6 5.4 3.9 setosa #> 7 4.6 3.4 setosa #> 8 5 3.4 setosa #> 9 4.4 2.9 setosa #> 10 4.9 3.1 setosa #> # ... with 140 more rows
# The .data pronoun is available: select(mtcars, .data$cyl)
#> cyl #> Mazda RX4 6 #> Mazda RX4 Wag 6 #> Datsun 710 4 #> Hornet 4 Drive 6 #> Hornet Sportabout 8 #> Valiant 6 #> Duster 360 8 #> Merc 240D 4 #> Merc 230 4 #> Merc 280 6 #> Merc 280C 6 #> Merc 450SE 8 #> Merc 450SL 8 #> Merc 450SLC 8 #> Cadillac Fleetwood 8 #> Lincoln Continental 8 #> Chrysler Imperial 8 #> Fiat 128 4 #> Honda Civic 4 #> Toyota Corolla 4 #> Toyota Corona 4 #> Dodge Challenger 8 #> AMC Javelin 8 #> Camaro Z28 8 #> Pontiac Firebird 8 #> Fiat X1-9 4 #> Porsche 914-2 4 #> Lotus Europa 4 #> Ford Pantera L 8 #> Ferrari Dino 6 #> Maserati Bora 8 #> Volvo 142E 4
select(mtcars, .data$mpg : .data$disp)
#> mpg cyl disp #> Mazda RX4 21.0 6 160.0 #> Mazda RX4 Wag 21.0 6 160.0 #> Datsun 710 22.8 4 108.0 #> Hornet 4 Drive 21.4 6 258.0 #> Hornet Sportabout 18.7 8 360.0 #> Valiant 18.1 6 225.0 #> Duster 360 14.3 8 360.0 #> Merc 240D 24.4 4 146.7 #> Merc 230 22.8 4 140.8 #> Merc 280 19.2 6 167.6 #> Merc 280C 17.8 6 167.6 #> Merc 450SE 16.4 8 275.8 #> Merc 450SL 17.3 8 275.8 #> Merc 450SLC 15.2 8 275.8 #> Cadillac Fleetwood 10.4 8 472.0 #> Lincoln Continental 10.4 8 460.0 #> Chrysler Imperial 14.7 8 440.0 #> Fiat 128 32.4 4 78.7 #> Honda Civic 30.4 4 75.7 #> Toyota Corolla 33.9 4 71.1 #> Toyota Corona 21.5 4 120.1 #> Dodge Challenger 15.5 8 318.0 #> AMC Javelin 15.2 8 304.0 #> Camaro Z28 13.3 8 350.0 #> Pontiac Firebird 19.2 8 400.0 #> Fiat X1-9 27.3 4 79.0 #> Porsche 914-2 26.0 4 120.3 #> Lotus Europa 30.4 4 95.1 #> Ford Pantera L 15.8 8 351.0 #> Ferrari Dino 19.7 6 145.0 #> Maserati Bora 15.0 8 301.0 #> Volvo 142E 21.4 4 121.0
# 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)
#> # A tibble: 150 x 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 x 5 #> Sepal.Length Sepal.Width petal_length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <fct> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 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 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 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
# Unquoting ---------------------------------------- # Like all dplyr verbs, select() supports unquoting of symbols: vars <- list( var1 = sym("cyl"), var2 = sym("am") ) select(mtcars, !!!vars)
#> var1 var2 #> Mazda RX4 6 1 #> Mazda RX4 Wag 6 1 #> Datsun 710 4 1 #> Hornet 4 Drive 6 0 #> Hornet Sportabout 8 0 #> Valiant 6 0 #> Duster 360 8 0 #> Merc 240D 4 0 #> Merc 230 4 0 #> Merc 280 6 0 #> Merc 280C 6 0 #> Merc 450SE 8 0 #> Merc 450SL 8 0 #> Merc 450SLC 8 0 #> Cadillac Fleetwood 8 0 #> Lincoln Continental 8 0 #> Chrysler Imperial 8 0 #> Fiat 128 4 1 #> Honda Civic 4 1 #> Toyota Corolla 4 1 #> Toyota Corona 4 0 #> Dodge Challenger 8 0 #> AMC Javelin 8 0 #> Camaro Z28 8 0 #> Pontiac Firebird 8 0 #> Fiat X1-9 4 1 #> Porsche 914-2 4 1 #> Lotus Europa 4 1 #> Ford Pantera L 8 1 #> Ferrari Dino 6 1 #> Maserati Bora 8 1 #> Volvo 142E 4 1
# 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)
#> var1 var2 #> Mazda RX4 6 1 #> Mazda RX4 Wag 6 1 #> Datsun 710 4 1 #> Hornet 4 Drive 6 0 #> Hornet Sportabout 8 0 #> Valiant 6 0 #> Duster 360 8 0 #> Merc 240D 4 0 #> Merc 230 4 0 #> Merc 280 6 0 #> Merc 280C 6 0 #> Merc 450SE 8 0 #> Merc 450SL 8 0 #> Merc 450SLC 8 0 #> Cadillac Fleetwood 8 0 #> Lincoln Continental 8 0 #> Chrysler Imperial 8 0 #> Fiat 128 4 1 #> Honda Civic 4 1 #> Toyota Corolla 4 1 #> Toyota Corona 4 0 #> Dodge Challenger 8 0 #> AMC Javelin 8 0 #> Camaro Z28 8 0 #> Pontiac Firebird 8 0 #> Fiat X1-9 4 1 #> Porsche 914-2 4 1 #> Lotus Europa 4 1 #> Ford Pantera L 8 1 #> Ferrari Dino 6 1 #> Maserati Bora 8 1 #> Volvo 142E 4 1
rename(mtcars, !!vars)
#> mpg var1 disp hp drat wt qsec vs var2 gear carb #> Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 #> Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 #> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #> Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 #> Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 #> Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 #> Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 #> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 #> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 #> Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 #> Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 #> Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 #> Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 #> Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 #> Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 #> Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 #> Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 #> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 #> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 #> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 #> Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 #> Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 #> AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 #> Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 #> Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 #> Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 #> Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 #> Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 #> Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 #> Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 #> Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 #> Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2