Topic: R Rename Column

Establishing crisp, clear column names is essential to keeping a large statistics project organized, especially if you are using a dataframe with multiple column or row names. At some point you will need to rename a column in r without having to create a new column. Fortunately, there are multiple ways to get this done.

Learning how to change column name in R is an essential skill. You should consider clear and specific column names as part of your official project documentation. I came to R from the Python language, which makes readability a key priority for developers. How often have you had to dust off your work six months later? Explain your ideas to a new hire? Debug the system at 3AM? Clean crisp code is your friend in these moments. The same goes for your data type: you will likely want to rename columns in your data frame to make it easier to understand and maintain each query and parameter over time, so that the field names in your dataframe can be understood and used in a regular expression, without confusion.

How To Rename Columns in R

Let’s take a look at how to change dataframe column names in R. For our first example of how to change column names in r, we’re going to use the the ChickWeight data frame and replace values within an existing dataframe column. We want to make it easier to understand by changing column names in R, and getting rid of anu null value issue or missing values in a column header. As you may remember, the ChickWeight data set includes four existing table columns:

  • Weight
  • Time
  • Chick
  • Diet

You can easily load the dataset into R by typing data(ChickWeight) into the R interpreter.

The “Time” column name is vague – there are multiple units of time. Days, Weeks, Months. Who knows, we might even merge it with other data frame that also has a Time column. Then we would have a good start on a mess. So, we are going to change that column name to make it more explicit. We can start by changing it to “days”; if we were running a complicated experiment, additional description is good- that’s where using R to rename your columns comes in handy. Especially when adding new data frame columns and new column names, it is important to keep track of every single column value and character vector properly, or it will be impossible to keep all of the variable names straight in your string or matrix.

# change column name in r

names(ChickWeight)[names(ChickWeight)=="Time"] <- "Days"

As you can see from the screenshot below, this worked:

rename a column dataframe renaming variables in r dplyr rename column rename rows in r dplyr changing column dataframe change name of column rename dplyr

We selected the “Time” field by name and successfully renamed it to “Days”. This value of this is that it simplifies things for future analysts and our collaborators. Data sets which explain themselves are a beautiful thing. Especially if you’re in charge of Data Quality Assurance. Rename columns to simple, natural terminology you can figure out several months later after you hand off your projects.

Renaming Columns by Position

Important: this technique assumes your data structure is effectively immutable. If you expect to make changes to the order of the columns or number of columns included  in the future, we recommend the other approach. That being said… you do have the option of targeting the nth column for renaming.

Example below, in this case flipping the Weight field to Ounces.

# rename column in r
names(ChickWeight)[1] <- "Ounces"
how to rename column name dplyr colnames set dataframe column names tidyverse rename columns dataframe column name change pandas change name of index column dataframe set column names r renaming variables r rename r rename variable dplyr


As you can see, this worked as well.

Again, we need to stress the danger of using this approach if you expect to change your data frame design in the future. There is a substantial burden from using a brittle system like column position. That being said, this can be an excellent quick and dirty solution for throwaway data hygiene scripts if you’re in a hurry. Remapping fields based on name is a much safer way to proceed, of course, if you have time.

Other Solutions: R to Rename Columns

There’s almost always more than one way to get things done in R.

If you’re working with the dplyr package to manipulate your data, there is a rename function. They changed it a couple of releases ago; the current syntax is

# rename column in r dplyr
rename (new_field_name = old_field_name)

You were previously able to directly use column index references in this package. with the more recent releases, you need to use a different approach to get the dplyr rename column by index function to work. We suggest turning the column names into a vector and using the index to select the right name from that vector to rename a column in r.

# dplyr rename column by index
rename (new_field_name = names(.)[index_of_field])
rename (my_shiny_new_field = names(.)[3])

Why This Matters

A good tip from traditional software development is that you easily spend as much time reading your code as writing it, particularly when you are working as part of a larger team. This is an especially important tip for folks transitioning from academia to industry. As you move from doing solo projects and projects with highly structured releases to supporting a business team, being able to pass projects to another analyst and quickly resume work from months or years ago is a crucial skill. Changing column names in your data frame so they are easy to understand can significantly simplify your life.

And that concludes our summation of how to rename a column in R. By changing your column names into easily remembered references, you simplify future updates to your projects. And as we demonstrated, it isn’t hard to change column names in R. Just to be sure to think about the balance of speed vs. flexibility your want when you write your project code and set data frame column names.

Related Topics: rename column in r

This is part of our larger series about manipulating data in R. The topics most related to this one relate to cleaning up a data frame: how to remove a column in R and remove rows in R.

Or for the full index….

For more information about handy functions for cleaning up data, check out our functions reference.

Scroll to top
Privacy Policy