Table of Contents
How to change the column names of the R DataFrame (data.frame). It is possible to change the column names of the dataframe in R using three methods. We can able to change single or all column names using colnames(). Next, we are using setNames() which is used to change all the column names in a dataframe. Finally, we are using rename() method with the %>% operator
to change all the column names.
1 Quick Examples of Change Column Names of R DataFrame
Following are quick examples of changing column names of 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))
#change the column names to c1,c2,c3,c4 and c5 using colnames()
colnames(my_dataframe) = c('c1','c2','c3','c4','c5')
#change second column to c2
colnames(my_dataframe)[2] ="c2"
#change fifth column to c5
colnames(my_dataframe)[5] ="c5"
#change the column names to c1,c2,c3,c4 and c5 using setNames()
print(setNames(my_dataframe, c('c1','c2','c3','c4','c5')))
#load the dplyr library
library("dplyr")
#change the column names to c1,c2,c3,c4 and c5 using rename()
print(my_dataframe %>% rename(c1 = id,c2 = pages,c3 = name,c4 = chapters,c5 = price))
Let’s create the dataframe with five rows and five columns.
# 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:
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
We can see that the column names are: id
, pages
, name
, chapters
, and price
. Let’s change the column names for the this dataframe using several methods.
2. Change Column Names of the dataframe using the colnames() method
colnames() is the method available in R, which is used to rename all the columns present in the dataframe.
Syntax:
#Syntax for colnames to rename all columns
colnames(my_dataframe) = c('new_column_name',...........)
where,
my_dataframe
is the input dataframenew_column_name
is the new column name that replaces the old column name
Example:
Let’s rename the columns of the above dataframe to c1
,c2
,c3
,c4
, and c5
.
#Change the column names to c1,c2,c3,c4 and c5 using colnames()
colnames(my_dataframe) = c('c1','c2','c3','c4','c5')
#Display the dataframe
print(my_dataframe)
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
We can see that column names are changed.
If we want to rename a particular column by index, then we have to specify the index of the column. In R, indexing starts with 1.
Syntax:
#Syntax to rename single column wth colnames() by column index.
colnames(my_dataframe)[index_pos] ="new_column_name"
where index_pos
represent the column index.
Example:
In this example, we will change the second column to c2
and the fifth column to c5
.
# 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))
#change second column to c2
colnames(my_dataframe)[2] ="c2"
#change fifth column to c5
colnames(my_dataframe)[5] ="c5"
#display the dataframe
print(my_dataframe)
Output:
id 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
We can see that the second and fifth column names are changed.
3. Change Column Names of the dataframe using the setNames() method
setNames() is the method available in R, which is used to change all the columns present in the dataframe.
Syntax:
#Syntax for setNames() to change the column names
setNames(my_dataframe, c('new_column_name',...........))
where,
- my_dataframe is the input dataframe
- new_column_name is the new column name that replaces the old column name
Example:
Let’s rename the columns of the above dataframe to c1,c2,c3,c4, and c5.
# 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))
# Change the column names to c1,c2,c3,c4 and c5 using setNames()
print(setNames(my_dataframe, c('c1','c2','c3','c4','c5')))
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
We can see that column names are changed.
4. Change Column Names of the dataframe using rename with %>% operator
rename() is the method available in the dplyr package, which is used to change the particular columns present 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. But we have to load the dplyr library in order to use this method.
Syntax:
#Syntax to load the dplyr library
library("dplyr")
#Syntax to change column names using rename()
my_dataframe %>% rename(new_column_name = old_column_name,...........)
where,
- my_dataframe is the input dataframe
- new_column_name is the new column name that replaces the old_column_name
Example:
Let’s rename the columns of the above dataframe to c1,c2,c3,c4, and c5.
#Load the dplyr library
library("dplyr")
#Change the column names to c1,c2,c3,c4 and c5 using rename()
print(my_dataframe %>% rename(c1 = id,c2 = pages,c3 = name,c4 = chapters,c5 = price))
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
We can see that column names are changed.
Conclusion
In this article, we have seen how to change the column names in the data frame using colnames(), rename(), and setNames() methods, It can be possible to change single or multiple columns using colnames() and rename() methods.
Related Articles
- Select Columns by Name in R?
- Select Columns by Index Position in R
- R select() Function from dplyr – Usage with Examples
- R Filter DataFrame by Column Value
References
