Create a data.table From a List in R (2 Examples)

In this R tutorial, you’ll learn how to transform a list into a data.table.

Setting up the Examples

We start by installing and loading the data.table package.

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

For the examples, we use the built-in iris dataset.

data(iris)                                         # Load iris data set
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

From the iris data, we construct a list, each list element consists of the first 10 values of one of the variables Sepal.Length, Sepal.Width, and Species.

list_iris <- list(Sepal.Length = iris$Sepal.Length[1:10],
                  Sepal.Width  = iris$Sepal.Width[1:10],
                  Species      = iris$Species[1:10])
list_iris
# $Sepal.Length
#  [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9
# 
# $Sepal.Width
#  [1] 3.5 3.0 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1
# 
# $Species
#  [1] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
# Levels: setosa versicolor virginica

Example 1: Converting a list to a data.table

In this Example, we use setDT(), which takes a list as input and returns a data.table. Before we apply the function, we create a copy of list l_1 with data.table::copy() as setDT() overrides its input.

iris_dt <- data.table::copy(list_iris)             # Replicate list_iris
setDT(iris_dt)                                     # Convert iris_dt to a data.table
head(iris_dt)                                      # Head of the data
#    Sepal.Length Sepal.Width Species
# 1:          5.1         3.5  setosa
# 2:          4.9         3.0  setosa
# 3:          4.7         3.2  setosa
# 4:          4.6         3.1  setosa
# 5:          5.0         3.6  setosa
# 6:          5.4         3.9  setosa

Example 2: Alternative to Converting a list to a data.table

There are other possible ways of turning a list into a data.table. One is to use do.call() and determine that the elements of list_iris are combined columnwise (cbind()) with the following command.

iris_dt2 <- data.table(do.call(cbind, list_iris))  # Convert iris list to a data.table
head(iris_dt2)                                     # Head of the data
#    Sepal.Length Sepal.Width Species
# 1:          5.1         3.5       1
# 2:          4.9         3.0       1
# 3:          4.7         3.2       1
# 4:          4.6         3.1       1
# 5:          5.0         3.6       1
# 6:          5.4         3.9       1

With outer function data.table(), the output is transformed into a data.table.

Related Articles & Further Resources

Please find some additional R tutorials on topics such as lists, indices, and vectors in the following list.

 

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.

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