In this brief tutorial, you will learn how to transpose a dataframe or a matrix in R statistical programming environment. Transposing rows and columns is a quite simple task if your data is 2-dimensional (e.g., a matrix or a dataframe). If you have a, for example, 3-dimensional array the function we are going to use in this post will not work.

## Outline

In this post, we will start by answering the question regarding which function we can use to transpose a given matrix. After that, we will create a simple matrix and, then, we will rotate it. In the following section, we will create a dataframe from the matrix. Finally, we will go on to the section where we will interchange rows with columns in the dataframe, as well.

## Which function is used to create a transpose of a given matrix in R?

To interchange rows with columns, you can use the `t()`

function. For example, if you have the matrix (or dataframe) mat you can transpose it by typing `t(mat)`

. This will, as previously hinted, result in a new **matrix** that is obtained by exchanging the rows and columns.

## How do you transpose a matrix in R?

The easiest way to transpose a matrix in R is to use the `t()`

function. As previously, mentioned, you can use `t(YourMatrix)`

to get the transpose of your matrix “YourMatrix”.

## How do I transpose data in R?

Transposing data, e.g. in a data frame or matrix, is easy to do with the `t()`

function. For example, if you want to transpose a data frame you can type `t(dataFrame)`

.

For learning more about useful functions and operators see for example the post about how to use %in% in R. First, before transposing a matrix, we will create a matrix that we can practice on.

## Create a Matrix in R

In this section, we are going to create the matrix that we later are going to transpose. Here we are going to use the `matrix()`

function:

```
# Creating a matrix:
mat <- matrix(1:15, ncol = 5)
```

Code language: R (r)

This will produce a matrix with 3 rows and 5 columns. Here’s the result:

Note, in the example above we created a sequence of numbers in R using the `:`

operator. In the next section, we will learn how to transpose the matrix we just created.

## How to Transpose a Matrix in R

Here’s how to transpose a matrix in R with the t() function:

```
# Transpose matrix:
mat.t <- t(mat)
```

Code language: CSS (css)

In the image below, you can see that we now have a transposed matrix. That is, we now have a matrix in which we have rotated the rows and columns. This results that the rows become columns and the columns now are rows:

Now using the this function (i.e., transpose) will also create a new matrix object. We can see this by using the `class()`

function:

```
class(mat.t)
# [1] "matrix" "array"
```

Code language: CSS (css)

In the next section, we will create a dataframe and, in the following section, we will use the `t()`

function to rotate the dataframe, as well.

## Create a Dataframe in R

In this section, we are going to use the functions `data.frame()`

and `matrix()`

to create a dataframe:

```
# Creating a dataframe
dataf <- data.frame(matrix(1:9, ncol = 3))
# Setting the column and row names
colnames(dataf) <- c('A', 'B' ,'C', 'D', 'E')
rownames(dataf) <- c('G', 'H','I')
```

Code language: PHP (php)

Additionally, we set the column names as “A”, “B”, “C”, “D,” and “E” using the `colnames()`

function. Additionally, we set the row names as “E”, “D”, and “F” using the `rownames()`

function. Now, we have a small dataframe that we can rotate!

Naturally, dataframes can be created in many other ways. More common ways are, in fact, to read data from some kind of file format. See the following blog posts to learn more:

- How to Read & Write SPSS Files in R Statistical Environment
- R Excel Tutorial: How to Read and Write xlsx files in R
- How to Read and Write Stata (.dta) Files in R with Haven

Now, that we have a dataframe we can make the columns rows in this dataframe. That is, let’s move to the next section of this tutorial

## How to Transpose a Dataframe in R

To transpose a dataframe in R we can apply exactly the same method as we did with the matrix earlier. That is, we rotate the dataframe with the `t()`

function. Here’s how to rotate a dataframe:

```
# transpose a dataframe
t(dataf)
```

Code language: PHP (php)

Here’s the resulting, transposed, dataframe. Notice how the row names now are the column names:

Note, if your dataframe contains categorical data and you need to change the name of these you can use R to rename the levels of a factor. If we, on the other hand, have an array we cannot rotate it. Here’s what happens if we try:

```
t(x)
x <- array(rep(1, 12*4*2), dim=c(12, 4, 2))
# Error in t.default(x) : argument is not a matrix
```

Code language: PHP (php)

Now, there are many other tasks that you might find yourself in need of doing. For example, if you need to drop variables from your dataframe you can use dplyr to remove columns. Additionally, you can also extract year from datetime in R or create dummy variable in r

## Conclusion: Transposing a Matrix or a Dataframe in R

In this post, you have learned one of the simplest methods to reshape your data. First, you learned how to transpose a matrix. Second, you learned how to rotate a dataframe. Finally, you also learned that you cannot use t() to transpose an array. Finally, along with t() there are other useful R functions worth mentioning such as the repeat and replicate functions.