Skip to content

[Superseded]

rename_if(), rename_at(), and rename_all() have been superseded by rename_with(). The matching select statements have been superseded by the combination of a select() + rename_with(). Any predicate functions passed as arguments to select() or rename_with() must be wrapped in where().

These functions were superseded because mutate_if() and friends were superseded by across(). select_if() and rename_if() already use tidy selection so they can't be replaced by across() and instead we need a new function.

Usage

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 function fun, a purrr style lambda ~ fun(.) or a list of either form.

...

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

mtcars <- as_tibble(mtcars) # for nicer printing

mtcars %>% rename_all(toupper)
#> # A tibble: 32 × 11
#>      MPG   CYL  DISP    HP  DRAT    WT  QSEC    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows
# ->
mtcars %>% rename_with(toupper)
#> # A tibble: 32 × 11
#>      MPG   CYL  DISP    HP  DRAT    WT  QSEC    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

# NB: the transformation comes first in rename_with
is_whole <- function(x) all(floor(x) == x)
mtcars %>% rename_if(is_whole, toupper)
#> # A tibble: 32 × 11
#>      mpg   CYL  disp    HP  drat    wt  qsec    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows
# ->
mtcars %>% rename_with(toupper, where(is_whole))
#> # A tibble: 32 × 11
#>      mpg   CYL  disp    HP  drat    wt  qsec    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

mtcars %>% rename_at(vars(mpg:hp), toupper)
#> # A tibble: 32 × 11
#>      MPG   CYL  DISP    HP  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows
# ->
mtcars %>% rename_with(toupper, mpg:hp)
#> # A tibble: 32 × 11
#>      MPG   CYL  DISP    HP  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

# You now must select() and then rename

mtcars %>% select_all(toupper)
#> # A tibble: 32 × 11
#>      MPG   CYL  DISP    HP  DRAT    WT  QSEC    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows
# ->
mtcars %>% rename_with(toupper)
#> # A tibble: 32 × 11
#>      MPG   CYL  DISP    HP  DRAT    WT  QSEC    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # ℹ 22 more rows

# Selection drops unselected variables:
mtcars %>% select_if(is_whole, toupper)
#> # A tibble: 32 × 6
#>      CYL    HP    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     6   110     0     1     4     4
#>  2     6   110     0     1     4     4
#>  3     4    93     1     1     4     1
#>  4     6   110     1     0     3     1
#>  5     8   175     0     0     3     2
#>  6     6   105     1     0     3     1
#>  7     8   245     0     0     3     4
#>  8     4    62     1     0     4     2
#>  9     4    95     1     0     4     2
#> 10     6   123     1     0     4     4
#> # ℹ 22 more rows
# ->
mtcars %>% select(where(is_whole)) %>% rename_with(toupper)
#> # A tibble: 32 × 6
#>      CYL    HP    VS    AM  GEAR  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     6   110     0     1     4     4
#>  2     6   110     0     1     4     4
#>  3     4    93     1     1     4     1
#>  4     6   110     1     0     3     1
#>  5     8   175     0     0     3     2
#>  6     6   105     1     0     3     1
#>  7     8   245     0     0     3     4
#>  8     4    62     1     0     4     2
#>  9     4    95     1     0     4     2
#> 10     6   123     1     0     4     4
#> # ℹ 22 more rows

mtcars %>% select_at(vars(-contains("ar"), starts_with("c")), toupper)
#> # A tibble: 32 × 10
#>      MPG   CYL  DISP    HP  DRAT    WT  QSEC    VS    AM  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4
#> # ℹ 22 more rows
# ->
mtcars %>%
  select(!contains("ar") | starts_with("c")) %>%
  rename_with(toupper)
#> # A tibble: 32 × 10
#>      MPG   CYL  DISP    HP  DRAT    WT  QSEC    VS    AM  CARB
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4
#> # ℹ 22 more rows