Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between dplyr::rename and dplyr::rename_all

Tags:

r

dplyr

tidyverse

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.

like image 860
user3614648 Avatar asked Aug 06 '17 18:08

user3614648


People also ask

How to rename a column in r dplyr?

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.

How to rename variables in r dplyr?

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) .

Is dplyr in tidyverse?

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.


2 Answers

  • 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
like image 30
Moody_Mudskipper Avatar answered Oct 07 '22 02:10

Moody_Mudskipper


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} )
like image 125
SoFarther Avatar answered Oct 07 '22 00:10

SoFarther