To get the number of rows in a matrix in R, you can use the nrow()
function, which returns the number of rows present in the given matrix.
In this example,
matrix1
using the matrix()
function and specify the elements and dimensions. The matrix()
function takes a vector of values and arranges them into the specified number of rows and columns.nrow()
function to get the number of rows in matrix1
. The nrow()
function takes the matrix as an argument and returns the number of rows.numRows
.numRows
using the print()
function to display the number of rows in the matrix.matrix1 <- matrix(1:9, nrow = 3, ncol = 3)
numRows <- nrow(matrix1)
print(paste("Number of rows in matrix1 is:", numRows))
Number of rows in matrix1 is: 3
In this example,
matrix2
using the matrix()
function and specify the elements and dimensions. The matrix()
function takes a vector of values and arranges them into the specified number of rows and columns.nrow()
function to get the number of rows in matrix2
. The nrow()
function takes the matrix as an argument and returns the number of rows.numRows
.numRows
using the print()
function to display the number of rows in the matrix.matrix2 <- matrix(1:8, nrow = 4, ncol = 2)
numRows <- nrow(matrix2)
print(paste("Number of rows in matrix2 is:", numRows))
Number of rows in matrix2 is: 4
In this example,
matrix3
using the matrix()
function and specify the elements and dimensions. The matrix()
function takes a vector of values and arranges them into the specified number of rows and columns.nrow()
function to get the number of rows in matrix3
. The nrow()
function takes the matrix as an argument and returns the number of rows.numRows
.numRows
using the print()
function to display the number of rows in the matrix.matrix3 <- matrix(1:25, nrow = 5, ncol = 5)
numRows <- nrow(matrix3)
print(paste("Number of rows in matrix3 is:", numRows))
Number of rows in matrix3 is: 5
In this tutorial, we learned How to get Number of Rows in Matrix in R language with well detailed examples.