These scoped variants of select() and rename() operate on a selection of variables. The semantics of these verbs have subtle but important differences:

  • Selection drops variables that are not in the selection while renaming retains them.

  • The renaming function is optional for selection but not for renaming.

The _if and _at variants always retain grouping variables for grouped data frames.

select_all(.tbl, .funs = list(), ...)

rename_all(.tbl, .funs = list(), ...)

select_if(.tbl, .predicate, .funs = list(), ...)

rename_if(.tbl, .predicate, .funs = list(), ...)

select_at(.tbl, .vars, .funs = list(), ...)

rename_at(.tbl, .vars, .funs = list(), ...)

Arguments

.tbl

A tbl object.

.funs

A single expression quoted with funs() or within a quosure, a string naming a function, or a function.

...

Additional arguments for the function calls in .funs. These are evaluated only once, with tidy dots support.

.predicate

A predicate function to be applied to the columns or a logical vector. The variables for which .predicate is or returns TRUE are selected. This argument is passed to rlang::as_function() and thus supports quosure-style lambda functions and strings representing function names.

.vars

A list of columns generated by vars(), a character vector of column names, a numeric vector of column positions, or NULL.

Examples

# Supply a renaming function: select_all(mtcars, toupper)
#> MPG CYL DISP HP DRAT WT QSEC VS AM 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 #> [ reached getOption("max.print") -- omitted 23 rows ]
select_all(mtcars, "toupper")
#> MPG CYL DISP HP DRAT WT QSEC VS AM 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 #> [ reached getOption("max.print") -- omitted 23 rows ]
select_all(mtcars, funs(toupper(.)))
#> MPG CYL DISP HP DRAT WT QSEC VS AM 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 #> [ reached getOption("max.print") -- omitted 23 rows ]
# Selection drops unselected variables: is_whole <- function(x) all(floor(x) == x) select_if(mtcars, is_whole, toupper)
#> CYL HP VS AM GEAR CARB #> Mazda RX4 6 110 0 1 4 4 #> Mazda RX4 Wag 6 110 0 1 4 4 #> Datsun 710 4 93 1 1 4 1 #> Hornet 4 Drive 6 110 1 0 3 1 #> Hornet Sportabout 8 175 0 0 3 2 #> Valiant 6 105 1 0 3 1 #> Duster 360 8 245 0 0 3 4 #> Merc 240D 4 62 1 0 4 2 #> Merc 230 4 95 1 0 4 2 #> Merc 280 6 123 1 0 4 4 #> Merc 280C 6 123 1 0 4 4 #> Merc 450SE 8 180 0 0 3 3 #> Merc 450SL 8 180 0 0 3 3 #> Merc 450SLC 8 180 0 0 3 3 #> Cadillac Fleetwood 8 205 0 0 3 4 #> Lincoln Continental 8 215 0 0 3 4 #> [ reached getOption("max.print") -- omitted 16 rows ]
# But renaming retains them: rename_if(mtcars, is_whole, toupper)
#> mpg CYL disp HP drat wt qsec VS AM 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 #> [ reached getOption("max.print") -- omitted 23 rows ]
# The renaming function is optional for selection: select_if(mtcars, is_whole)
#> cyl hp vs am gear carb #> Mazda RX4 6 110 0 1 4 4 #> Mazda RX4 Wag 6 110 0 1 4 4 #> Datsun 710 4 93 1 1 4 1 #> Hornet 4 Drive 6 110 1 0 3 1 #> Hornet Sportabout 8 175 0 0 3 2 #> Valiant 6 105 1 0 3 1 #> Duster 360 8 245 0 0 3 4 #> Merc 240D 4 62 1 0 4 2 #> Merc 230 4 95 1 0 4 2 #> Merc 280 6 123 1 0 4 4 #> Merc 280C 6 123 1 0 4 4 #> Merc 450SE 8 180 0 0 3 3 #> Merc 450SL 8 180 0 0 3 3 #> Merc 450SLC 8 180 0 0 3 3 #> Cadillac Fleetwood 8 205 0 0 3 4 #> Lincoln Continental 8 215 0 0 3 4 #> [ reached getOption("max.print") -- omitted 16 rows ]