Get or Set Dimensions of a Matrix in R Programming - dim() Function Last Updated : 18 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The dim() function in R is used to get or set the dimensions of an object. This function is particularly useful when working with matrices, arrays, and data frames. Below, we will discuss how to use dim() to both retrieve and set dimensions, with examples for better understanding.Syntax: dim(x)Parameters:x: array, matrix or data frame.Now we will discuss this with an example in R Programming Language. R # R program to illustrate # dim function # Getting R Biochemical Oxygen Demand Dataset BOD # Getting dimension of the above dataset dim(BOD) Output: Time demand1 1 8.32 2 10.33 3 19.04 4 16.05 5 15.66 7 19.8[1] 6 2Getting Dimensions of a MatrixThe dim() function returns the dimensions of an object as an integer vector. For a matrix, it returns the number of rows and columns. R # Create a matrix matrix_example <- matrix(1:12, nrow = 3, ncol = 4) # Print the matrix print(matrix_example) # Get the dimensions of the matrix dimensions <- dim(matrix_example) print(dimensions) Output: [,1] [,2] [,3] [,4][1,] 1 4 7 10[2,] 2 5 8 11[3,] 3 6 9 12[1] 3 4ConclusionThe dim() function in R is a versatile tool for getting and setting the dimensions of matrices, arrays, and data frames. Whether you need to reshape a vector into a matrix or simply check the size of an existing matrix, dim() provides an easy and efficient way to handle dimensions in R. Comment More infoAdvertise with us Next Article Get or Set Dimensions of a Matrix in R Programming - dim() Function K Kanchan_Ray 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 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 2 min read Scale the Columns of a Matrix in R Programming - scale() Function The scale() function in R is used to center and/or scale the columns of a numeric matrix. It helps standardize data by transforming it to have a mean of 0 and standard deviation of 1 .Syntax scale(x, center = TRUE, scale = TRUE) Parameters: x: represents numeric matrix .center: represents either log 2 min read Finding Inverse of a Matrix in R Programming - inv() Function inv() function in R Language is used to calculate inverse of a matrix. Note: Determinant of the matrix must not be zero Syntax: inv(x) Parameters: x: Matrix Example 1: Python3 1== # R program to calculate # inverse of a matrix # Loading library library(matlib) # Create 3 different vectors. a1 <- 1 min read Getting a Matrix of number of rows in R Programming - row() Function In this article, we will discuss how to find the number of rows and columns in a matrix in R Programming Language. nrow() Function in Rnrow() function in R Language is used to get the row number of a matrix. Syntax: row(x, as.factor=FALSE) Parameters:x: matrixas.factor: a logical value indicating w 2 min read Like