Transforming data.table to data.frame or Matrix in R (4 Examples)

In this tutorial, I’ll explain how to change the structure of a data.table to data.frame and matrix in R programming, and how to do it the other way around.

Preparing the Examples

First, install and load the data.table package.

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

For our examples, we take the iris dataset.

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

str(iris)                                                             # See the structure of the data
# 'data.frame':	150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

Example 1: Converting a data.frame to a data.table

The iris dataset is a data.frame. We now copy the data and convert it into a data.table with the following lines of code.

iris_DT_1 <- data.table::copy(iris)                                   # Replicate the iris data set
iris_DT_1 <- setDT(iris_DT_1)                                         # Convert to data.table

str(iris_DT_1)                                                        # See the structure of the data
# Classes ‘data.table’ and 'data.frame':	150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#  - attr(*, ".internal.selfref")=<externalptr> 

head(iris_DT_1)                                                       # Printing the data head
#    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 2: Converting a data.table to a data.frame

In this example, we switch the direction of the transformation. We now transform a data.table into a data.frame.

iris_DF_1 <- data.table::copy(iris_DT_1)                              # Replicate the dataset iris_DT_1
iris_DF_1 <- as.data.frame(iris_DF_1)                                 # Convert to data.table

str(iris_DF_1)                                                        # See the structure of the data
# 'data.frame':	150 obs. of  5 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

head(iris_DF_1)                                                       # Printing the data head
#   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 3: Converting a data.table to a Matrix

For transforming a data.table into a matrix, we have to be aware of the following: When there is only one non-numeric column in the data.table, all cell entries in the corresponding matrix will be characters. We therefore only select the numeric columns of the iris data.table in the following. The resulting dataset is called iris_DT_2.

iris_DT_2 <- data.table::copy(iris_DT_1)                              # Replicate the dataset iris_DT_1
num_cols <- unlist(lapply(iris_DT_2, is.numeric))                     # Identify the numeric columns
iris_DT_2 <- iris_DT_2[ , .SD, .SDcols = names(num_cols)[num_cols] ]  # Reduce the data such that it only contains numeric columns

head(iris_DT_2)                                                       # Printing the data head
#    Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1:          5.1         3.5          1.4         0.2
# 2:          4.9         3.0          1.4         0.2
# 3:          4.7         3.2          1.3         0.2
# 4:          4.6         3.1          1.5         0.2
# 5:          5.0         3.6          1.4         0.2
# 6:          5.4         3.9          1.7         0.4

You can simply use as.matrix() to transform the data.table into a matrix.

iris_mat_2 <- as.matrix(iris_DT_2)                                    # Convert a data.table to a matrix
str(iris_mat_2)                                                       # See the structure of the data
#  num [1:150, 1:4] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  - attr(*, "dimnames")=List of 2
#   ..$ : NULL
#   ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"

head(iris_mat_2)                                                      # Printing the data head
#      Sepal.Length Sepal.Width Petal.Length Petal.Width
# [1,]          5.1         3.5          1.4         0.2
# [2,]          4.9         3.0          1.4         0.2
# [3,]          4.7         3.2          1.3         0.2
# [4,]          4.6         3.1          1.5         0.2
# [5,]          5.0         3.6          1.4         0.2
# [6,]          5.4         3.9          1.7         0.4

Example 4: Converting a Matrix to a data.table

The conversion of a matrix to a data.table is straightforward by use of as.data.table(), as you can see in the following.

iris_DT_2b <- as.data.table(iris_mat_2)                               # Coerce to data.table
str(iris_DT_2b)                                                       # See the structure of the data
# Classes ‘data.table’ and 'data.frame':	150 obs. of  4 variables:
#  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  - attr(*, ".internal.selfref")=<externalptr> 

head(iris_DT_2b)                                                      # Printing the data head
#    Sepal.Length Sepal.Width Petal.Length Petal.Width
# 1:          5.1         3.5          1.4         0.2
# 2:          4.9         3.0          1.4         0.2
# 3:          4.7         3.2          1.3         0.2
# 4:          4.6         3.1          1.5         0.2
# 5:          5.0         3.6          1.4         0.2
# 6:          5.4         3.9          1.7         0.4

Further Resources

Please find some further R programming tutorials on topics such as lists, data conversion, variables, and matrices 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