By using rename() and rename_with() functions from dplyr/tidyverse package you can rename/change a single column name, multiple columns, rename by index, and rename all column names on R DataFrame.
dplyr is an R package that provides a grammar of data manipulation and provides a most used set of verbs that helps data science analysts to solve the most common data manipulation. In order to use this, you have to install it first using install.packages('dplyr')
 and load it using library(dplyr)
.
Alternatively, you also install and load library tidyverse to use rename() function to rename columns.
1. dplyr rename() Syntax
Following is the syntax of rename() and rename_with() functions.
# Syntax
rename(.data, ...)
rename_with(.data, .fn, .cols = everything(), ...)
Let’s create an R DataFrame, run these examples and explore the output. If you already have data in CSV you can easily import CSV files to R DataFrame. Also, refer to Import Excel File into R.
# Create dataframe
df=data.frame(id=c(11,22,33,44,55),
pages=c(32,45,33,22,56),
name=c("spark","python","R","java","jsp"),
chapters=c(76,86,11,15,7),
price=c(144,553,321,567,890))
#display the dataframe
print(df)
Output:
# Output
id pages name chapters price
1 11 32 spark 76 144
2 22 45 python 86 553
3 33 33 R 11 321
4 44 22 java 15 567
5 55 56 jsp 7 890
Notice that the column names are: id
, pages
, name
, chapters
, and price
.
2. dplyr rename column
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. When you use %>% operator, the functions we use after this will be applied to the data frame to the left of the operator.
# Load library
library("dplyr")
#Change the column name - id to c1
df %>%
rename("c1" = "id")
Note that the below example doesn’t change the column on the existing data frame. print() of this data frame results in unchanged data. In order to update assign this statement to the existing data frame.
#Change the column name - id to c1
df <- df %>%
rename("id" = "c1")
Yields the below output.
#Output
c1 pages name chapters price
1 11 32 spark 76 144
2 22 45 python 86 553
3 33 33 R 11 321
4 44 22 java 15 567
5 55 56 jsp 7 890
3. dplyr rename multiple columns
Similarly, we can also use dplyr function to rename multiple columns, here all you need is you should know your old column name and the new column name.
# Rename multiple columns
df <- df %>%
rename("c2" = "pages",
"c5" = "price")
print(my_dataframe)
Yields the below output.
#Output
c1 c2 name chapters c5
1 11 32 spark 76 144
2 22 45 python 86 553
3 33 33 R 11 321
4 44 22 java 15 567
5 55 56 jsp 7 890
4. dplyr rename column by index
To rename column by index using dplyr function, just use the index for old column names in the above example. This is the best approach as it is easily readable and the code is more organized. Note that dplyr doesn’t change the current data frame instead, it returns the new data frame after the rename, hence we need to assign the output of the rename to a variable.
# Rename column by index
df <- df %>%
rename(c1 = 1, c3 = 3)
Yields the below output.
#Output
c1 c2 c3 chapters c5
1 11 32 spark 76 144
2 22 45 python 86 553
3 33 33 R 11 321
4 44 22 java 15 567
5 55 56 jsp 7 890
5. dplyr rename all columns
rename_with() function is from R dplyr library that can be used to rename all data frame columns. In the below example let’s use this to convert all column names to upper case.
# Rename using rename_with()
my_dataframe <- my_dataframe %>%
rename_with(toupper)
Yields the below output.
#Output
C1 C2 C3 CHAPTERS C5
1 11 32 spark 76 144
2 22 45 python 86 553
3 33 33 R 11 321
4 44 22 java 15 567
5 55 56 jsp 7 890
Conclusion
In this article, you have learned the syntax of rename() and rename_with() functions from the dplyr package that is used to rename a column, rename multiple columns, rename by index, and rename all column names.
Related Articles
- dplyr filter() Function
- dplyr select() Function
- dplyr mutate() Function
- dplyr slice() Function
- dplyr distinct() Function
- dplyr arrange() Function in R
- R lm() Function – Fitting Linear Models
- Reorder Columns of DataFrame in R
- Sort Table in R with Examples