R Func Dim
# R dim() nrow() ncol() Functions - View Dimensions
[ R Language Examples](https://example.com/r/r-examples.html)
The R dim() function is used to get or set the dimensions (number of rows and columns) of a matrix/data frame.
nrow() and ncol() are used to get the number of rows and columns separately.
The syntax of these functions is as follows:
dim(x) nrow(x) ncol(x)
**Parameter Description:**
* **x** Input matrix, data frame, or array.
## Example
# Create a matrix
m <-matrix(1:12, nrow=3, ncol=4)
print("Matrix:")
print(m)
# View dimensions
print(paste("Dimensions dim:", toString(dim(m))))
print(paste("Rows nrow:", nrow(m)))
print(paste("Columns ncol:", ncol(m)))
# Modify matrix shape using dim
vec <-1:12
dim(vec)<-c(3, 4)
print("Vector becomes 3x4 matrix:")
print(vec)
The output of the above code is:
"Matrix:" [,1] [,2] [,3] [,4][1,] 1 4 7 10[2,] 2 5 8 11[3,] 3 6 9 12 "Dimensions dim: 3, 4" "Rows nrow: 3" "Columns ncol: 4" "Vector becomes 3x4 matrix:" [,1] [,2] [,3] [,4][1,] 1 4 7 10[2,] 2 5 8 11[3,] 3 6 9 12
[ R Language Examples](https://example.com/r/r-examples.html)
[R dnorm() pnorm() qnorm() Functions - Normal Distribution Family](https://example.com/r/r-func-dnorm-pnorm.html)[](https://example.com/r/r-func-dnorm-pnorm.html)
YouTip