select: Select/rename variables by name

Description Usage Arguments Value Useful functions Scoped selection and renaming Tidy data See Also Examples

View source: R/manip.r

Description

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

Usage

1
2
3

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

 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
40
41
42
43
44
45
46
47
48
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 <- 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 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)

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

# 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
# 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
# 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
# A tibble: 10 x 8
      V4    V7     V1     V9    V8      V5     V2     V6
   <dbl> <dbl>  <dbl>  <dbl> <dbl>   <dbl>  <dbl>  <dbl>
 1 0.697 0.172 0.552  0.0822 0.207 0.369   0.842  0.563 
 2 0.483 0.227 0.992  0.944  0.301 0.118   0.392  0.795 
 3 0.299 0.944 0.946  0.916  0.263 0.614   0.524  0.815 
 4 0.937 0.997 0.574  0.647  0.806 0.00613 0.968  0.221 
 5 0.488 0.730 0.651  0.968  0.438 0.726   0.234  0.773 
 6 0.773 0.916 0.641  0.947  0.277 0.385   0.0905 0.316 
 7 0.133 0.795 0.684  0.439  0.968 0.788   0.226  0.581 
 8 0.912 0.778 0.840  0.0811 0.365 0.667   0.123  0.391 
 9 0.532 0.803 0.0221 0.787  0.320 0.338   0.690  0.0567
10 0.236 0.161 0.921  0.0122 0.864 0.618   0.889  0.764 
# A tibble: 10 x 3
      V4      V5     V6
   <dbl>   <dbl>  <dbl>
 1 0.697 0.369   0.563 
 2 0.483 0.118   0.795 
 3 0.299 0.614   0.815 
 4 0.937 0.00613 0.221 
 5 0.488 0.726   0.773 
 6 0.773 0.385   0.316 
 7 0.133 0.788   0.581 
 8 0.912 0.667   0.391 
 9 0.532 0.338   0.0567
10 0.236 0.618   0.764 
# 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
                    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
                     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
# 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
# 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
                    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
                    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
                     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

dplyr documentation built on Nov. 10, 2018, 9:04 a.m.