When you are working with real-time projects, you often come across raw datasets where columns have names like (“v1”, “var2”, and “cust_id”). These names seem very cryptic and are not descriptive.
That’s why we must rename them to descriptive names (e.g., “age”, “income”, “customer_ID”), which makes the data much easier to understand at a glance.
Here are the four ways to rename a data frame column in R:
The most efficient way to rename a single column is to use Base R’s colnames() function, which sets or gets the names of the data frame columns. This function provides flexibility, allowing you to rename a column by name or position.
For renaming, you want to assign a new character vector to colnames(your_data_frame). Ensure that the vector length is the same as the number of columns.
df <- data.frame(
name = c("Krunal", "Ankit", "Rushabh"),
score = c(85, 90, 78),
subject = c("Math", "Math", "History"),
grade = c("10th", "11th", "11th")
)
print("Before renaming the column")
df
colnames(df)[colnames(df) == "grade"] <- "class"
print("After renaming the column")
df
Output
The names() is a general-purpose base function that can be used to get or set names of various R objects, including data frame columns.
How can I use it? Well, assign a new character vector (column name) to names(your_data_frame). It will be renamed with your new character vector.
df <- data.frame( name = c("Krunal", "Ankit", "Rushabh"), score = c(85, 90, 78), subject = c("Math", "Math", "History"), grade = c("10th", "11th", "11th") ) print("Before renaming the column") df names(df)[names(df) == "grade"] <- "class" print("After renaming the column") df
Output
As illustrated in the above image, our final data frame’s column name has been changed.
The pros and cons are the same as the “colnames()” method.
You can also use the dplyr::rename() function, where you specify the new column name on the left side of the = and the old name on the right side.
library(dplyr)
df <- data.frame(
name = c("Krunal", "Ankit", "Rushabh"),
score = c(85, 90, 78),
subject = c("Math", "Math", "History"),
grade = c("10th", "11th", "11th")
)
print("Before renaming the column")
df
print("After renaming the column")
df %>% rename(class = grade)
Output
If you want to rename single or multiple columns at once, you can use the “rename_with()” function from the “dplyr” package.
This approach is not only helpful for changing the column name but also for converting column names to lowercase or replacing specific characters.
library(dplyr)
df <- data.frame(
name = c("Krunal", "Ankit", "Rushabh"),
score = c(85, 90, 78),
subject = c("Math", "Math", "History"),
grade = c("10th", "11th", "11th")
)
print("Before renaming the column")
df
print("After renaming the column")
df <- df %>% rename_with(~ ifelse(. == "grade", "class", .), .cols = "grade")
df
Output
For renaming a single column, use the “colnames()” or “names()” function.
For renaming multiple columns at once in R, use the “rename_with()” or “rename()” function from the “dplyr” package.
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
R vectors are atomic, which means they have homogeneous data types. They are contiguous in…
DataFrames are like tables that contain rows and columns. Each column can have a different…
Dates in R are stored as the number of days since 1970-01-01, so converting a…
In R, you can think of a vector as a series of values in a…
The dplyr filter() function in R subsets a data frame and retains all rows that…
The dplyr::distinct() function in R removes duplicate rows from a data frame or tibble and keeps…