Matrices in R | How to Create, Update, and Modify R Matrices

76

In this tutorial, you will learn all about matrices in R. We will see what a matrix is, how to create matrices, adding data to matrices, and modifying matrices.

Matrix is a two-dimensional data structure in R, which has rows and columns as two dimensions. A row is used to represent the horizontal representation of the data, and a column is used to represent the vertical representation of the data.

Also Read10 Best R Programming Courses On Udemy

How to Create Matrix in R

matrices in r

We can create a matrix in R using the matrix() function.

Syntax:

matrix(data,nrow,ncol)

where,

  1. data is the input vector that holds elements
  2. nrow represents the number of rows that should be present in a matrix.
  3. ncol represents the number of columns that should be present in a matrix.

Example 1: We will create a matrix with 6 integer elements with 2 rows using nrow parameter.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=2)

#display matrix
print(my_matrix)

Output:

     [,1] [,2] [,3]
[1,]   34   78   45
[2,]   56  655   43

Also ReadHow To Learn Data Science [Beginner’s Guide]

Example 2: We will create a matrix with 6 integer elements with 2 columns using ncol parameter.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),ncol=2)

#display matrix
print(my_matrix)

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43

Example 3: We will create a matrix with 6 integer elements with 3 rows and 2 columns using nrow and ncol parameters.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43

Also Read15 Best Python Libraries for Data Science and Analysis

Example 4: We will create a matrix with 6 string elements with 3 rows and 2 columns using nrow and ncol parameters.

#create a matrix with 6 string elements
my_matrix=matrix(c("php","c","cpp","html","java","mysql database"),nrow=3,ncol=2)

#display matrix
print(my_matrix)

Output:

     [,1]  [,2]            
[1,] "php" "html"          
[2,] "c"   "java"          
[3,] "cpp" "mysql database"

Also Read10 Best Data Science Coursera Courses For Beginners

How to access elements in an R matrix

We can access elements in a matrix using the index of elements; indexing starts with 1. By row and column index, we can access a particular element.

Syntax:

my_matrix[row_index,column_index]

where,

  1. my_matrix is the input matrix
  2. row_index represents row index value
  3. column_index represents column index value

Example: We will create an integer matrix with 3 rows and 2 columns and particular access elements in this example.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#access element present in first row and second column
print(my_matrix[1,2])

#access element present in third row and second column
print(my_matrix[3,2])

#access element present in first row and first column
print(my_matrix[1,1])

#access element present in third row and first column
print(my_matrix[3,1])

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
[1] 655
[1] 43
[1] 34
[1] 78

We can access all the elements present in a row by specifying only the row index.

Syntax:

my_matrix[row_index,]

Also ReadWhat Is a Business Intelligence Analyst?

Example: We will create an integer matrix with 3 rows and 2 columns and particular access elements from rows in this example.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#access elements present in first row 
print(my_matrix[1,])


#access elemenst present in third row 
print(my_matrix[3,])

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
[1]  34 655
[1] 78 43

We can access all the elements present in a particular column by specifying only the column index.

Syntax:

my_matrix[,column_index]

Also Read11 Best Free Android Apps To Learn Data Science

Example: We will create an integer matrix with 3 rows and 2 columns and access particular elements from the columns in this example.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#access elements present in first column 
print(my_matrix[,1])


#access elemenst present in second column
print(my_matrix[,2])

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
[1] 34 56 78
[1] 655  45  43

If we want to access more than one row at a time, we can specify row indices in a vector.

Syntax:

my_matrix[c(row_indices),]

Where c() represents a vector that takes row indices.

Example:

In this example, we will access elements in a matrix from more than one row at a time.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#access elements present in first, second  and third rows
print(my_matrix[c(1,2,3),])


#access elements present in first and third rows
print(my_matrix[c(1,3),])

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
     [,1] [,2]
[1,]   34  655
[2,]   78   43

If we want to access more than one column at a time, we can specify column indices in a vector.

Syntax:

my_matrix[,c(column_indices)]

Where c() represents a vector that takes column indices.

Also Read10 Best Books On Data Science For Beginners [2022]

Example: In this example, we will access elements in a matrix from more than one column at a time.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#access elements present in first, second  columns
print(my_matrix[,c(1,2)])

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43

How to add a row in an R matrix

We can add a row to the existing matrix using the rbind() function.

Syntax:

rbind(my_matrix, elements)

where,

  1. my_matrix is the input matrix
  2. elements represent row elements to be added through a vector.

Example:

In this example, we will add a row to the existing matrix.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#add a row with integer elements
my_matrix=rbind(my_matrix, c(90,100))

#display matrix
print(my_matrix)

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
[4,]   90  100

Also ReadGetting Started With PySpark on Ubuntu with Jupyter Notebook

How to add a column in an R matrix

We can add a column to the existing matrix using the cbind() function.

Syntax:

cbind(my_matrix, elements)

where,

  1. my_matrix is the input matrix
  2. elements represent column elements to be added through a vector.

Example:

In this example, we will add a column to the existing matrix.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#add a column with integer elements
my_matrix=cbind(my_matrix, c(90,100,110))

#display matrix
print(my_matrix)

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
     [,1] [,2] [,3]
[1,]   34  655   90
[2,]   56   45  100
[3,]   78   43  110

Also Read: Introduction to Data Analysis and Visualization with Excel

How to remove elements in an R matrix

We can remove elements in a matrix using the index of elements. Indexing starts with 1. By row and column index, we can remove a particular element.

Syntax:

my_matrix[-row_index,-column_index]

where,

  1. my_matrix is the input matrix
  2. row_index represents row index value
  3. column_index represents column index value.

Example 1: In this example, we will remove the first row.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#remove first row
my_matrix=my_matrix[-1,]


#display matrix
print(my_matrix)

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
     [,1] [,2]
[1,]   56   45
[2,]   78   43

Example 2: In this example, we will remove the first column.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#remove first column
my_matrix=my_matrix[,-1]


#display matrix
print(my_matrix)

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
[1] 655  45  43

Also ReadIntroduction to Data Science and Analytics

How to check if elements exist in an R matrix

We can check if an element exists in a  matrix or not by using the %in% operator. If an element exists, it will return TRUE, else FALSE.

Syntax:

element %in% my_matrix

Example:

In this example, we will check whether the given element exists in a matrix or not.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display matrix
print(my_matrix)

#check 655 present in matrix or not
print(655 %in% my_matrix)

#check 34 present in matrix or not
print(34 %in% my_matrix)

#check 567 present in matrix or not
print(567 %in% my_matrix)

Output:

     [,1] [,2]
[1,]   34  655
[2,]   56   45
[3,]   78   43
[1] TRUE
[1] TRUE
[1] FALSE

Also Read: 10 Best Websites To Draw UML Class Diagrams Online [Free]

How to get R matrix dimensions

We will get the number of rows and columns in a matrix using the dim() function.

Syntax:

dim(my_matrix)

Example:

Get the dimensions of the matrix.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display the dimensions of the matrix
print(dim(my_matrix))

Output:

[1] 3 2

If we want to get the total number of elements in a matrix, we will use the length() function.

Syntax:

length(my_matrix)

Example:

Get the total number of elements from a matrix.

#create a matrix with 6 values
my_matrix=matrix(c(34,56,78,655,45,43),nrow=3,ncol=2)

#display the length of the matrix
print(length(my_matrix))

Output:

[1] 6

Also Read15 Best Websites to Learn Android App Development

Wrapping Up

Here you explore everything about matrices in R. From this tutorial, you learned how to create a matrix with rows and columns, add rows and columns to a matrix, delete rows and columns from a matrix and get the total number of elements in a matrix we also discussed how to check whether the element exists in a matrix or not.