The rbindlist() function in R can be used to create one data.table from a list of many data.table or data.frame objects.
This function uses the following basic syntax:
rbindlist(l, use.names="check", fill=FALSE, idcol=NULL)
- l: List containing data.table, data.frame, or list objects.
- use.names: TRUE binds by column names. FALSE binds by position.
- fill: TRUE fills missing values with NA.
- idcol: Creates columnshowing which list item those rows came from.
The following example shows how to use this function in practice.
Example: Use rbindlist to Make One Data Table
Suppose we have the following list of data.table and data.frame objects in R:
library(data.table) #create data frames and data tables data1 <- data.table(team=c('A', 'B', 'C'), points=c(22, 27, 38)) data2 <- data.table(team=c('D', 'E', 'F'), points=c(22, 14, 20)) data3 <- data.frame(team=c('G', 'H', 'I'), points=c(11, 15, 18)) #view data frames and data tables print(data1) print(data2) print(data3) team points 1: A 22 2: B 27 3: C 38 team points 1: D 22 2: E 14 3: F 20 team points 1 G 11 2 H 15 3 I 18
We can use the following rbindlist() function to bind together the list of data.table and data.frame objects into one data.table:
#define list of objects to bind together data_list <- list(data1, data2, data3) #bind together list of objects big_data <- rbindlist(data_list) #view result big_data team points 1: A 22 2: B 27 3: C 38 4: D 22 5: E 14 6: F 20 7: G 11 8: H 15 9: I 18
The result is a data.table object with nine rows that are composed of the rows from the list of objects we provided.
We can also use the class() function to verify that the result is indeed a data.table object:
#view class of resulting object class(big_data)  "data.table" "data.frame"
We can see that the result is indeed a data.table object.
The Benefit of Using rbindlist
The alternative to using rbindlist would be to use do.call with the rbind function in base R:
#use rbind to bind together list of objects do.call("rbind", data_list) team points 1: A 22 2: B 27 3: C 38 4: D 22 5: E 14 6: F 20 7: G 11 8: H 15 9: I 18
This code produces the same result but it turns out that rbindlist is significantly faster, especially for extremely large data.table or data.frame objects.
The following tutorials explain how to perform other common operations in R: