You are on page 1of 11

WORKING WITH MATRIX

What is Matrix?

In R, a matrix is a collection of elements of the same data type such as numeric, character, or
logical). Moreover, these elements are arranged into a fixed number of rows and columns
(e.g., 3 rows and 3 columns, 10 rows, and 2 columns). This type of data is 2-dimensional.
These are some examples of matrices:

\ 5 3' 4 9
f
[l 4 5]
2 ,5 6 7j

Syntax of the Matrix() Function:


1 matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE
2 dimnames = NULL)

As you can see, there are 5 parameters that you can use:

data - this argument is optional and will contain e.g. a vector of your data

nrow - this argument is used to get the number of rows, you want
ncol - this argument is, like nrow, but for the number of columns you want in your created
matrix
byrow - The byrow parameter is a logical clue. If its value is true, then the input vector
elements are arranged by row.
dimnames - this argument is used if you want to name the columns and rows
How to Create a Matrix in R ?

1 mtx <- matrix(seq(l,9),


2 nrow = 3, ncol = 3)
3
4

As you can see, in the above code, we used the seq() function to create a sequence in R (i.e., a
vector). Moreover, we used the nrow and ncol arguments to tell the matrix() function that we
want to create a three by three matrix. Here's how the matrix, called mtx, look like:

Create a Matrix in R by rows:

Here's how we can create a matrix in R, from a sequence of numbers (i.e., a vector) and get
the numbers by rows:

1 mtx <- matrix(seq(l,9),


2 nrow = 3, ncol = 3, byrow =
3 TRUE)
4
5
> mtx <- matrix(seq(l,9),
+ nrow - 3, MAKING A
+ ncol = 3, * MATRIX BY
+ byrow = TRUE) ROW
> mtx
[,1] [,2] [,3]
[1J 1 2 3
[2,] 4 5 6
[3,] 7 8 9

Create a Matrix from vectors in R using the cbind() Function:

Here's how to use the cbind() function to produce a matrix in R:

Syntax : mtx <- cbind(seq(1,3), seq(4, 6),

seq(7, 9))

> mtx<- seq(4, 6),


cbind(seq(l,3),
+ seq(7,9)) ^ CREATING A
> ^ MATRIX IN R
> mtx USING CBINDO
C,1] c,2][»3]
Cl,] 1 4 7
[2,] 2 5 8

[3,] 3 6 9 THE CREATED


> MATRIX

Create a Matrix from vectors in R using the rbind() Function:

Here's how to use the rbind() function to make a matrix from vectors in R:

Syntax : mtx <- rbind(seq(1,3), seq(4, 6),


seq(7, 9))

> mtx <- rbind(seq(l,3), seq(4, 6),


seq(7, 9))
> mtx CREATING A
[,1] C12] [,3] MATRIX FROM
VECTORS IN R
[1,] 1 2 3 USING RBIND()
[2,] 4 5 6
[3,] 789

How to Create an Empty Matrix in R ?


The term empty matrix has no rows and no columns. A matrix that contains missing values has
at least one row and column, as does a matrix that contains zeros

There are three ways of creating an empty matrix:

Using row and column.

Using only row.

Using only column.

Method 1: Using both row and column:

Here in this, we need to pass both row and column to create an empty matrix: we need to

pass both row and column to create an empty matrix

Syntax: matrix name = matrix(, nrow = value 1, ncol = value2)


1 # creating empty matrix,
2 # storing in variable mat and passing
3 # number of rows and columns
4 mat = matrixC, nrow = 1, ncol T—1
C II
3
6 # printing empty matrix.
7 print(mat)

Output:

[,1]
[1,] NA
> I

Here we got NA as output which means not a number or not available.

Another Example with 10 rows and 10 columns

1 # creating empty matrix,


2 # storing in variable matl and passing
3 # number of rows and columns
4 matl = matrixC, nrow = 10, ncol = 10)
5
6 # printing empty matrix.
7 print(matl)
S
9

Output:
[pi] CP2] [ T 3] [ i 4] [,5] [,6] [ i 7] [ i 8][ > 9]
C.io]
[1,] NA NA NA NA NA NA NA NA NA NA
[2,] NA NA NA NA NA NA NA NA NA NA
[3,] NA NA NA NA NA NA NA NA NA NA
[4,] NA NA NA NA NA NA NA NA NA NA
[5,] NA NA NA NA NA NA NA NA NA NA
[6,] NA NA NA NA NA NA NA NA NA NA
[7,] NA NA NA NA NA NA NA NA NA NA
[S,] NA NA NA NA NA NA NA NA NA NA
[9,] NA NA NA NA NA NA NA NA NA NA
[10,] NA NA NA NA NA NA NA NA NA NA

Method 2: Using only row :


Here we need to pass the only one row to create an empty matrix

Syntax: matrix name = matrix(, nrow = value 1)

Where,

Here matrix name can be any valid identifier

value 1 is for number of rows.

Example:

1
2 Matc-matrix(,nrow=10)
3
4 # printing empty matrix
5 print(Mat)

Output:
[,1]
[1,]
NA
[2,]
NA
[3,]
NA
[4,]
NA
15,1
NA
[6,]
NA
[7,]
NA
Method 3: Using only column.
[8,]
NA
[9,]
Here we need
NA to pass the only columns to create an empty matrix.
[10,
] NA

Syntax: matrix name = matrix(, ncol = value 1)

Where,
Here matrix name can be any valid identifier

Value 1 is for number of column.

Example:
1 Mat<-matrix(,ncol=10)
2
3 #printing empty matrix.
4 print(Mat)
5 I
Output:
[,£] [’,2] [i 3] [,4] [, 5] [,6] [,7] [,8] [,9] [,10] [1,] NA NA NA NA NA NA NA NA NA
NA
How to Name the Rows and Columns when Creating a Matrix in R ?

to name the rows and columns when creating a matrix we use the dimnames argument

1 mtx <- matrix(seq(l,9),


2 nrow = 3,
3 ncol = 3,
4 dimnames = 1ist(c("l", 3"),
5 c C'vec List", "Matrix")))

In the code chunk above, we named the rows something very simple: 1, 2, and 3. Just for
fun, we named the columns "Vector", "List", and "Matrix".
mtx <- matrix(seq(l,9) ,
NAMING THE ROWS AND
nrow = 3, / COLUMNS WHEN
ncol = B, CREATING A MATRIX IN R

dimnames =list(c("l", "2"


c("Vector",t "List", "Matrix")))
mtx
Vector List Matrix
1 4 7
2 5 8-THE MATRIX
B 6 9
>

Accessing matrix elements in R

Like C and C++, we can easily access the elements of our matrix by using the index of the
element. There are three ways to access the elements from the matrix.

We can access the element which presents on nth row and mth column.

We can access all the elements of the matrix which are present on the nth row.

We can also access all the elements of the matrix which are present on the mth column.
Let see an example to understand how elements are accessed from the matrix present on
nth row mth column, nth row, or mth column.
1 # Defining the column and row names. row_names = cC'rowl'1, "row2",
2 "row3", ,Trow4") col_names = c("callM, "col2", "col3")
3 ^Creating matrix
4 R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names,
5 col_names))
6
7 pri nt(R)
8 I
9
^Accessing element present on 3rd row and 2nd column
1
print(R[3,2])
0
1 ^Accessing element present in 3rd row
1 printCR[3,])
1
2 ^Accessing element present in 2nd column
1 print(R[,2])
3
1
4
1
5
Output:
1
6
> ^Creating matrix
> R <- matrix(c(5:16), nrow = byrow = TRUE, dimnames = list(row_names, col_name
s))
4,
> print(R)
coll col 2 col 3
rowl 5 S 7
row 2 8 9 10
row 3 11 12 13
row4 14 15 16

> #Accessing element present on 3rd row and 2nd column


> print(R[3,2])
[1] 12
>
> #Accessing element present in 3rd row
> pri nt(R[3,]) coll col2 col3
11 12 13

> #Accessing element present in 2nd column


> pri nt(R [,2]) rowl row2 row3 row4
6 9 12 IS

Modification of the matrix:


R allows us to do modification in the matrix. There are several methods to do
modification in the matrix, which are as follows:
Modification methods

Addition of rows
and columns

Use of relation
operators

Assign a single element:


In matrix modification, the first method is to assign a single element to the matrix at a
particular position. By assigning a new value to that position, the old value will get replaced
with the new one. This modification technique is quite simple to perform matrix modification.
The basic syntax for it is as follows:

matrix[n, m]<-y

Here, n and m are the rows and columns of the element, respectively. And, y is the value which
we assign to modify our matrix.

Let see an example to understand how modification will be done:

Example:
1 # Defining the column and row names.
2 row_names = cC'rowl", "row2", "row3", "row4")
3 col_names = c("coll", "col2", "col3")
4
5 R <- matrix(c(5:16), nrow = 4, byrow = TRUE, dimnames = list(row_names,
6 col_names))
7 print(R)
S
9 ^Assigning value 20 to the element| at 3d row and 2nd column LO R[3,2]<-20 LI
print(R)

Output:

You might also like