How to rename multiple columns by index and name in the R DataFrame (data.frame)? To change multiple column names by name and by index use rename()
function of the dplyr
package and to rename by just name use setnames()
from data.table
.
From R base functionality, we have colnames()
and names()
functions that can be used to rename a data frame column by a single index or name. These functions are used to set or get the names of a data frame object.
In a separate article, I have covered different ways to rename columns in R if you have time, I would recommend reading it.
1. Quick Examples of Rename Multiple Columns
Following are quick examples to rename multiple columns by name and index in the R data frame.
# Load library dplyr
library("dplyr")
# Example 1 - Rename multiple columns by name
my_dataframe <- my_dataframe %>% rename("c1" = "id",
"c2" = "pages",
"c3" = "name")
# Example 2 - Rename multiple columns by index
my_dataframe <- my_dataframe %>%
rename(c1 = 1, c2 = 2)
# Example 3 - Load library data.table
library(data.table)
# Rename multiple columns for old to new
setnames(my_dataframe, old = c('c1','c2','c3'),
new = c('id','pages','name'))
# Example 4 - Rename all columns
names(my_dataframe) <- c('c1','c2','c3','c4','c5')
# Example 5 - Rename multiple columns
colnames(my_dataframe) <- c('c1','c2','c3','c4','c5')
Let’s create an R DataFrame,
# Create dataframe
my_dataframe=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(my_dataframe)
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
2. Rename Multiple Columns by Name
The rename() function is a part of dplyr library which is used to rename multiple columns (column names) by name in the dataframe. The operator – %>%
is used to load the renamed column names to the dataframe. At a time it will change single or multiple column names. When you use the %>%
operator, the functions we use after this will be applied to the dataframe to the left of the operator.
dplyr
is a third-party library hence, to use dplyr library, you need to first install it by using install.packages('dplyr')
. Once installation completes, load the dplyr
library using library("dplyr")
.
# Load library dplyr
library("dplyr")
# Rename multiple columns
my_dataframe <- my_dataframe %>% rename("c1" = "id",
"c2" = "pages",
"c3" = "name")
print(my_dataframe)
Yields below output. This changes the column names id
to c1
, pages
to c2
and name
to c3
.
#Output
c1 c2 c3 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. Rename Multiple Columns by Index
Use the same rename()
from R dplyr library to change multiple columns by index position in the data frame. Note that rename()
with %>%
doesn’t change the current data frame instead, it returns the new data frame after the rename hence, assign this to a data frame to change it on the existing data frame.
# Rename multiple columns by index
# load library
library(dplyr)
# Rename column by index
my_dataframe <- my_dataframe %>%
rename(c1 = 1, c2 = 2)
# print dataframe
print(my_dataframe)
Yields below output.
# Output
c1 c2 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
4. Rename Multiple Columns using setnames()
setnames()
is a function from data.table library that can be used to change multiple columns at a time in the R dataframe.
Like dplyr, data.table
is a third-party library hence, in order to use data.table library, you need to first install it by using install.packages('data.table')
. Once installation completes, load the data.table
library in order to use this setnames()
method. To load a library use library("data.table")
.
# Load library data.table
library(data.table)
# Rename multiple columns for old to new
setnames(my_dataframe, old = c('c1','c2','c3'),
new = c('id','pages','name'))
#Display column names
print(colnames(my_dataframe))
Yields below output. The above example changes back the column names from c1
to id
, c2
to pages
and c3
to name
.
# Output column names
[1] "id" "pages" "name" "chapters" "price"
5. Rename Using names()
names()
is the method available in R which can be used to change all column names. You can also use this method to rename data frame column by index.
Following is the syntax of the names() to use column names from the list.
# Syntax using names()
names(my_dataframe) <- c(list_col_names)
Here c()
returns a one-dimensional vector with the column names and names()
is used to set the names to a data frame. In the below example we are assigning a vector of names to data frame column names.
# Rename all column names
names(my_dataframe) <- c('c1','c2','c3','c4','c5')
print(my_dataframe)
Yields below output. You can also have the columns with a list to rename.
# Output
c1 c2 c3 c4 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
6. Rename Using colnames()
colnames()
is another method available in R which can also be used to rename all column names (list with column names). Actually, colnames()
is used to set or get the names of a data frame. You can also use this method to rename data frame column by index in R.
# Rename all columns
colnames(my_dataframe) <- c('c1','c2','c3','c4','c5')
print(my_dataframe)
Yields below output. Here c()
returns a one-dimensional vector with the column names and colnames()
is used to set the names to a data frame. In the below example we are assigning a vector of names to data frame column names. Let’s see an example.
# Output
c1 c2 c3 c4 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
7. Conclusion
In this article, you have learned how to rename multiple columns at a time in an R Data Frame (data.frame). To change multiple columns by name and by index use rename()
function of the dplyr
package and use setnames()
from data.table
.
Related Articles
- How to drop data frame columns by name?
- How to drop data frame rows in R?
- How to replace column value with another column?
- How to replace character in a string
- How to select rows or observations in R
- How to select columns or variables in R?
- R – Replace Values Based on Condition