The Wayback Machine - https://web.archive.org/web/20241006210013/https://www.geeksforgeeks.org/matrix-transpose-in-r/
Open In App

Matrix Transpose in R

Last Updated : 27 Apr, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Transpose of a matrix is an operation in which we convert the rows of the matrix in column and column of the matrix in rows. The general equation for performing the transpose of a matrix is as follows.

Aij = Aji  where i is not equal to j

Example:

Matrix M ---> [1, 8, 9
               12, 6, 2
               19, 42, 3]

Transpose of M
Output --->   [1, 12, 19
               8, 6, 42,
               9, 2, 3]

Transpose of a Matrix can be performed in two ways:

  • Finding the transpose by using the t() function 

Python3




# R program for Transpose of a Matrix
 
# create a matrix with 2 rows
# using matrix() method
M <- matrix(1:6, nrow = 2)
 
# print the original matrix
print(M)
 
# transpose of matrix
# using t() function.
t <- t(M)
 
# print the transpose matrix
print(t)


  • Output:
     [, 1] [, 2] [, 3]
[1, ]    1    3    5
[2, ]    2    4    6

     [, 1] [, 2]
[1, ]    1    2
[2, ]    3    4
[3, ]    5    6
  • By iterating over each value using Loops: 

Python3




# create matrix with 3 rows and 3 columns
Matrix = matrix(1:9, nrow = 3)
 
# print the matrix
print(Matrix)
 
# create another matrix
M2 = Matrix
 
# Loops for Matrix Transpose
for (i in 1:nrow(M2))
{
    # iterate over each row
    for (j in 1:ncol(M2))
    {
        # iterate over each column
        # assign the correspondent elements
        # from row to column and column to row.
        M2[i, j] <- Matrix[j, i]
    }
}
 
# print the transposed matrix
print(M2)


  • Output:
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9


Similar Reads

Calculate the cross-product of the Transpose of a Matrix in R Programming - tcrossprod() Function
tcrossprod() function in R Language is used to return the cross-product of the transpose of the specified matrix. Syntax: tcrossprod(x) Parameters: x: numeric matrix Example 1: # R program to illustrate # tcrossprod function # Initializing a matrix with # 2 rows and 2 columns x <- matrix(1:4, 2, 2) # Getting the matrix representation x # Calling
1 min read
Get Transpose of a Matrix or Data Frame in R Programming - t() Function
t() function in R Language is used to calculate transpose of a matrix or Data Frame. Syntax: t(x) Parameters: x: matrix or data frame Example 1: # R program to illustrate # t function # Getting R Biochemical Oxygen Demand Dataset BOD # Calling t() function t(BOD) Output: Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4 4 16.0 5 5 15.6 6 7 19.8 [, 1] [, 2] [
1 min read
Transpose sparse matrix in R
In R Programming language there's a package called Matrix that's great for dealing with sparse matrices. When you transpose a matrix, you swap its rows and columns. But with sparse matrices, you need to be careful to keep them efficient. What is a Sparse Matrix?Imagine a big table full of numbers, but most of those numbers are zeros. A sparse matri
3 min read
Transpose In R
In this article, we will discuss what is Transpose and perform Transpose operations on a matrix and data frame in R Programming Language. What is Transpose?Transpose is a fundamental operation in linear algebra and data manipulation that involves flipping the rows and columns of a matrix or a data frame. In R programming, the transpose function all
3 min read
How to Convert Sparse Matrix to Dense Matrix in R?
R is a programming language used for performing statistical computation and graphical purposes. It is currently supported and backed by the R Core Team and the R Foundation for Statistical Computing. For data analysis and the creation of statistical software, R Programming Language is used by statisticians, bioinformaticians, and data miners. The p
2 min read
Check if the Object is a Matrix in R Programming - is.matrix() Function
is.matrix() function in R Language is used to return TRUE if the specified data is in the form of matrix else return FALSE. Syntax: is.matrix(x) Parameters: x: specified matrix Example 1: # R program to illustrate # is.matrix function # Specifying some different types of arrays A <- matrix(c(1:5)) B <- matrix(c(1:12), nrow = 4, byrow = TRUE)
1 min read
Convert a Data Frame into a Numeric Matrix in R Programming - data.matrix() Function
data.matrix() function in R Language is used to create a matrix by converting all the values of a Data Frame into numeric mode and then binding them as a matrix. Syntax: data.matrix(df) Parameters: df: Data frame to be converted. Example 1: # R program to convert a data frame # into a numeric matrix # Creating a dataframe df1 = data.frame( "Na
2 min read
Transform the Scaled Matrix to its Original Form in R Programming - Using Matrix Computations
In R programming, a matrix can be scaled and centered using scale() function. But, there is no in-built function to transform the scaled matrix back to the original matrix. In this article, we'll learn to transform the scaled matrix back to the original matrix using some simple computations. Using Matrix Attributes Attributes of the scaled matrix c
3 min read
Convert an Object into a Matrix in R Programming - as.matrix() Function
In R Programming Language as. matrix() function is used to convert an object into a Matrix. Syntax: as.matrix(x) Parameters: x: Object to be converted as.matrix() Function in R ExampleExample 1: Convert vector to the matrix using as.matrix() C/C++ Code # R program to convert an object to matrix # Creating a vector x <- c(1:9) # Calling as.matrix
2 min read
Convert a matrix into a lower triangular matrix using R
In this article, we will explore various methods to convert a matrix into a lower triangular matrix in R programming language. What is a matrix?A matrix is a two-dimensional rectangular data structure, which is a collection of rows and columns. Representation of rows is horizontal and columns are vertical. A matrix can contain data of the same type
5 min read
Getting the Modulus of the Determinant of a Matrix in R Programming - determinant() Function
determinant() function in R Language is a generic function that returns separately the modulus of the determinant, optionally on the logarithm scale, and the sign of the determinant. Syntax: determinant(x, logarithm = TRUE, ...) Parameters: x: matrix logarithm: if TRUE (default) return the logarithm of the modulus of the determinant Example 1: # R
2 min read
Return a Matrix with Lower Triangle as TRUE values in R Programming - lower.tri() Function
lower.tri() function in R Language is used to return a matrix of logical values with lower triangle as TRUE. Syntax: lower.tri(x, diag) Parameters: x: Matrix object diag: Boolean value to include diagonal Example 1: # R program to print the # lower triangle of a matrix # Creating a matrix mat <- matrix(c(1:9), 3, 3, byrow = T) # Calling lower.tr
1 min read
How to Create a Distance Matrix in R?
A distance matrix is a matrix that contains the distance between each pair of elements in a dataset. In R Programming Language, there are several functions available for creating a distance matrix such as dist(), daisy(), and vegdist() from the stats, cluster, and vegan packages respectively. Distance metrics: A distance metric is a function that d
8 min read
Compute Choleski factorization of a Matrix in R Programming - chol() Function
chol() function in R Language is used to compute the Choleski factorization of a real symmetric positive-definite square matrix. Syntax: chol(x, ...)Parameters: x: an object for which a method exists. The default method applies to real symmetric, positive-definite matrices Example 1: C/C++ Code # R program to illustrate # chol function # Initializi
2 min read
Matrix vs Dataframe in R
A data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks.The two most important data structures in R are Matrix and Dataframe, they look the same but different in nature. Matrix in R - It's a homogeneous collection of data sets
3 min read
Inverse of Matrix in R
The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value of unknown variables. The inverse of a matrix is that matrix which when multiplied with the original matrix will give as an identity matrix. Finding the inverse of a matrix is one of the
3 min read
Changing row and column values of a Matrix in R Language - sweep() function
In this article, we will learn changing row and column values of a Matrix in R Programming Language. R - sweep() function sweep() function in R Language is used to apply the operation "+ or -" to the row or column in data matrix. It is used to sweep the values from the data-framework. Syntax: sweep(x, MARGIN, STATS, FUN) Parameters: x: Typically a
2 min read
Create Matrix and Data Frame from Lists in R Programming
In R programming, there 5 basic objects. Lists are the objects that can contain heterogeneous types of elements, unlike vectors. Matrices can contain the same type of elements or homogeneous elements. On the other hand, data frames are similar to matrices but have an advantage over matrices to keep heterogeneous elements. In this article, we'll lea
3 min read
Combine Vectors, Matrix or Data Frames by Columns in R Language - cbind() Function
cbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by columns. Syntax: cbind(x1, x2, ..., deparse.level = 1) Parameters: x1, x2: vector, matrix, data frames deparse.level: This value determines how the column names generated. The default value of deparse.level is 1. Example 1: # R program to illustrate # cbind
2 min read
Calculate the Sum of Matrix or Array columns in R Programming - colSums() Function
colSums() function in R Language is used to compute the sums of matrix or array columns. Syntax: colSums (x, na.rm = FALSE, dims = 1) Parameters: x: matrix or array dims: this is integer value whose dimensions are regarded as ‘columns’ to sum over. It is over dimensions 1:dims. Example 1: # R program to illustrate # colSums function # Initializing
2 min read
Getting a Matrix of number of columns in R Programming - col() Function
col() function in R Language is used to get a matrix which contains the number of columns of the matrix passed to it as argument. Syntax: col(x, as.factor=FALSE) Parameters: x: matrix as.factor: a logical value indicating whether the value should be returned as a factor of column labels (created if necessary) rather than as numbers Example 1: # R p
2 min read
Calculate the Mean of each Column of a Matrix or Array in R Programming - colMeans() Function
colMeans() function in R Language is used to compute the mean of each column of a matrix or array. Syntax: colMeans(x, dims = 1) Parameters: x: array of two or more dimensions, containing numeric, complex, integer or logical values, or a numeric data frame dims: integer value, which dimensions are regarded as ‘columns’ to sum over. It is over dimen
2 min read
Calculate the cross-product of a Matrix in R Programming - crossprod() Function
crossprod() function in R Language is used to return the cross-product of the specified matrix. Syntax: crossprod(x) Parameters: x: numeric matrix Example 1: # R program to illustrate # crossprod function # Initializing a matrix with # 2 rows and 2 columns x <- matrix(1:4, 2, 2) # Getting the matrix representation x # Calling the crossprod() fun
1 min read
Getting the Determinant of the Matrix in R Programming - det() Function
det() function in R Language is used to calculate the determinant of the specified matrix. Syntax: det(x, ...) Parameters: x: matrix Example 1: # R program to illustrate # det function # Initializing a matrix with # 3 rows and 3 columns x <- matrix(c(3, 2, 6, -1, 7, 3, 2, 6, -1), 3, 3) # Getting the matrix representation x # Calling the det() fu
1 min read
Construct a Diagonal Matrix in R Programming - diag() Function
diag() function in R Language is used to construct a diagonal matrix. Syntax: diag(x, nrow, ncol)Parameters: x: value present as the diagonal elements. nrow, ncol: number of rows and columns in which elements are represented. Example 1: C/C++ Code # R program to illustrate # diag function # Calling the diag() function with # some number which will
1 min read
Find String Matches in a Vector or Matrix in R Programming - str_detect() Function
str_detect() Function in R Language is used to check if the specified match of the substring exists in the original string. It will return TRUE for a match found otherwise FALSE against each of the element of the Vector or matrix. Note: This function uses 'stringr' Library. Syntax: str_detect(string, pattern) Parameter: string: specified string pat
1 min read
Perform Operations over Margins of an Array or Matrix in R Programming - apply() Function
apply() function in R Language is used to perform mathematical operations across elements of an array or a matrix. Syntax: apply(x, margin, func) Parameters: x: Array or matrix margin: dimension on which operation is to be applied func: operation to be applied Example 1: # R program to illustrate # apply() function # Creating a matrix A = matrix(1:
2 min read
Naming Rows and Columns of a Matrix in R Programming - rownames() and colnames() Function
rownames() function in R Language is used to set the names to rows of a matrix. Syntax: rownames(x) <- value Parameters: x: Matrix value: Vector of names to be set Example: # R program to provide a name # to rows of a Matrix # Creating a 3X3 Matrix A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), 3, 3, byrow = TRUE) # Calling rownames() Function rowname
1 min read
Return a Matrix with Upper Triangle as TRUE values in R Programming - upper.tri() Function
upper.tri() function in R Language is used to return a matrix of logical values with upper triangle as TRUE. Syntax: upper.tri(x, diag) Parameters: x: Matrix object diag: Boolean value to include diagonal Example 1: # R program to print the # upper triangle of a matrix # Creating a matrix mat <- matrix(c(1:9), 3, 3, byrow = T) # Calling upper.tr
1 min read
Get the position of the maximum element in each Row of a Matrix in R Programming - max.col() Function
max.col() function in R Language check for maximum value in each row and returns the column no. for it. Syntax: max.col(x, ties.method) Parameters: x: Numeric matrix ties.method: It takes random, first, and last as value and returns the position accordingly in case of a tie. Example 1: # R program to find positions # of maximum elements of a matrix
1 min read
Article Tags :