# Transpose Data Frame in R (Example) | Rotate Matrix & Table with t Function

In this article, I’ll show how to transpose a data matrix in the R programming language.

The article will contain the following content blocks:

Let’s dive right into the examples:

## Creation of Example Data

In this R tutorial, we’ll use the following data frame as basement:

```data <- data.frame(x1 = 1:5,           # Create example data
x2 = 2:6,
x3 = 3:7)
row.names(data) <- LETTERS[1:5]
data                                   # Print example data
#   x1 x2 x3
# A  1  2  3
# B  2  3  4
# C  3  4  5
# D  4  5  6
# E  5  6  7```

As you can see based on the previously shown RStudio console output, our data matrix consists of five rows and three columns. The rows are named alphabetically and the variables are named x1, x2, and x3.

In the following example, I’ll show how to transpose (i.e. rotate) this data table in R. So keep on reading!

## Example: Applying t() Function in R

If we want to transpose our data frame, we can use the t function provided by the basic installation of the R programming language. Have a look at the following R code and the produced output:

```t(data)                                # Transpose data with t() function
#    A B C D E
# x1 1 2 3 4 5
# x2 2 3 4 5 6
# x3 3 4 5 6 7```

As you can see, we rotated our data matrix. You can also see that the row names of the original data frame are now the column names and the column names are now the row names.

## Video & Further Resources

Do you need more information on the R code of this tutorial? Then you could watch the following video of my YouTube channel. In the video, I illustrate the R codes of this tutorial in R.

In addition, you may have a look at the other tutorials on my website.

Summary: At this point you should know how to flip and rotate data tables and matrices in the R programming language. Don’t hesitate to let me know in the comments section below, in case you have further questions. Besides that, don’t forget to subscribe to my email newsletter for updates on new articles.

Subscribe to the Statistics Globe Newsletter