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.
rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to change the column from old to a new name. The following example renames the column from id to c1 . The operator – %>% is used to load the renamed column names to the data frame.
To rename a column in R, you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B” again, you can run the following code: rename(dataframe, B = A) .
Similarly to readr , dplyr and tidyr are also part of the tidyverse. These packages were loaded in R's memory when we called library(tidyverse) earlier.
rename_all
to apply a function on all namesrename
to give individual replacement namesFor 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
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} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With