Open In App

Concatenate List of Two data.tables Using rbindlist() Function in R

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will be looking at the approach to concatenating a list of two data.tables using rbindlist() function in the R programming language.

Concatenate List of Two data.tables Using rbindlist() Function

In this method of concatenating a list of two data.tables using the rbindlist() function, the user needs to first install and import the data.table package to the working R console, then the user needs to call the rbindlist() function passed with the data tables created and further this function will be returning the data table concatenated with passed data tables in the function in the R programming language.

Syntax: rbindlist( l, fill = FALSE, use.names = “check”, idcol = NULL)

Parameters:

  •     l : This is a list of data.table or data.frame or list objects.
  •    fill : This is false by default. If we specify this as true, then it automatically fills the missing columns with NAs.
  •    use.names : By default, it is specified as check which implies all the elements may not have same names in the same order.
  •    idcol : It basically creates a column in the result, displaying which list item those particular rows came from.

Example 1:

In this example, we have imported the data.table library and then created two data tables using the data.table() function from the data.table package further with the call of the rbindlist() function of the data.table package we have passed the name of the table created and in the result, this function returns the concatenated table in the R programming language.

R




# import required library
library("data.table")
  
# create data table to concatenate 
sales_2021 <- data.table(quarter = 1:4,
                         amount=c(3550,3000,3120,3670))
sales_2022 <- data.table(quarter = 1:4,
                         amount = c(3200,2590,2970,3420))
  
# Use rbindlist() function to concatenate
data_concat <- rbindlist(list(sales_2021,
                              sales_2022))   
data_concat      


Output:

 

Example 2:

In this example, we have imported the data.table library and then created two data tables using the data.table() function from the data.table package further with the call of the rbindlist() function of the data.table package we have passed the name of the table created and with the special parameter passed idcol which add an identifier columnin the result, this function returns the concatenated table in the R programming language.

R




# import required library
library("data.table")
  
# create data table to concatenate 
sales_2021 <- data.table(quarter = 1:4,
                         amount=c(3550,3000,3120,3670))
sales_2022 <- data.table(quarter = 1:4,
                         amount = c(3200,2590,2970,3420))
  
# Use rbindlist() function to concatenate
data_concat <- rbindlist(list(sales_2021,
                              sales_2021),
                         idcol = TRUE)   
data_concat      


Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads