10

I have reviewed the documentation for dplyr multiple times and it indicates that dplyr::rename_all is a "scoped" variant of dplyr::rename. Can someone explain what this entails with regard to syntax and functionality? Why use one versus the other? The documentation for dplyr is not clear about this.

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
socialscientist
  • 3,759
  • 5
  • 23
  • 58

2 Answers2

24
  • Use rename_all to apply a function on all names
  • Use rename to give individual replacement names

For example:

library(dplyr)
cars %>% rename_all(toupper) %>% head
#   SPEED DIST
# 1     4    2
# 2     4   10
# 3     7    4
# 4     7   22
# 5     8   16
# 6     9   10

cars %>% rename_all(substr, 3) %>% head
#   spe dis
# 1   4   2
# 2   4  10
# 3   7   4
# 4   7  22
# 5   8  16
# 6   9  10

cars %>% rename(speeeeeed = speed, distance = dist) %>% head
#   speeeeeed distance
# 1         4        2
# 2         4       10
# 3         7        4
# 4         7       22
# 5         8       16
# 6         9       10
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167
10

In addition to the cases already mentioned, rename_all comes in handy when you have a full set of column name replacements assigned to an existing variable.

The difficulty comes when you try to pass that variable into rename_all. Passing the variable directly into the second argument, .funs, won't work, with or without the funs() wrapper mentioned in dplyr's help file. A variable name is not a function or an expression. It is a symbol.

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

new_car_names <- c("a", "b")

# Won't work.
cars %>% rename_all( new_car_names ) %>% head
cars %>% rename_all( funs( new_car_names ) ) %>% head

Here are some examples of a "single expression quoted with funs()" that work.

cars %>% rename_all( funs( c("a", "b")) ) %>% head

cars %>% rename_all( funs( c(new_car_names) ) ) %>% head

cars %>% rename_all( funs( ~new_car_names ) ) %>% head

cars %>% rename_all( funs( quo(new_car_names) ) ) %>% head

Here is an example of a "single expression within a quosure".

cars %>% rename_all( quo( quo(new_car_names) ) ) %>% head

Here is an example of "a function" (one that does not use its arguments).

cars %>% rename_all( function(.){new_car_names} ) %>% head

And, finally, an example of "a string naming a function".

test_function <- function(.){new_car_names}

cars %>% rename_all( "test_function" ) %>% head

While this question does not refer to rename_at, these examples inform possible usage. Note that the second argument for rename_at, .vars, accepts character vectors or position numbers to identify the existing columns, which you would like to rename.

cars %>% rename_at( .vars = "speed", function(.){new_car_names[[1]]} )

cars %>% rename_at( .vars = 1, function(.){new_car_names[[1]]} )

cars %>% rename_at( .vars = c(1,2), function(.){new_car_names} )
SoFarther
  • 524
  • 5
  • 10
  • 2
    you'd mostly use `cars %>% setNames(new_car_names)` in these cases though, wouldn't you ? `cars %>% rename_all( funs( ~new_car_names )) ` doesn't work. Maybe you meant `cars %>% rename_all(~new_car_names)` – moodymudskipper Jul 19 '18 at 21:48
  • Habit on my part. I've been using the rename family (`rename`, `rename_all`, `rename_at` and `rename_if`) given the range of situation the family addresses and that a search for "rename" finds all in code. `setNames` works, though check out `set_names` from the package `rlang`. It works like `setNames`, but with added features. – SoFarther Jul 19 '18 at 22:45
  • As for `cars %>% rename_all(~new_car_names)` not working, I just tried: `new_car_names <- c("a", "b")` `cars %>% rename_all( funs( ~new_car_names ) ) %>% head` and it worked. Do those two lines fail to execute or do they fail to rename for you? – SoFarther Jul 19 '18 at 22:46
  • @SoFarther I confirm that `new_car_names <- c("a", "b"); cars %>% rename_all( funs( ~new_car_names ) ) %>% head` does not work for me. – SavedByJESUS Sep 19 '18 at 02:05