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.  

Transpose matrix
  • Save

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.

For learning more about useful functions and operators see for example the post about how to use %in% in R.

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)

This will produce a matrix with 3 rows and 5 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.

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)

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:

The transposed matrix
  • Save
The transposed matrix

This will also create a new matrix object. We can see this by using the class() function:

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

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')

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
Dataframe

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 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)

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

Transpose dataframe in R
  • Save
Transposed dataframe

If we, on the other hand, have an array we cannot rotate it. Here’s what happen 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

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.

transpose matrix in R
  • Save
Share via
Copy link
Powered by Social Snap