How to Rename a Column Name in R | 3 Examples to Change Colnames of a Data Frame


Basic R Syntax:

# Change colname of one column
colnames(data)[colnames(data) == "Old_Name"] <- "New_Name"
# Change colnames of all columns
colnames(data) <- c("New_Name1", "New_Name2", "New_Name3")
# Change colnames of some columns
colnames(data)[colnames(data) %in% c("Old_Name1", "Old_Name2")] <- c("New_Name1", "New_Name2")


As R user you will agree: To rename column names is one of the most often applied data manipulations in R. However, depending on your specific data situation, a different R syntax might be needed.

Do you need to change only one column name in R? Would you like to rename all columns of your data frame? Or do you want to replace some variable names of your data, but keep the other columns like they are?

Above, you can find the basic R code for these three data situations. For further illustration, I’m going to show you in the following tutorial how to rename a column in R, based on 3 reproducible examples.

Let’s dive in…


Example 1: Rename One Column Name in R

For the following examples, I’m going to use the iris data set. Let’s have a look how the data looks like:

data(iris)                                 # Load iris data set
head(iris)                                 # First 6 rows of iris


colnames function in R - Iris Example Data Frame

Table 1: First 6 Rows of the Iris Data Set.


The data table consists of 5 columns, with the following column names:

colnames(iris)                             # Retrieve all column names
# "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

Now, let’s replicate this data for our first example…

data_ex1 <- iris                           # Replicate iris data for first example

…and replace one of the column names with a new name:

colnames(data_ex1)[colnames(data_ex1) == "Species"] <- "New_Name" # Rename column

With the previous code, we changed the column name Species to New_Name. Let’s print our new column names to the RStudio console to check whether our R code worked well:

colnames(data_ex1)                         # Check column names after renaming
# "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "New_Name"

Looks good!

In the first example we renamed only one column – But how could we rename all column names of our data frame?

You guessed it: That’s what I’m going to show you in the next example…

Example 2: Change All R Data Frame Column Names

In the second example, I’ll show you how to modify all column names of a data frame with one line of code. First, let’s create another copy of our iris example data set:

data_ex2 <- iris                           # Replicate iris data for second example

We can change all variable names of our data as follows:

colnames(data_ex2) <- c("x1", "x2", "x3", "x4", "x5") # Modify column names

Let’s check, if we did it well:

colnames(data_ex2)                         # Check column names after renaming


Note: The replacement vector of column names has to have the same length as the number of columns of our original data. Otherwise, the remaining column names are labelled as NA:

colnames(data_ex2) <- c("x1", "x2", "x3", "x4") # The last column is NA
colnames(data_ex2)                         # Check column names again
# "x1" "x2" "x3" "x4" NA


Example 3: How to Change Multiple Column Names in R

It is also possible to change only some variable names, but leaving the others as they are. Again, let’s start by replicating the iris data:

data_ex3 <- iris                           # Replicate iris data for third example

With the following R code, you can replace the two colnames Sepal.Width and Petal.Width by New1 and New2:

colnames(data_ex3)[colnames(data_ex3)      # Rename two variable names
                   %in% c("Sepal.Width", "Petal.Width")] <- c("New1", "New2")

Please note that the ordering of the new column names has to reflect the order of the columns in the data frame.

Let’s see if it worked:

colnames(data_ex3)                         # Check column names after renaming



Video Explanation: Renaming Variables in R

In case you want to see further examples, have a look at the following video of my Statistical Porgramming YouTube channel. In the video, I’m applying the codes of the three previous examples to the airquality data set.


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Further Reading


Subscribe to my free statistics newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

6 Comments. Leave new

  • Hey, quick thing, in your original example for changing two colnames at a time, you are missing a close square bracket before the assign operator. In the explanation it is correct.

    colnames(data)[colnames(data) %in% c(“Old_Name1”, “Old_Name2”) <- c("New_Name1", "New_Name2")


    colnames(data)[colnames(data) %in% c("Old_Name1", "Old_Name2")] <- c("New_Name1", "New_Name2")

  • At least for data.table, the method for renaming multiple columns at once does not always work. It seems that
    `colnames(data)[colnames(data) %in% c(“Old_Name1”, “Old_Name2”)]` returns the column names in the order they exist in the data.table (which is not necessarily the order of the names in the RHS of %in%), so the vector from which the names are assigned has too have the same order of the columns of the data.table, or columns would be assigned wrong names.

    So in order for this to work, the order of the names in the new names vector must be the same as the order of the columns of the data.table. To me this requirement seems to strict.

    • Hi Amit,

      Thank you for this clarifications. Indeed, the ordering of the new names has to reflect the ordering of the columns in the data frame. I added a clarification to the example.



  • parminder chana
    October 21, 2020 9:58 am

    how can we replace a name with another name in r notebook

    • Hey Parminder,

      Thanks for the question. I have no experience with R notebook, so unfortunately you will have to find the answer somewhere else. Sorry for that!




Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.