{"id":75252,"date":"2020-01-08T15:00:09","date_gmt":"2020-01-08T09:30:09","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=75252"},"modified":"2020-01-08T15:00:09","modified_gmt":"2020-01-08T09:30:09","slug":"r-matrix","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/","title":{"rendered":"R Matrix &#8211; How to create, name and modify matrices in R?"},"content":{"rendered":"<p><strong>In this TechVidvan tutorial, you will explore a matrix in R with examples. You will learn to create, modify, and access R matrix components.<\/strong><\/p>\n<p>A matrix is a <strong>two-dimensional<\/strong>, <strong>homogeneous<\/strong> data structure in R. This means that it has two dimensions, rows and columns.<\/p>\n<p>A matrix can <strong>store<\/strong> data of a <strong>single basic type<\/strong> (numeric, logical, character, etc.). Therefore, a matrix can be a combination of two or more vectors.<\/p>\n<p>In this article, we will learn about matrices in R. We will explore the various methods to create matrices and also learn how to access their elements and how to modify them.<\/p>\n<p>We will also look at how to convert vectors into matrices and vice versa. Finally, we will look at how R handles matrix arithmetic.<\/p>\n<h2>How to create a matrix in R?<\/h2>\n<p>We can create matrics using the <strong><code>matrix()<\/code><\/strong> function. The syntax of the <strong><code>matrix()<\/code> <\/strong>function is:<\/p>\n<p><strong><code>matrix(data,byrow,nrow,ncol,dimnames)<\/code><\/strong><\/p>\n<p>The arguments in the matrix function are the following:<\/p>\n<ol>\n<li><strong>data<\/strong> &#8211; data contains the elements in the R matrix.<\/li>\n<li><strong>byrow<\/strong> &#8211; byrow is a logical variable. Matrices are by default column-wise. By setting byrow as TRUE, we can arrange the data row-wise in the matrix.<\/li>\n<li><strong>nrow<\/strong> &#8211; defines the number of rows in the R matrix.<\/li>\n<li><strong>ncol<\/strong> &#8211; defines the number of columns in the R matrix.<\/li>\n<li><strong>dimnames<\/strong> &#8211; takes two character arrays as input for row names and column names.<\/li>\n<\/ol>\n<h3>1. Using matrix() Function<\/h3>\n<p>Here is an example of creating a matrix with the <strong><code>matrix()<\/code><\/strong> function:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat1.data &lt;- c(1,2,3,4,5,6,7,8,9)\n&gt; mat1 &lt;- matrix(mat1.data,nrow=3,ncol=3,byrow=TRUE)\n&gt; mat1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-mat1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75277\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-mat1.png\" alt=\"creating R matrix\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Note &#8211;<\/strong> It is not necessary to specify both <strong><code>nrow<\/code><\/strong> and <strong><code>ncol.<\/code><\/strong> Only one of the two is required. R calculated the other based on the length of the data. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2.data &lt;- c(10,11,12,13,14,15,16,17,18)\n&gt; mat2 &lt;- matrix(mat2.data,nrow=3)\n&gt; mat2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-mat2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75283\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-mat2.png\" alt=\"creating R matrix\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><em><strong>Note-<\/strong> Notice how without the<\/em> <strong><code>byrow=TRUE<\/code><\/strong><em> argument, the matrix<\/em> <strong><code>mat2<\/code><\/strong><em> is column-wise.<\/em><\/p>\n<h3>2. Using rbind() or cbind() Function<\/h3>\n<p>Another way of creating an R matrix is to combine vectors as rows or columns using the <strong><code>rbind()<\/code><\/strong> or <strong><code>cbind()<\/code><\/strong> functions. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat3.data1 &lt;- c(1,2,3)\n&gt; mat3.data2 &lt;- c(4,5,6)\n&gt; mat3.data3 &lt;- c(7,8,9)\n&gt; mat3 &lt;- cbind(mat3.data1,mat3.data2,mat3.data3)\n&gt; mat3<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-using-cbind-mat3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75285\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-using-cbind-mat3.png\" alt=\"creating a R matrix using cbind()\" width=\"1299\" height=\"741\" \/><\/a><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat4 &lt;- rbind(mat3.data1,mat3.data2,mat3.data3)\n&gt; mat4<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-using-rbind-mat4-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75290\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-using-rbind-mat4-1.png\" alt=\"creating a matrix using rbind\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>3. Using dim() Function<\/h3>\n<p>We can also create an R matrix by changing a vector\u2019s dimensions using the <strong><code>dim()<\/code><\/strong> function. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat5 &lt;- c(2,3,5,9)\n&gt; mat5<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; dim(mat5) &lt;- c(2,2)\n&gt; mat5<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-using-dim-mat5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75289\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-creating-a-matrix-using-dim-mat5.png\" alt=\"creating R matrix using dim()\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>How to name the rows and columns of an R matrix?<\/h3>\n<p>It is possible to name the rows and columns of the matrix during creation with the <strong><code>dimnames<\/code><\/strong> argument of the <strong><code>matrix()<\/code><\/strong> function. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; rnames &lt;- c(\"row1\",\"row2\",\"row3\")\n&gt; cnames &lt;- c(\"col1\",\"col2\",\"col3\")\n&gt; named_matrix &lt;- matrix(mat1.data,nrow=3,byrow=TRUE,dimnames=list(rnames,cnames))\n&gt; named_matrix<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-naming-rows-and-columns-dimnames.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75307\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-naming-rows-and-columns-dimnames.png\" alt=\"naming rows and columns dimnames in R matrix\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can also name the rows and columns of an R matrix after its creation by using the <strong><code>rownames()<\/code><\/strong> or <strong><code>colnames()<\/code><\/strong> functions. For example:<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; rownames(mat1) &lt;- c(\"r1\",\"r2\",\"r3\")\n&gt; colnames(mat1) &lt;- c(\"c1\",\"c2\",\"c3\")\n&gt; mat1<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-naming-rownames-and-colnames.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75308\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-naming-rownames-and-colnames.png\" alt=\"naming rownames() and colnames() in R matrix\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>How to access individual components of the R matrix?<\/h3>\n<p>We can access the elements of a matrix named mat using square brackets as <strong><code>mat[row, col]<\/code><\/strong>, where row and col are the vectors. We can use the vector indexing techniques to index a matrix, similar to vector indexing.<\/p>\n<h4>Using integer vectors<\/h4>\n<p>We can specify the needed row and column numbers in vectors and use them to index a matrix. If we leave the row or column field blank inside the square brackets, R selects all the rows\/columns. We can also use negative integers to exclude rows or columns. If we select a single row or column this way, the result is a vector and not a matrix. We can use the <strong><code>drop=FALSE<\/code><\/strong> argument to avoid this behavior.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[1,2] #first row second column of mat2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] 13<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(1,3),c(2,3)] #first and third row and second and third column<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\u00a0 \u00a0 \u00a0 \u00a0 [,1]\u00a0 \u00a0[,2]<br \/>\n[1,]\u00a0 \u00a0 13\u00a0 \u00a016<br \/>\n[2,]\u00a0 \u00a0 15\u00a0 \u00a018<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[2, ] #second row of mat2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[1] 11 14 17<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[2, , drop=FALSE] #second row of mat2 with drop=FALSE<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\u00a0 \u00a0 \u00a0 \u00a0[,1]\u00a0 \u00a0[,2]\u00a0 \u00a0[,3]<br \/>\n[1,]\u00a0 \u00a011\u00a0 \u00a014\u00a0 \u00a0 \u00a017<\/div>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-positive-integers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75293\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-positive-integers.png\" alt=\"indexing using positive integers\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(-1),c(-2,-3),drop=FALSE] #first row and second and third column are excluded<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/mtrices-i-r-indexing-using-negative-integers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75294\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/mtrices-i-r-indexing-using-negative-integers.png\" alt=\"indexing using negative integers - R matrix\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>Using logical vectors<\/h4>\n<p>We can use logical vectors of the same lengths as the matrix\u2019s dimensions to specify the rows and columns needed. This excludes the row\/columns corresponding to <strong><code>FALSE<\/code><\/strong> and includes the one corresponding to <strong><code>TRUE<\/code>.<\/strong><\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(TRUE,FALSE,TRUE),c(FALSE,TRUE,TRUE)]<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-logical-vector.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75295\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-logical-vector.png\" alt=\"indexing using logical R vector\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>When using logical vectors for indexing, R recycles the vector, if the length of the indexing vector is not equal to the matrix\u2019s dimensions.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(TRUE,FALSE),c(FALSE,TRUE,TRUE)] #the row vector is recycled to 3 elements.<\/pre>\n<p><strong>Output:<\/strong><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-logical-vectors-recycling.png\"><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75296\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-logical-vectors-recycling.png\" alt=\" indexing using logical R vectors recycling\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>Using character vectors<\/h4>\n<p>For matrices with named rows and columns, we can use the row and column names to access elements of the R matrix.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; named_matrix[c(\"row1\",\"row2\"),c(\"col2\",\"col3\")]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-character-vectors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75297\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-character-vectors.png\" alt=\"indexing using character R vectors\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h4>Using combination of indexing<\/h4>\n<p>We can also use these indexing techniques in combination with one another as well.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(1,3),c(T,F)]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[,1]\u00a0 \u00a0[,2]<br \/>\n[1,]\u00a0 \u00a0 \u00a010\u00a0 \u00a0 16<br \/>\n[2,]\u00a0 \u00a0 \u00a012\u00a0 \u00a018<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(1,2),-3]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[,1]\u00a0 [,2]<br \/>\n[1,]\u00a0 \u00a0 10\u00a0 \u00a013<br \/>\n[2,]\u00a0 \u00a0 11\u00a0 \u00a014<\/div>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; named_matrix[c(\"row2\",\"row3\"),c(1,2)]<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">col1\u00a0 col2<br \/>\nrow2\u00a0 \u00a0 4\u00a0 \u00a0 \u00a05<br \/>\nrow3\u00a0 \u00a0 7\u00a0 \u00a0 \u00a08<\/div>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-combination-of-vectors.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75298\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-combination-of-vectors.png\" alt=\"indexing using combination of R vectors\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can index the R matrix with a single vector. When using this technique, the result is a vector formed by stacking the columns of the matrix one after another.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(3,4,5,6,7)]<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-a-single-vector.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75300\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-indexing-using-a-single-vector.png\" alt=\"indexing using a single R vector\" width=\"1299\" height=\"736\" \/><\/a><\/p>\n<h3>How to modify a matrix in R?<\/h3>\n<p>We modify the R matrix by using the various indexing techniques along with the assignment operator.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[c(1), ] &lt;- c(1,2,3)\n&gt; mat2<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2[mat2%%2!=0] &lt;- 1 #elements not divisible by 2 are relaced with 1\n&gt; mat2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-modifying-elements.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75301\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-modifying-elements.png\" alt=\"modifying elements in R matrix\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can add a row or column by storing the new row\/column in a vector and using the <strong><code>rbind()<\/code><\/strong> or <strong><code>cbind()<\/code><\/strong> functions to combine them with the matrix.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2 &lt;- cbind(mat2,c(1,2,3))\n&gt; mat2<\/pre>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat2 &lt;- rbind(mat2,c(4,5,6,7))\n&gt; mat2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-modifying-using-cbind-and-rbind.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75309\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-modifying-using-cbind-and-rbind.png\" alt=\"modifying R matix using cbind and rbind\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>Transposing a matrix is very easy in <a href=\"https:\/\/www.r-project.org\/\">R<\/a>. We simply use the <strong><code>t()<\/code><\/strong> function.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; t(mat1)<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-modifying-t-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75310\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-modifying-t-1.png\" alt=\"matrices in r modifying t()\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h3>How to perform arithmetic operations in R matrix?<\/h3>\n<p>We can use the R operators to perform various arithmetic operations on matrices. The results of these operations are also matrices. We can add, subtract, multiply or divide matrices with constants.<\/p>\n<p><strong>Multiplication:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat1*5<\/pre>\n<p><strong>Addition:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat1+2<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-arithmetic-constant-multiply-and-add.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75302\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-arithmetic-constant-multiply-and-add.png\" alt=\"R matrix arithmetic constant multiply and add\" width=\"1299\" height=\"741\" \/><\/a><strong>Subtraction:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat1-1<\/pre>\n<p><strong>Division:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat1\/2<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-arithmetic-constant-subtract-and-divide.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75303\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-arithmetic-constant-subtract-and-divide.png\" alt=\"matrmatrix arithmetic constant subtract and divide - R vector\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p>We can also add, subtract, multiply or divide matrices with other matrices within the rules of matrix arithmetic.<\/p>\n<p><strong>Addition of matrices:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat_add &lt;- mat1+mat3\n&gt; mat_add<\/pre>\n<p><strong>Subtraction of matrices:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat_sub &lt;- mat1-mat3\n&gt; mat_sub<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-addition-and-subtraction.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75304\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-addition-and-subtraction.png\" alt=\"R matrix addition and subtraction\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<p><strong>Multiplication of matrices:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat_mul &lt;- mat1*mat3\n&gt; mat_mul<\/pre>\n<p><strong>Division of matrices:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt; mat_div &lt;- mat1\/mat3\n&gt; mat_div<\/pre>\n<p><strong>Output:<\/strong><br \/>\n<a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-multiplication-and-division.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-75305\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/01\/matrices-in-r-matrix-multiplication-and-division.png\" alt=\"R matrix multiplication and division\" width=\"1299\" height=\"741\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Matrices are homogeneous data structures in R. They can only store data of a single basic data type.<\/p>\n<p>Matrices are vectors with two-dimensions. If confused with vectors, get a complete understanding of<em><strong><a href=\"https:\/\/techvidvan.com\/tutorials\/r-vector\/\"> R vector<\/a><\/strong><\/em>.<\/p>\n<p>In this article, we learned about R matrices and their role in the R programming language.<\/p>\n<p>We learned how to create them and how to name their rows and columns. We also learned how to access their elements and how to modify them. Finally, we learned how basic matrix arithmetic works in R.<\/p>\n<p>We hope that this article clearly explained the basics of matrices in the R programming language to you.<\/p>\n<p>Keep Learning!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this TechVidvan tutorial, you will explore a matrix in R with examples. You will learn to create, modify, and access R matrix components. A matrix is a two-dimensional, homogeneous data structure in R.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":75360,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1020],"tags":[1269,1270,1271,1272,1273,1253,1274],"class_list":["post-75252","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r","tag-make-a-matrix-in-r","tag-matrix-function-in-r","tag-modify-r-matrix","tag-r-identity-matrix","tag-r-matrices","tag-r-matrix","tag-transpose-in-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>R Matrix - How to create, name and modify matrices in R? - TechVidvan<\/title>\n<meta name=\"description\" content=\"R matrix - Learn to create a matrix,name the rows and columns, access individual components, modify elements, &amp; perform arithmetic operations in R matrices.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/r-matrix\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Matrix - How to create, name and modify matrices in R? - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"R matrix - Learn to create a matrix,name the rows and columns, access individual components, modify elements, &amp; perform arithmetic operations in R matrices.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/r-matrix\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-08T09:30:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Matrix-in-R-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"R Matrix - How to create, name and modify matrices in R? - TechVidvan","description":"R matrix - Learn to create a matrix,name the rows and columns, access individual components, modify elements, & perform arithmetic operations in R matrices.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/","og_locale":"en_US","og_type":"article","og_title":"R Matrix - How to create, name and modify matrices in R? - TechVidvan","og_description":"R matrix - Learn to create a matrix,name the rows and columns, access individual components, modify elements, & perform arithmetic operations in R matrices.","og_url":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-01-08T09:30:09+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Matrix-in-R-1.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"R Matrix &#8211; How to create, name and modify matrices in R?","datePublished":"2020-01-08T09:30:09+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/"},"wordCount":953,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Matrix-in-R-1.jpg","keywords":["make a matrix in R","Matrix Function in R","Modify R Matrix","R identity matrix","R Matrices","R matrix","transpose in r"],"articleSection":["R Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/r-matrix\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/","url":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/","name":"R Matrix - How to create, name and modify matrices in R? - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Matrix-in-R-1.jpg","datePublished":"2020-01-08T09:30:09+00:00","description":"R matrix - Learn to create a matrix,name the rows and columns, access individual components, modify elements, & perform arithmetic operations in R matrices.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/r-matrix\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Matrix-in-R-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/01\/Matrix-in-R-1.jpg","width":802,"height":420,"caption":"R matrix"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/r-matrix\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"R Matrix &#8211; How to create, name and modify matrices in R?"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=75252"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/75252\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/75360"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=75252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=75252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=75252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}