Calculate Trace of a Matrix in R Programming - tr() Function Last Updated : 19 Jun, 2020 Comments Improve Suggest changes Like Article Like Report tr() function in R Language is used to calculate the trace of a matrix. Trace of a matrix is the sum of the values on the main diagonal(upper left to lower right) of the matrix. Syntax: tr(x) Parameters: x: Matrix Example 1: Python3 1== # R program to calculate # trace of a matrix # Loading library library(psych) # Creating a matrix A = matrix( c(6, 1, 1, 4, -2, 5, 2, 8, 7), nrow = 3, ncol = 3, byrow = TRUE ) A # Calling tr() function cat("Trace of A:\n") tr(A) Output: [, 1] [, 2] [, 3] [1, ] 6 1 1 [2, ] 4 -2 5 [3, ] 2 8 7 Trace of A: [1] 11 Example 2: Python3 1== # R program to calculate # trace of a matrix # Loading library library(psych) # Creating a matrix A = matrix(c(1:9), 3, 3) A # Calling tr() function cat("Trace of A:\n") tr(A) Output: [, 1] [, 2] [, 3] [1, ] 1 4 7 [2, ] 2 5 8 [3, ] 3 6 9 Trace of A: [1] 15 Comment More infoAdvertise with us Next Article Calculate Trace of a Matrix in R Programming - tr() Function N nidhi_biet Follow Improve Article Tags : R Language R Matrix-Function Similar Reads 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: Python3 # 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 1 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: Python3 # 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: Python3 # R program to illustrate # crossprod function # Initializing a matrix with # 2 rows and 2 columns x <- matrix(1:4, 2, 2) # Getting 1 min read Calculate the Mean of each Row of an Object in R Programming â rowMeans() Function rowMeans() function in R Language is used to find out the mean of each row of a data frame, matrix, or array. Syntax: rowMeans(data) Parameter: data: data frame, array, or matrix Example 1 Python3 1== # R program to illustrate # rowMean function # Create example values # Set seed set.seed(1234) # Cr 1 min read 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: Python3 # R program to illustrate # tcrossprod function # Initializing a matrix with # 2 rows and 2 columns x <- matrix( 1 min read Like