Renaming Column Names of a data.table in R (2 Examples)

In this post you’ll learn how to assign other names to the columns of a data.table in the R programming language.

Setting up the Examples

First, install and load the data.table package.

install.packages("data.table")                                   # Install & load data.table
library("data.table")

We use the built-in iris data for this post.

data(iris)                                                       # Load iris data set
head(iris)                                                       # Print the head of the data
#   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

Copy and convert the data to the data.table format.

iris_DT <- data.table::copy(iris)                                # Replicate iris data set
setDT(iris_DT)                                                   # Convert iris to a data.table
head(iris_DT)
#    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

Example 1: Rename column “Species”

In this example, we know that there is a column with name Species and we want to edit the column name of the data.table into Type. For that, we first copy the data and use the following lines of code.

iris_DT_2 <- data.table::copy(iris_DT)                           # Replicate iris data set
colnames(iris_DT_2)                                              # Print column names
# [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     

colnames(iris_DT_2)[colnames(iris_DT_2) == "Species"] <- "Type"  # Rename column "Species"
head(iris_DT_2)
#    Sepal.Length Sepal.Width Petal.Length Petal.Width   Type
# 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

We successfully renamed the last column.

Example 2: Rename column number three

In Example 2, we rename a column by indexing its position. More precisely, we change the name of column number thee.

iris_DT_3 <- data.table::copy(iris_DT)                           # Replicate iris data set
colnames(iris_DT_3)                                              # Print column names
# [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     

colnames(iris_DT_3)[3] <- "Petal.L"                              # Rename column number three
head(iris_DT_3)
#    Sepal.Length Sepal.Width Petal.L 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

Related Articles

Please find some related tutorials on topics such as dplyr, data conversion, and data.table below:

 

Anna-Lena Wölwer R Programming & Survey Statistics

Note: This article was created in collaboration with Anna-Lena Wölwer. Anna-Lena is a researcher and programmer who creates tutorials on statistical methodology as well as on the R programming language. You may find more info about Anna-Lena and her other articles on her profile page.

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.
You need to agree with the terms to proceed

Menu
Top