How to Rename Multiple Columns in R
Last Updated :
29 Feb, 2024
Renaming columns in R Programming Language is a basic task when working with data frames, and it’s done to make things clearer. Whether you want names to be more understandable, follow certain rules, or match your analysis, there are different ways to change column names.
There are types of methods available for Rename Multiple Columns in R
- Using the names function
- Using the colNames function
- Using “dplyr” package
- Using Index
- Using Setnames
R
df <- data.frame (
old_name1 = c (1, 2, 3),
old_name2 = c (4, 5, 6),
old_name3 = c (7, 8, 9)
)
print ( "Original Data Frame:" )
print (df)
|
Output:
[1] "Original Data Frame:"
old_name1 old_name2 old_name3
1 1 4 7
2 2 5 8
3 3 6 9
Use the names() function to rename the columns
R
new_names <- c ( "new_name1" , "new_name2" , "new_name3" )
names (df) <- new_names
print ( "Data Frame with Renamed Columns:" )
print (df)
|
Output:
[1] "Data Frame with Renamed Columns:"
new_name1 new_name2 new_name3
1 1 4 7
2 2 5 8
3 3 6 9
Using colNames() function
R
df <- data.frame (
old_Frame1 = c (1, 2, 3),
old_Frame2 = c (4, 5, 6),
old_Frame3 = c (7, 8, 9)
)
print ( "Original Data Frame:" )
print (df)
|
Output
[1] "Original Data Frame:"
old_Frame1 old_Frame2 old_Frame3
1 1 4 7
2 2 5 8
3 3 6 9
Now , use the colnames() function for renaming the multiple columns
R
new_names <- c ( "new_Frame1" , "new_Frame2" , "new_Frame3" )
colnames (df) <- new_names
print ( "Data Frame with Renamed Columns:" )
print (df)
|
Output:
[1] "Data Frame with Renamed Columns:"
new_Frame1 new_Frame2 new_Frame3
1 1 4 7
2 2 5 8
3 3 6 9
Rename Multiple Columns in R Using “dplyr” package
R
library (dplyr)
df <- data.frame (
old_Column1 = c (1, 1, 1),
old_Column2 = c (5, 5, 5),
old_Column3 = c (8, 8, 8)
)
print ( "Original Data Frame:" )
print (df)
|
Output:
[1] "Original Data Frame:"
old_Column1 old_Column2 old_Column3
1 1 5 8
2 1 5 8
3 1 5 8
Use the dplyr package rename function for renaming multiple columns
R
df <- df %>%
rename (new_Column1 = old_Column1,
new_Column2 = old_Column2,
new_Column3 = old_Column3)
print ( "Data Frame with Renamed Columns:" )
print (df)
|
Output:
[1] "Data Frame with Renamed Columns:"
new_Column1 new_Column2 new_Column3
1 1 5 8
2 1 5 8
3 1 5 8
Rename Multiple Columns in R Using Index
R
library (dplyr)
my_dataframe <- data.frame (
old1 = c (1, 2, 3),
old2 = c (4, 5, 6),
old3 = c (7, 8, 9)
)
print ( "Old Dataframe" )
print (my_dataframe)
|
Output
[1] "Old Dataframe"
old1 old2 old3
1 1 4 7
2 2 5 8
3 3 6 9
Use the Column Index for renaming
R
my_dataframe <- my_dataframe %>%
rename (c1 = !!1, c2 = !!2)
print ( "New Dataframe" )
print (my_dataframe)
|
Output:
[1] "New Dataframe"
c1 c2 old3
1 1 4 7
2 2 5 8
3 3 6 9
Rename Multiple Columns in R Using Setnames ()
R
library (data.table)
my_data_table <- data.table (
oldName1 = c (1, 2, 3),
oldName2 = c (4, 5, 6),
oldName3 = c (7, 8, 9)
)
print ( "Orginal Dataframe" )
print (my_data_table)
|
Output:
[1] "Orginal Dataframe"
oldName1 oldName2 oldName3
1: 1 4 7
2: 2 5 8
3: 3 6 9
Use the data.table package’s setnames() function for renaming the columns
R
new_names <- c ( "newName1" , "newName2" , "newName3" )
setnames (my_data_table, old = c ( "oldName1" , "oldName2" , "oldName3" ), new = new_names)
print ( "New Dataframe" )
print (my_data_table)
|
Output:
[1] "New Dataframe"
newName1 newName2 newName3
1: 1 4 7
2: 2 5 8
3: 3 6 9
Conclusion
In R, changing column names is easy and can be done in different ways. We can use basic functions like names() and colnames(), the helpful dplyr package with its rename() function, specify column positions, or use setnames() from the data.table package. Each approach caters to different preferences and scenarios, providing users with flexibility in adapting their code to suit diverse data manipulation requirements.
Share your thoughts in the comments
Please Login to comment...