Rename Data Frame Columns
There are several ways of renaming variables in R. In this post you will learn how to change a column name using base R function, the dplyr way and using data.table package.
I will use the R built-in iris data frame.
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Rename column names with R base functions
Let’s change Sepal.Length to S.P. The procedure is simple; get column names using names()
or colnames
, extract the specific column name, and assign a new name.
colnames(iris)[colnames(iris)=="Sepal.Length"] <- "S.P"
head(iris)
## S.P Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
The code does the following:
colnames(iris)
looks into all the names in the iris.names(iris) == "Sepal.Length"
returns a vector with true and false values to extract the variable name you want to change.<- "S.P"
assigns the new name.
You can also rename a variable by its index as well. For example, let’s change Sepal.Width [the second column] to S.W.
colnames(iris)[2] <- "S.W"
head(iris)
## S.P S.W Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Rename column names with dplyr
You can use rename()
function to change column names as following. For instance, let’s change Petal.Length to P.L and Petal.Width to P.W.
# install.packages("dplyr")
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.5
iris %>%
rename(
P.L = Petal.Length,
P.W = Petal.Width
) %>%
head()
## S.P S.W P.L P.W Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Rename column names with data.table
setnames()
function from data.table package can be used to rename a variable. The syntax is setnames(df, "old_name", "new_name")
. As an example, let’s change Species to SPECIES.
# install.packages("data.table")
library(data.table)
setnames(iris,
"Species", "SPECIES")
colnames(iris)
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width" "SPECIES"