Establishing crisp, clear column names is essential to keeping a large statistics project organized. 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: you will likely want to rename columns in your data frame to make it easier to understand and maintain over time.
Renaming columns in R
We’re going to use the ChickWeight data frame for this exercise and make it easier to understand by changing column names in R. As you may remember, the ChickWeight data set includes four fields:
You can easily load the dataset into R by typing data(ChickWeight) into the R interpreter.
We’re going to rename a few variables in this R dataframe to make it easier to remember.
names(ChickWeight)[names(ChickWeight)=="Time"] <- "Days"
As you can see from the screenshot below, this worked:
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.
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.
names(ChickWeight) <- "Ounces"
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.
And that concludes our summation of how to rename a column in R. By renaming your variables into easily remembered names, 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.