Transposing a data.table in R (Example Code)

This post shows how to flip a data.table over its diagonal in the R programming language.

Setting up the Example

Install and load the data.table package.

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

Load the built-in iris dataset as an example.

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

Convert the example data into a data.table with function setDT() from the data.table package.

iris_dt <- data.table::copy(iris)       # Replicate iris data set
setDT(iris_dt)                          # Convert iris to a data.table
head(iris_dt)                           # Print the head of the data
#    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: Get the Transpose of the iris Data

For illustrate how to rotate the iris dataset, we use the first three data rows.

iris_dt_2 <- iris_dt[1:3,]              # Reduce the data to the first three rows
iris_dt_2                               # Print the data
#    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

We can transpose the data.table with function transpose() from the data.table package.

iris_dt_2_tr <- transpose(iris_dt_2)    # Transpose the data
iris_dt_2_tr                            # Print the data
#        V1     V2     V3
# 1:    5.1    4.9    4.7
# 2:    3.5      3    3.2
# 3:    1.4    1.4    1.3
# 4:    0.2    0.2    0.2
# 5: setosa setosa setosa

You can see that the data was rotated along the diagonal line.

 

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. Required fields are marked *

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