Want to Know How to Enter a Matrix in R but Have Only Two Minutes to Spare?

If yes, here is a quick tutorial…

Let’s begin by understanding a Matrix. Well, a Matrix is an array of numbers, in a combination of rows and columns. The size of a matrix is defined by the number of rows and columns that it contains.  A matrix with an m rows and n columns is called an m x n matrix or m-by-n matrix, while m and n are called its dimensions, denoted by [a_ij]

Entering a Matrix in R and understanding the tech jargon

Syntax: M=matrix(elements,dimension,arrangement)

Elements: are the entries to be entered in a matrix.

Dimension:  defines the number of rows and columns in a matrix.

Arrangement: defines how do you want the enter the matrix(row wise or column wise).

Example: Enter the range of the values 1 to 6 in a 2×3 matrix

2×3 matrix will have 2 rows and three columns

M1=matrix(1:6,nrow=2,byrow=TRUE)

To enter the values in two rows we use nrow = 2 . To enter values in columns use ncol = “integer”.byrow =True  tells R to enter the values row wise.

Picture4

We can also enter the elements using c()  function.

Note: R will take the values column wise in default. If we don’t specify “arrangement” condition and dimension then it will be a 6×1 matrix.

Entering an identity matrix in R

To enter an n dimension identity matrix in R we use diag (n) function.

Example: Let n =3

I-diag(3), this will enter a 3×3 matrix of dimension 3.

 

Picture2

Operations in Matrices:

To add and subtract the matrices with same number of rows and columns. We use plus, minus and multiplication.

Let A and B be two matrices with same number of rows and columns.

Picture11

M2=A+B

M3=A-B

M4=A*B

Note: You can see that these are vector wise addition, subtraction and multiplication.

Matrix multiplication: In order to multiply two matrices, A and B, the number of columns in A must equal the number of rows in B. Thus, if A is an m x n  matrix and B is an r x s matrix, .  n = r

Syntax: A% * %B

Picture9

Note:

  1. If we just use the multiplication operator *, R will multiply the corresponding elements of the two matrices, provided they have the same dimension.
  2. Transpose of a matrix can be found using tr(A)
  3. Determinant of a matrix can be calculated using det(A)
  4. Inverse of a matrix is determined by solve (A), if f det(A) is non zero.

Solve a System of Equations

Syntax: solve (A,b)

A, a square numeric or complex matrix containing the coefficient of linear system and b a matrix which gives the right hand side of linear system.

Consider the linear equations x+y+z=6, 2y+5z=-4, 2x+5y-z=27.

Picture10

If  is missing in the function solve (A,b) then b is taken to be identity matrix and result will be inverse of  A.

Hope this was useful. If you have any questions, feel free to write in. And watch this space for many more useful guides to all things related to data and analytics and stats!

Interested in learning about other Analytics and Big Data tools and techniques? Click on our course links and explore more.
 Jigsaw’s Data Science with SAS Course – click here.
 Jigsaw’s Data Science with R Course – click here.
Jigsaw’s Big Data Course – click here.

Suggested Read:

Faster Versions of R

Stringi Package in R

Related Articles

loader
Please wait while your application is being created.
Request Callback