Monday, June 18, 2018

How do you represent a 3x3 matrix in R?

Matrix (matrices) is a data type in R used in many mathematical problems. A matrix has rows and columns. A 3 by 3 matrix has 3 rows and 3 columns. A matrix can have only columns, only rows and both columns and rows.

Here is an example of a 3x 3 matrix.

1, 2, 3
4, 5, 6
7, 8, 9

Let us create a matrix which is obtained by arranging the data, a vector of values 1,2,3,4,5,6,7,8,9 by assigning three elements each to a row and create 3 columns from the rows. This is how you do it in R.

Matrix_1

If you want to find the elements of this matrix, you provide the row and column where your element is found as shown.

You find 5 in the 2nd row and 2nd column and you find 6 in 2nd row and 3rd column.

Matrix_2
How do you arrange the same vectors column-wise arranged. You use the same definition as in the previous but omit, byrow attribute as shown.

Matrix_3

This is just the basic but you can do a whole lot more using R.

No comments: