Rename Columns of data.table in R (2 Examples)

 

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

Table of contents:

Here’s how to do it:

 

Example Data & Add-On Packages

To use the functions of the data.table package (github page here & our introduction on Statistics Globe here), we first have to install and load data.table to R:

install.packages("data.table")                             # Install data.table package
library("data.table")                                      # Load data.table

As a next step, I’ll also have to create some data that we can use in the examples below:

ex_dt <- data.table(a = c(TRUE, FALSE, TRUE, FALSE),  
                    b = 1:4,
                    c = letters[1:4],
                    d = seq(0, 1, length.out = 4))         # Create data.table
ex_dt                                                      # Print data

 

table 1 data frame rename columns data table

 

Have a look at the previous table. It shows that our example data is made up of four rows and four columns. We explicitly chose the column names of the data table in the code lines above.

 

Example 1: Rename a Column in a data.table

This section demonstrates how to rename the columns of the example data.table.

ex_dt_2 <- data.table::copy(ex_dt)                         # Duplicate the data
colnames(ex_dt_2)                                          # Print column names
# [1] "a" "b" "c" "d"
colnames(ex_dt_2)[2] <- "B_rename"                         # Rename second column name
ex_dt_2

 

table 2 data frame rename columns data table

 

In Table 2 you can see that we have created a second data.table with a new column name by running the previously shown R code. That is, we indexed column name number two and assigned a new name to the column.

 

Example 2: Rename a Column in a data.table Without Indexing the Column

In this section, I’ll demonstrate how to change the name of a column without indexing it. Sometimes, our dataset consists of many columns and we may not be sure which is the number of the column that we want to rename.

ex_dt_3 <- data.table::copy(ex_dt)                         # Duplicate the data
colnames(ex_dt_3)                                          # Print column names
# [1] "a" "b" "c" "d"
colnames(ex_dt_3)[colnames(ex_dt_3) == "c"] <- "C_rename"  # Rename the column which was names "c"
ex_dt_3

 

table 3 data frame rename columns data table

 

As shown in Table 3, the previous R syntax has created a data.table, where the name of the column, which was previously named c, was changed to C_rename.

 

Video & Further Resources

Have a look at the following video on my YouTube channel. I illustrate the R programming codes of this page in the video.

 

The YouTube video will be added soon.

 

In addition, you may want to have a look at the other articles on my website.

 

Summary: In this article, you have learned how to edit the column names of a data.table in R programming. Don’t hesitate to please let me know in the comments, if you have any additional questions.

 

Anna-Lena Wölwer Survey Statistician & R Programmer

This page was created in collaboration with Anna-Lena Wölwer. Have a look at Anna-Lena’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

  • Vinay Prasad Bijalwan
    May 8, 2023 5:55 am

    i Want to rename multiple column name in csv file and also save in csv .

    Reply
    • Hello Vinay,

      You can do the following:

      library(tidyverse)
       
      file_path <- "C:/Users/Admin/Desktop/Pizza.csv" # Replace this with the path to your CSV file
      data <- read.csv(file_path) #define data
      names(data)
      # [1] "brand"  "id"     "mois"   "prot"   "fat"    "ash"    "sodium" "carb"   "cal"   
       
      names(data)<-paste(c(1:9), colnames(data), sep=".")
      names(data)
      # [1] "1.brand"  "2.id"     "3.mois"   "4.prot"   "5.fat"    "6.ash"    "7.sodium" "8.carb"   "9.cal"   
       
      write.csv(data, file = "my_pizza_data.csv") # Resave the csv file under the new name

      You can find the new csv file in your working directory.

      Regards,
      Cansu

      Reply

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.

Top