Looking to transform your data in R? Transposing your matrix or dataframe can be the key to unlocking new insights from your data. But how do you transpose in R?

In this tutorial, we will cover everything you need to know about transposing data in R, including creating a matrix and dataframe, and then transposing them with just a few lines of code. You will learn which function is used to create a transpose of a given matrix and how to transpose a matrix and a dataframe. By the end of this tutorial, you will know to quickly transform your data in R and unlock its full potential. Let’s get started!

Transpose matrix
  • Save


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 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 three rows and five columns. Here’s the result:

matrix in R
  • Save

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. Learn more here:

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 becoming columns and the columns now are rows:

The transposed matrix
  • Save
The transposed matrix

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

# [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!

dataframe to rotate
  • Save

Naturally, dataframes can be created in many other ways. More common ways are to read data from a file stored on your hard drive. See the following blog posts to learn more:

Now, that we have a dataframe we can make the rows of the column 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:

Transpose dataframe in R
  • Save
Transposed dataframe

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:

x <- array(rep(1, 12*4*2), dim=c(12, 4, 2))
# Error in t.default(x) : argument is not a matrixCode 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.

  • Save
Share via
Copy link
Powered by Social Snap