How To Use The rename() Function In R

rename() Function In R

The rename() function in R can help you rename columns in a data frame. Check out the examples below to understand how it works.

rename() Function In R

Install dplyr

It is important to note that rename() isn’t a built-in function that comes with default R installations. It belongs to the tidyverse collection, or to be more exact, the dplyr package.

To use this function, you will need to install dplyr or tidyverse first. Pick one of the two commands:

install.packages("tidyverse")

install.packages("dplyr")

When the installation completes, load the dplyr package into your existing R environment:

library("dplyr")

How To Use rename()

Let’s create a simple 3×3 data frame whose column names are “col1”, “col2”, and “col3”.

df <- data.frame(col1 = 1:3, col2 = 4:6, col3 = 7:9)
df
  col1 col2 col3
1 1 4 7
2 2 5 8
3 3 6 9

If you no longer want to use these names, the rename() function can help you. There are many circumstances you may want to do this, such as when the names R extracts from a data source may be too hard to work with.

The rename() function only needs the source data frame and an assignment operation for the new name. For example, the rename() command will change the name of the first column from “col1” to “C1”:

df2 <- rename(df, "C1" = "col1")
df2
  C1 col2 col3
1 1 4 7
2 2 5 8
3 3 6 9

Remember that rename() doesn’t modify data on the input data frame you pass to it. Instead, the new data frame with the new column names will be returned.

The above example is fairly straightforward. You can rename multiple columns at once by adding them as arguments, separated by commas. This example will change the names of all three columns in the data frame df:

rename(df, "C1" = "col1", "C2" = "col2", "C3" = "col3")
  C1 C2 C3
1 1 4 7
2 2 5 8
3 3 6 9

You can also put those arguments into a single vector with the c() function:

rename(df, c("C1" = "col1", "C2" = "col2", "C3" = "col3"))
  C1 C2 C3
1 1 4 7
2 2 5 8
3 3 6 9

The tidyverse collection also comes with the pipe operator %>%. You can use it to make your code more readable, especially when you need to write complex analyses in R:

df %>% rename(c("C1" = "col1", "C2" = "col2", "C3" = "col3"))
  C1 C2 C3
1 1 4 7
2 2 5 8
3 3 6 9

You can use a variable to store the vector to make the command even shorter:

vt <- c("C1" = "col1", "C2" = "col2", "C3" = "col3")
df %>% rename(vt)
  C1 C2 C3
1 1 4 7
2 2 5 8
3 3 6 9

The dplyr package also provides the rename_with() function. It acts like rename() but renames data frame columns using R functions instead.

This command, for example, will convert the names of columns in df to uppercase. The rename_with() function will run through every column in the input data frame and change its name to the upper case with toupper().

df %>% rename_with(toupper)
  COL1 COL2 COL3
1 1 4 7
2 2 5 8
3 3 6 9

Summary

You can use the rename() function in R to change the names of a data frame’s columns. Don’t forget to install and load the dplyr package first.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *