

- returns the entire element of n1 and n2 row.We can access more than one row or column in R using the c() function. matrix1 - access entire elements at 2nd row i.e.matrix1 - access entire elements at 1st row i.e.- returns the entire element of the nth column.įor example, matrix1 - returns the entire element of the nth row.

In R, we can also access the entire row or column based on the value passed inside. Here, specifies we are trying to access element present at 1st row, 2nd column i.e. Notice the use of index operator, matrix1 In the above example, we have created a 2 by 2 matrix named matrix1 with 4 string type datas. Let's see an example, matrix1 <- matrix(c("Sabby", "Cathy", "Larry", "Harry"), nrow = 2, ncol = 2) The syntax to access a matrix element is matrix We use the vector index operator to access specific elements of a matrix in R. If we didn't pass byrow argument as matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3) Since we have passed byrow = TRUE, the data items in the matrix are filled row-wise. And nrow = 2 and ncol = 3 means the matrix has 2 rows and 3 columns. Here, we have passed data items of integer type and used c() to combine data items together. In the above example, we have used the matrix() function to create a matrix named matrix1.

Let's see an example, # create a 2 by 3 matrix By default, the matrix is filled column-wise.
