Difference between dplyr::rename and dplyr::rename_all

Viewed 8869

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.

2 Answers

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} )
Related