Explain with example some of the ways of creating a matrix in R.

516    Asked by JamesWILLIAMS in Data Science , Asked on Jan 2, 2020
Answered by James WILLIAMS

In R, matrix can be created by many ways

Let us create a matrix in the most simple way

matrix(1:9, nrow = 3, ncol = 3)

Output 


In the above matrix, it can be filled row wise by putting byrow=TRUE

Another way of creating a matrix is to bind the rows and columns by using rbind() and cbind() function respectively.

cbind(c(1,2,3),c(4,5,6),c(7,8,9))

Output

     [,1] [,2] [,3]

[1,] 1 4 7

[2,] 2 5 8

[3,] 3 6 9

rbind(c(1,2,3),c(4,5,6),c(7,8,9))

     [,1] [,2] [,3]

[1,] 1 2 3

[2,] 4 5 6

[3,] 7 8 9



Your Answer

Interviews

Parent Categories