Home » How to Rename Data Frame Columns in R

How to Rename Data Frame Columns in R

by Erma Khan

This tutorial explains how to rename data frame columns in R using a variety of different approaches.

For each of these examples, we’ll be working with the built-in dataset mtcars in R.

Renaming the First Columns Using Base R

There are a total of 11 column names in mtcars:

#view column names of mtcars
names(mtcars)

# [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
# [11] "carb"

To rename the first 4 columns, we can use the following syntax:

#rename first 4 columns
names(mtcars) 

Notice that R starts with the first column name, and simply renames as many columns as you provide it with. In this example, since there are 11 column names and we only provided 4 column names, only the first 4 columns were renamed. To rename all 11 columns, we would need to provide a vector of 11 column names.

Renaming Columns by Name Using Base R

If we want to rename a specific column in the mtcars dataset, such as the column “wt”, we can do so by name:

#rename just the "wt" column in mtcars
names(mtcars)[names(mtcars)=="wt"] 

Notice how only the “wt” column is renamed to “weight” and all of the other columns keep their original names.

Renaming Columns by Index Using Base R

We can also rename a specific column in the mtcars dataset by index. For example, here is how to rename the second column name “cyl” by index:

#rename the second column name in mtcars
names(mtcars)[2] 

Notice how only the “cyl” column is renamed to “cylinders” and all of the other columns keep their original names.

Renaming Columns Using dplyr

Another way to rename columns in R is by using the rename() function in the dplyr package. The basic syntax for doing so is as follows:

data %>% rename(new_name1 = old_name1, new_name2 = old_name2, ....)

For example, here is how to rename the “mpg” and “cyl” column names in the mtcars dataset:

#install (if not already installed) and load dplyr package
if(!require(dplyr)){install.packages('dplyr')}

#rename the "mpg" and "cyl" columns
new_mtcars % 
                rename(
                  miles_g = mpg,
                  cylinder = cyl
                  )

#view new column names
names(new_mtcars)

# [1] "miles_g" "cylinder" "disp" "hp" "drat" "wt" 
# [7] "qsec" "vs" "am" "gear" "carb" 

Using this approach,  you can rename as many columns at once as you’d like by name.

Renaming Columns Using data.table

Yet another way to rename columns in R is by using the setnames() function in the data.table package. The basic syntax for doing so is as follows:

setnames(data, old=c("old_name1","old_name2"), new=c("new_name1", "new_name2"))

For example, here is how to rename the “mpg” and “cyl” column names in the mtcars dataset:

#install (if not already installed) and load data.table package
if(!require(data.table)){install.packages('data.table')}

#rename "mpg" and "cyl" column names in mtcars
setnames(mtcars, old=c("mpg","cyl"), new=c("miles_g", "cylinder"))

#view new column names
names(mtcars)

#[1] "miles_g" "cylinder" "disp" "hp" "drat" "wt" 
#[7] "qsec" "vs" "am" "gear" "carb"  

Using this approach,  you can rename as many columns at once as you’d like by name.

Related Posts