To find the transpose of a matrix in R, you can use the t()
function. This function takes a matrix as input and returns its transpose.
In this example,
matrix1
using the matrix()
function with a vector of values c(1, 2, 3, 4)
and specify the number of rows as 2.t()
function on matrix1
and store the result in a new variable called transposeMatrix1
.t()
function switches the rows and columns of the matrix.print()
function to display the transposed matrix stored in transposeMatrix1
.matrix1 <- matrix(c(1, 2, 3, 4), nrow = 2)
transposeMatrix1 <- t(matrix1)
print(transposeMatrix1)
[,1] [,2] [1,] 1 3 [2,] 2 4
In this example,
matrix2
using the matrix()
function with a vector of values c(1, 2, 3, 4, 5, 6)
and specify the number of rows as 3.t()
function on matrix2
and store the result in a new variable called transposeMatrix2
.t()
function switches the rows and columns of the matrix.print()
function to display the transposed matrix stored in transposeMatrix2
.matrix2 <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3)
transposeMatrix2 <- t(matrix2)
print(transposeMatrix2)
[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6
In this example,
matrix3
using the matrix()
function with a vector of values c(7, 8, 9, 10, 11, 12)
and specify the number of rows as 2.t()
function on matrix3
and store the result in a new variable called transposeMatrix3
.t()
function switches the rows and columns of the matrix.print()
function to display the transposed matrix stored in transposeMatrix3
.matrix3 <- matrix(c(7, 8, 9, 10, 11, 12), nrow = 2)
transposeMatrix3 <- t(matrix3)
print(transposeMatrix3)
[,1] [,2] [1,] 7 10 [2,] 8 11 [3,] 9 12
In this tutorial, we learned How to find Transpose of a Matrix in R language with well detailed examples.