<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>matrix Archives - Data Analytics</title>
	<atom:link href="https://dataanalytics.org.uk/tag/matrix/feed/" rel="self" type="application/rss+xml" />
	<link>https://dataanalytics.org.uk/tag/matrix/</link>
	<description>Understanding Data</description>
	<lastBuildDate>Thu, 01 Sep 2022 11:53:31 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.1</generator>

<image>
	<url>https://dataanalytics.org.uk/wp-content/uploads/2019/11/cropped-DA-graph-logo-2019-cyan-600-32x32.png</url>
	<title>matrix Archives - Data Analytics</title>
	<link>https://dataanalytics.org.uk/tag/matrix/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Matrix Math</title>
		<link>https://dataanalytics.org.uk/matrix-math-operations-using-r/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matrix-math-operations-using-r</link>
		
		<dc:creator><![CDATA[gmark]]></dc:creator>
		<pubDate>Thu, 01 Sep 2022 11:53:31 +0000</pubDate>
				<category><![CDATA[Introduction-to-R]]></category>
		<category><![CDATA[chol]]></category>
		<category><![CDATA[Cholensky]]></category>
		<category><![CDATA[cross product]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[determinant]]></category>
		<category><![CDATA[eigenvalue]]></category>
		<category><![CDATA[eigenvector]]></category>
		<category><![CDATA[inner product]]></category>
		<category><![CDATA[Kronecker]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[math functions]]></category>
		<category><![CDATA[matrices]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[outer]]></category>
		<category><![CDATA[outer-product]]></category>
		<category><![CDATA[qr]]></category>
		<category><![CDATA[QR decomposition]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[sad]]></category>
		<category><![CDATA[Singular Value Decomposition]]></category>
		<category><![CDATA[transpose]]></category>
		<guid isPermaLink="false">https://www.dataanalytics.org.uk/?p=39610</guid>

					<description><![CDATA[<p>Matrix Math Operations Using R Matrix math operations using R are easy to conduct using basic math operators such as + - * [&#8230;]</p>
<p>The post <a href="https://dataanalytics.org.uk/matrix-math-operations-using-r/">Matrix Math</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h1>Matrix Math Operations Using R</h1>
<p>Matrix math operations using R are easy to conduct using basic math operators such as <code>+</code> <code>-</code> <code>*</code> and <code>/</code>. Generally speaking the <code>matrix</code> objects you perform math on must be the same dimensions (that is conformable) as each other but there are exceptions.</p>
<p>There are two kinds of matrix math:</p>
<ul>
<li>Math using a <code>matrix</code> simply as a series of values.</li>
<li>Matrix math using array properties.</li>
</ul>
<p>In the first case you can use <code>R</code> to carry out regular mathematical operations on a <code>matrix</code> (that is a 2-dimensional array). In the second case you use <code>R</code> to conduct &#8220;proper&#8221; matrix math.</p>
<p>This article is not intended to be a detailed treatise on matrix math; think of it more as a rough guide to possibilities. If <code>matrix</code> math is what you need then this might help point you in the right direction.</p>
<p>Here are some of the topics covered:</p>
<ul>
<li><a href="#simple-math">Simple math</a>
<ul>
<li><a href="#identical-dim">Identical dimensions</a></li>
<li><a href="#single-num">Matrix and single numeric</a></li>
<li><a href="#multiple-num">Matrix and multiple numeric</a></li>
<li><a href="#other-ops">Other matrix operations</a></li>
<li><a href="#non-conform">Non-conformable matrices</a></li>
</ul>
</li>
<li><a href="#matrix-math">Matrix math</a>
<ul>
<li><a href="#inner">Inner product</a></li>
<li><a href="#outer">Outer product</a></li>
<li><a href="#kronecker">Kronecker product</a></li>
<li><a href="#diag">Diagonals</a></li>
<li><a href="#eigen">Eigenvalues and Eigenvectors</a></li>
<li><a href="#svd">Singular Value Decomposition</a></li>
<li><a href="#chol">Cholesky decomposition</a></li>
<li><a href="#qr">QR decomposition</a></li>
<li><a href="#det">Determinant</a></li>
<li><a href="#cross">Cross products</a></li>
</ul>
</li>
<li><a href="#misc-tools">Miscellaneous tools</a>
<ul>
<li><a href="#transpose">Transposition</a></li>
<li><a href="#triangles">Upper/lower triangles</a></li>
<li><a href="#row-index">Row/column index</a></li>
</ul>
</li>
</ul>
<h2 id="simple-math">Simple math</h2>
<p>Matrix math operations using R can be fairly simple. In the &#8220;simple&#8221; case you can use a <code>matrix</code> with basic math operators.</p>
<h3 id="identical-dim">Identical dimensions</h3>
<p>The &#8220;simplest&#8221; situation is where you have <code>matrix</code> objects that have the same dimensions.</p>
<pre><code class="language-r" lang="r">x &lt;- <span style="color: #0000ff;">matrix</span>(c(1,1, 3,0, 1,0), ncol = 3))
y &lt;- <span style="color: #0000ff;">matrix</span>(c(0,7, 0,5, 5,0), ncol = 3)
x ; y

     [,1] [,2] [,3]
[1,]    1    3    1
[2,]    1    0    0

     [,1] [,2] [,3]
[1,]    0    0    5
[2,]    7    5    0
</code></pre>
<p>The regular math operators with deal with the calculations:</p>
<pre><code class="language-r" lang="r">x + y
     [,1] [,2] [,3]
[1,]    1    3    6
[2,]    8    5    0

x * y
     [,1] [,2] [,3]
[1,]    0    0    5
[2,]    7    0    0

x / y
          [,1] [,2] [,3]
[1,]       Inf  Inf  0.2
[2,] 0.1428571    0  NaN

x - y
     [,1] [,2] [,3]
[1,]    1    3   -4
[2,]   -6   -5    0
</code></pre>
<p>In the preceding example 2 <code>matrix</code> objects were used but there is no reason why you cannot have more, as long as they all have the same dimensions.</p>
<h3 id="">Matrix and a single numeric</h3>
<p>If you have a <code>matrix</code> you can also use the basic operators with a single value:</p>
<pre><code class="language-r" lang="r">x * 4.5
     [,1] [,2] [,3]
[1,]  4.5 13.5  4.5
[2,]  4.5  0.0  0.0

y / 2
     [,1] [,2] [,3]
[1,]  0.0  0.0  2.5
[2,]  3.5  2.5  0.0
</code></pre>
<h3 id="multiple-num">Matrix and multiple numeric</h3>
<p>You can think of a <code>matrix</code> as a <code>vector</code> of values that happens to be split into rows and columns. This means that you can carry out math operations with a <code>matrix</code> and a <code>vector</code> of values, as long as the recursive process &#8220;fits&#8221; (i.e. is conformable).</p>
<p>In practical terms you can carry out a math operation between a <code>matrix</code> and a <code>vector</code> if the length of the <code>matrix</code> is divisible by the length of the <code>vector</code> exactly (i.e. producing an integer).</p>
<p>In the following example, the <code>matrix</code> has 6 elements and therefore any <code>vector</code> would have to contain 1, 2, or 3 elements. If your <code>vector</code> is too long it is truncated and you get a warning.</p>
<pre><code class="language-r" lang="r">x
     [,1] [,2] [,3]
[1,]    1    3    1
[2,]    1    0    0

<span style="color: #0000ff;">length</span>(x)
[1] 6

x * 2:4
     [,1] [,2] [,3]
[1,]    2   12    3
[2,]    3    0    0

<span style="color: #808000;"># Long vectors are truncated with a warning</span>
x + 1:4
     [,1] [,2] [,3]
[1,]    2    6    2
[2,]    3    4    2

Warning message:
In x + 1:4 :
  longer object length is not a multiple of shorter object length
</code></pre>
<p>It can help to see your <code>matrix</code> as a <code>vector</code>:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">c</span>(x)
[1] 1 1 3 0 1 0

<span style="color: #0000ff;">c</span>(x) * 2:4
[1]  2  3 12  0  3  0

<span style="color: #0000ff;">c</span>(x) + 1:4
[1] 2 3 6 4 2 2

Warning message:
In c(x) + 1:4 :
  longer object length is not a multiple of shorter object length
</code></pre>
<p>Note that <code>c(x)</code> was used in the example but <code>as.numeric(x)</code> would also work.</p>
<h3 id="other-ops">Other matrix math operations</h3>
<p>Many math functions that can operate on a <code>vector</code> will be able to work with a <code>matrix</code>. For example:</p>
<pre><code class="language-r" lang="r">xx
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

<span style="color: #0000ff;">log</span>(xx)
         [,1]      [,2]     [,3]
[1,] 0.000000 0.6931472 1.098612
[2,] 1.386294 1.6094379 1.791759

<span style="color: #0000ff;">cos</span>(xx)
           [,1]       [,2]       [,3]
[1,]  0.5403023 -0.4161468 -0.9899925
[2,] -0.6536436  0.2836622  0.9601703

<span style="color: #0000ff;">sqrt</span>(xx)
     [,1]     [,2]     [,3]
[1,]    1 1.414214 1.732051
[2,]    2 2.236068 2.449490
</code></pre>
<hr />
<h3 id="non-conform">Non-conformable matrices</h3>
<p>When you have two <code>matrix</code> objects of different dimensions you get an error. This example shows a single dimension <code>matrix</code>:</p>
<pre><code class="language-r" lang="r">yy &lt;- <span style="color: #0000ff;">matrix</span>(c(40,100), ncol = 1)
yy
     [,1]
[1,]   10
[2,]  100

x * yy
Error in x * yy : non-conformable arrays
</code></pre>
<p>In this case the <code>matrix</code> can be converted to a <code>vector</code> resulting in 2 elements. This is divisible by the number of elements in the first <code>matrix</code> and permits matrix math:</p>
<pre><code class="language-r" lang="r">x * <span style="color: #0000ff;">c</span>(yy)
     [,1] [,2] [,3]
[1,]   10   30   10
[2,]  100    0    0

x + <span style="color: #0000ff;">c</span>(yy)
     [,1] [,2] [,3]
[1,]   11   13   11
[2,]  101  100  100
</code></pre>
<p>There are other solutions for this case:</p>
<pre><code class="language-r" lang="r">xx * <span style="color: #0000ff;">c</span>(yy)
<span style="color: #0000ff;">sweep</span>(xx, 1, yy, `*`)
<span style="color: #0000ff;">apply</span>(xx, 2, <span style="color: #0000ff;">function</span>(x) x * yy)
xx * yy[,1]
xx * yy[<span style="color: #0000ff;">row</span>(xx),]
</code></pre>
<p>These solutions work for any of the math operators <code>+</code> <code>-</code> <code>*</code> and <code>/</code>.</p>
<hr />
<h2 id="matrix-math">Matrix Mathematics</h2>
<p>Matrix math operations using R involve more than just simple math. In mathematics <code>matrix</code> math is a substantial topic. Using <code>R</code> you can carry out various matrix math computations. These include:</p>
<ul>
<li>Inner product <code>%*%</code></li>
<li>Outer product <code>outer()</code> and <code>%o%</code></li>
<li>Kronecker product <code>kronecker()</code> and <code>%x%</code></li>
<li>Matrix diagonals <code>diag()</code></li>
<li>Eigenvalues <code>eigen()</code></li>
<li>Singular Value Decomposition <code>svd()</code></li>
<li>Cholesky (Choleski) decomposition <code>chol()</code></li>
<li>QR decomposition <code>qr()</code></li>
<li>Determinant <code>det()</code></li>
<li>Cross-product <code>crossprod()</code> and <code>tcrossprod()</code></li>
<li>Transposition <code>t()</code></li>
<li>Upper/lower triangles <code>upper.tri()</code> and <code>lower.tri()</code></li>
<li>Row/column index <code>row()</code> and <code>col()</code></li>
</ul>
<h3 id="inner">Inner product</h3>
<p>Standard matrix multiplication (or inner product) is a way to multiply two <code>matrix</code> objects. You use the <code>%*%</code> operator in <code>R</code>to execute the math.</p>
<pre><code class="language-r" lang="r">X %*% Y
</code></pre>
<p>In general if the number of columns of the first <code>matrix</code> equals the number of rows of the second <code>matrix</code> then you should be able to use the matrix-math operator <code>%*%</code> to multiply:</p>
<pre><code class="language-r" lang="r">X &lt;- <span style="color: #0000ff;">matrix</span>(c(2,7,4,8,5,9), ncol = 3)
Y &lt;- <span style="color: #0000ff;">matrix</span>(c(1,3,4,6,1,2), ncol = 2)

X ; Y
     [,1] [,2] [,3]
[1,]    2    4    5
[2,]    7    8    9

     [,1] [,2]
[1,]    1    6
[2,]    3    1
[3,]    4    2

X %*% Y
     [,1] [,2]
[1,]   34   26
[2,]   67   68
</code></pre>
<h3 id="outer">Outer product</h3>
<p>The outer product is a <code>matrix</code> operation that takes a <code>matrix</code> (or <code>vector</code> or <code>array</code>) and applies a function to it, with a second <code>matrix</code> (or a <code>vector</code> or <code>array</code>) as an argument. There are two options in <code>R</code>:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">outer</span>(X, Y, FUN = "*", ...)
X %o% Y
</code></pre>
<p>The <code>outer()</code> command allows any maths function, with the default being <code>*</code>. Essentially <code>X</code> and <code>Y</code> are the main arguments (you can pass additional parameters too). The <code>%o%</code> operator (the letter o) is a convenience function for multiply (i.e. <code>FUN = "*"</code>) so you end up specifying <code>X</code> multiplied by <code>Y</code>.</p>
<p>Use two vectors and the result is a <code>matrix</code> with dimensions determined by the length of the vectors:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">outer</span>(1:3, 1:3)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9
</code></pre>
<p>You can use different function, and use <code>Y</code> as a parameter passed to <code>FUN</code>, in the following example <code>Y</code> specifies the <code>base</code> for the <code>log</code>:</p>
<pre><code class="language-r" lang="r">m
     [,1] [,2]
[1,]    5    1
[2,]    1    3

<span style="color: #0000ff;">outer</span>(m, Y = 2, FUN = log)
, , 1

         [,1]     [,2]
[1,] 2.321928 0.000000
[2,] 0.000000 1.584963
</code></pre>
<p>Essentially what happens is that the second <code>matrix</code> is converted to a <code>vector</code> and this is &#8220;applied&#8221; to the first <code>matrix</code> using the function specified. Each element of the second <code>matrix</code> is applied in turn, and the result is an <code>array</code> with multiple dimensions. In the following example the first <code>matrix</code> is 2&#215;2 and so is the second, so the result has dimensions of: <code>2, 2, 2, 2</code></p>
<pre><code class="language-r" lang="r">A
     [,1] [,2]
[1,]    4   -1
[2,]    1    3

m
     [,1] [,2]
[1,]    5    1
[2,]    1    3

<span style="color: #0000ff;">outer</span>(A, m, FUN = "+")
, , 1, 1

     [,1] [,2]
[1,]    9    4
[2,]    6    8

, , 2, 1

     [,1] [,2]
[1,]    5    0
[2,]    2    4

, , 1, 2

     [,1] [,2]
[1,]    5    0
[2,]    2    4

, , 2, 2

     [,1] [,2]
[1,]    7    2
[2,]    4    6

<span style="color: #808000;"># Get identical result using a vector</span>
<span style="color: #0000ff;">outer</span>(A, c(5,1,1,3), FUN = "+")
</code></pre>
<h3 id="kronecker">Kronecker product</h3>
<p>The Kronecker matrix product is similar to the outer product but the result is a single <code>array</code> with dimensions equal to <code>dim(X)</code> x <code>dim(Y)</code>.</p>
<pre><code><span style="color: #0000ff;">kronecker</span>(X, Y, FUN = "*", ...)
X %x% Y
</code></pre>
<p>The <code>kronecker()</code> command allows any function to be used, whist the alias <code>%x%</code> is for multiply (i.e. <code>FUN = "*"</code>).</p>
<pre><code class="language-r" lang="r">m
     [,1] [,2]
[1,]    5    1
[2,]    1    3

m %x% 2
     [,1] [,2]
[1,]   10    2
[2,]    2    6
</code></pre>
<p>When your second array has &gt;1 dimension, the result has dimensions equal to <code>dim(X)</code> x <code>dim(Y)</code>.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">kronecker</span>(m, 2:3, FUN = "+")
     [,1] [,2]
[1,]    7    3
[2,]    8    4
[3,]    3    5
[4,]    4    6

xx
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

yy
     [,1]
[1,]   10
[2,]  100

<span style="color: #0000ff;">kronecker</span>(xx, yy)
     [,1] [,2] [,3]
[1,]   10   20   30
[2,]  100  200  300
[3,]   40   50   60
[4,]  400  500  600
</code></pre>
<h3 id="diag">Diagonals</h3>
<p>A <code>matrix</code> diagonal is more or less what it sounds like! Matrix diagonals are used in various branches of matrix math. In <code>R</code>you can use the <code>diag()</code> function to extract or create <code>matrix</code> diagonals.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">diag</span>(x = 1, nrow, ncol, names = TRUE)
<span style="color: #0000ff;">diag</span>(x) &lt;- value
</code></pre>
<p>There are 4 main uses for <code>diag()</code>:</p>
<ol>
<li>To extract the diagonal from <code>x</code> (a <code>matrix</code>).</li>
<li>To make an identity <code>matrix</code> by specifying <code>nrow</code>.</li>
<li>To make an identity <code>matrix</code> by specifying <code>x</code> as a scalar.</li>
<li>To make a <code>matrix</code> with specified diagonals.</li>
</ol>
<p>You can also assign diagonal values to an existing <code>matrix</code>.</p>
<h4>Extract diagonals</h4>
<p>To extract the diagonals from a <code>matrix</code> you simply specify the <code>matrix</code>:</p>
<pre><code class="language-r" lang="r">C
     [,1] [,2] [,3]
[1,]    3    1   -5
[2,]    2    0    4
[3,]    1    6    3

<span style="color: #0000ff;">diag</span>(C)
[1] 3 0 3
</code></pre>
<p>If the <code>matrix</code> is not square you get the diagonals starting from the top-left:</p>
<pre><code class="language-r" lang="r">E
     [,1] [,2] [,3]
[1,]   -3    2    0
[2,]    0    5   -1

<span style="color: #0000ff;">diag</span>(E)
[1] -3  5
</code></pre>
<h4>Identity matrix from nrow</h4>
<p>If you omit <code>x</code> and specify <code>nrow</code> you will return an identity matrix, that is one where the diagonals are <code>1</code> and the other elements are <code>0</code>. You may also give <code>ncol</code> to create a non-square <code>matrix</code> (note that <code>ncol</code> may be larger or smaller than <code>nrow</code>):</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">diag</span>(nrow = 4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1

<span style="color: #0000ff;">diag</span>(nrow = 4, ncol = 3)
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1
[4,]    0    0    0

<span style="color: #0000ff;">diag</span>(nrow = 4, ncol = 6)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    0    0    0    0    0
[2,]    0    1    0    0    0    0
[3,]    0    0    1    0    0    0
[4,]    0    0    0    1    0    0
</code></pre>
<h4>Identity matrix from scalar</h4>
<p>If you specify <code>x</code> as a scalar (single value) you return an identity <code>matrix</code> with diagonals of <code>1</code> and with dimensions (nrow, ncol) set to the of the scalar:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">diag</span>(4)
     [,1] [,2] [,3] [,4]
[1,]    1    0    0    0
[2,]    0    1    0    0
[3,]    0    0    1    0
[4,]    0    0    0    1
</code></pre>
<h4>Matrix from vector</h4>
<p>If you specify <code>x</code> as a <code>vector</code> you will return a <code>matrix</code> where the diagonals are the values in <code>x</code> and the other elements are <code>0</code>:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">diag</span>(x = c(3,4,2,6))
     [,1] [,2] [,3] [,4]
[1,]    3    0    0    0
[2,]    0    4    0    0
[3,]    0    0    2    0
[4,]    0    0    0    6
</code></pre>
<h4>Set diagonals for existing matrix</h4>
<p>You can set the diagonals for an existing <code>matrix</code> by assigning values:</p>
<pre><code class="language-r" lang="r">mm
     [,1] [,2]
[1,]    8    1
[2,]    1    9

<span style="color: #0000ff;">diag</span>(mm) &lt;- 100
mm
     [,1] [,2]
[1,]  100    1
[2,]    1  100

diag(mm) &lt;- c(6,3)
mm
     [,1] [,2]
[1,]    6    1
[2,]    1    3

CC
     [,1] [,2] [,3]
[1,]    3    1   -5
[2,]    2    0    4
[3,]    1    6    3

<span style="color: #0000ff;">diag</span>(CC) &lt;- c(99, 999)
Error in `diag&lt;-`(`*tmp*`, value = c(99, 999)) :
  replacement diagonal has wrong length
</code></pre>
<p><strong>Note</strong>:</p>
<ul>
<li>If you specify a single value it is used for all diagonals.</li>
<li>If you specify the &#8220;correct&#8221; number of elements you replace the existing values.</li>
<li>If you specify too few or too many elements you get an error.</li>
</ul>
<h3 id="eigen">Eigenvalues and eigenvectors</h3>
<p>Spectral decomposition of a <code>matrix</code> produces eigenvalues and eigenvectors. These values are used often in linear algebra and various branches of mathematics.</p>
<p>The <code>R</code> function <code>eigen()</code> can compute the eigenvalues and eigenvectors of a (square) <code>matrix</code>.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">eigen</span>(x, symmetric, only.values = FALSE)
</code></pre>
<p>The <code>eigen()</code> function only works on square <code>matrix</code> objects.</p>
<p>The <code>symmetric</code> parameter allows you to specify <code>TRUE</code> to force the calculations to use only the lower triangle, when you have symmetrical <code>matrix</code> objects. If you leave this to the default then the function checks to see if the values are symmetric.</p>
<p>The <code>only.values</code> parameter can be set to <code>TRUE</code> to return only the eigenvalues.</p>
<pre><code class="language-r" lang="r">m
     [,1] [,2]
[1,]    5    1
[2,]    1    3

<span style="color: #0000ff;">eigen</span>(m)
eigen() decomposition
$values
[1] 5.414214 2.585786

$vectors
           [,1]       [,2]
[1,] -0.9238795  0.3826834
[2,] -0.3826834 -0.9238795
</code></pre>
<p>If you have negative values you&#8217;ll return <code>complex</code> values:</p>
<pre><code class="language-r" lang="r">M
     [,1] [,2]
[1,]    5   -2
[2,]    2    4

<span style="color: #0000ff;">eigen</span>(M)
eigen() decomposition
$values
[1] 4.5+1.936492i 4.5-1.936492i

$vectors
                     [,1]                 [,2]
[1,] 0.7071068+0.0000000i 0.7071068+0.0000000i
[2,] 0.1767767-0.6846532i 0.1767767+0.6846532i
</code></pre>
<h3 id="svd">Singular Value Decomposition</h3>
<p>Singular value decomposition of a <code>matrix</code> is important in many statistical routines. SVD is a kind of factorisation of a <code>matrix</code> and is carried out in <code>R</code> with the <code>svd()</code> function:</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">svd</span>(x, nu = min(n, p), nv = min(n, p))
</code></pre>
<p>In the <code>svd()</code> function:</p>
<ul>
<li><code>x</code> is a <code>matrix</code></li>
<li><code>nu</code> is the number of left singular vectors to compute</li>
<li><code>nv</code> is the number of right singular vectors to compute</li>
</ul>
<p>The <code>svd()</code> function returns a result with several components:</p>
<ul>
<li><code>$d</code> a <code>vector</code> of singular values in descending order.</li>
<li><code>$u</code> a <code>matrix</code> of left singular vectors.</li>
<li><code>$v</code> a <code>matrix</code> of right singular vectors.</li>
</ul>
<pre><code class="language-r" lang="r">C
     [,1] [,2] [,3]
[1,]    3    1   -5
[2,]    2    0    4
[3,]    1    6    3

<span style="color: #0000ff;">svd</span>(C)
$d
[1] 7.620874 5.810848 3.025941

$u
           [,1]       [,2]       [,3]
[1,] -0.4710376 -0.7804341 -0.4111522
[2,]  0.4517527  0.1869132 -0.8723434
[3,]  0.7576563 -0.5966457  0.2645201

$v
           [,1]       [,2]       [,3]
[1,] 0.03254861 -0.4412646 -0.8967866
[2,] 0.53470246 -0.7503738  0.3886290
[3,] 0.84441333  0.4921633 -0.2115217
</code></pre>
<p>You can use the <code>nu</code> and <code>nv</code> arguments to reduce (to <code>0</code> if you like) the number of left and/or right singular vectors.</p>
<pre><code class="language-r" lang="r">VADeaths
      Rural Male Rural Female Urban Male Urban Female
50-54       11.7          8.7       15.4          8.4
55-59       18.1         11.7       24.3         13.6
60-64       26.9         20.3       37.0         19.3
65-69       41.0         30.9       54.6         35.1
70-74       66.0         54.3       71.1         50.0

<span style="color: #0000ff;">svd</span>(VADeaths, nu = 2, nv = 2)
$d
[1] 161.967972  10.037542   3.212076   1.194284

$u
           [,1]       [,2]
[1,] -0.1404491 -0.1476512
[2,] -0.2161352 -0.3623160
[3,] -0.3297674 -0.4373676
[4,] -0.5099065 -0.4819224
[5,] -0.7515374  0.6506817

$v
           [,1]       [,2]
[1,] -0.5243856  0.3123820
[2,] -0.4137210  0.6015805
[3,] -0.6229108 -0.7282861
[4,] -0.4072306  0.1005871
</code></pre>
<h3 id="chol">Cholesky decomposition</h3>
<p>Compute the Choleski (Cholesky) factorization of a real symmetric positive-definite square <code>matrix</code> using the <code>chol()</code>function. It is a kind of optimisation that can be used in various mathematical problems.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">chol</span>(x, pivot = FALSE, tol = -1, ...)
</code></pre>
<p>In <code>chol()</code>:</p>
<ul>
<li><code>x</code> is a <code>matrix</code>, which should be positive and symmetric, (i.e. square).</li>
<li><code>pivot</code> can be set to <code>TRUE</code> in some cases where the <code>matrix</code> is semi-positive definite.</li>
<li><code>tol</code> is used to set a tolerance when <code>pivot = TRUE</code>.</li>
</ul>
<p>How to tell if a matrix is positive definite, or positive semi-definite?</p>
<ul>
<li>A <code>matrix</code> is <strong>symmetric</strong> if <code>nrow</code> = <code>ncol</code>, in other words, square. In addition the transposed <code>matrix</code> must be the same as the original.</li>
<li>If all the eigenvalues are positive (&gt;0) the <code>matrix</code> is <strong>positive definite</strong>.</li>
<li>If any eigenvalues are <code>0</code> and the others positive then the <code>matrix</code> is <strong>positive semi-definite</strong>.</li>
</ul>
<p>You can check the eigenvalues with <code>eigen(x)</code>.</p>
<pre><code class="language-r" lang="r">m
     [,1] [,2]
[1,]    5    1
[2,]    1    3

<span style="color: #808000;"># Check matrix is positive definite</span>
<span style="color: #0000ff;">eigen</span>(m)$val
[1] 5.414214 2.585786

<span style="color: #808000;"># Check that matrix is symmetric</span>
<span style="color: #0000ff;">identical</span>(m, <span style="color: #0000ff;">t</span>(m))
[1] TRUE

<span style="color: #0000ff;">chol</span>(m)
         [,1]      [,2]
[1,] 2.236068 0.4472136
[2,] 0.000000 1.6733201

A
     [,1] [,2]
[1,]    4   -1
[2,]    1    3

<span style="color: #808000;"># Check matrix (it is complex but is +ve definite)</span>
<span style="color: #0000ff;">eigen</span>(A)$val
[1] 3.5+0.866025i 3.5-0.866025i

# Check if matrix is symmetric
<span style="color: #0000ff;">identical</span>(A, <span style="color: #0000ff;">t</span>(A))
[1] FALSE

<span style="color: #808000;"># chol() will return a result as it does not check symmetry!
</span></code></pre>
<p>If you have an eigenvalue of <code>0</code> (with the others <code>&gt;0</code>) then your <code>matrix</code> is positive semi-definite. You can perform Cholensky decomposition with <code>chol()</code> but need to use <code>pivot = TRUE</code> as an argument.</p>
<pre><code class="language-r" lang="r">im &lt;- <span style="color: #0000ff;">matrix</span>(c(1,-1, -1,1), ncol = 2)

im
     [,1] [,2]
[1,]    1   -1
[2,]   -1    1

<span style="color: #808000;"># matrix is +ve semi-definite</span>
eigen(im)$val
[1] 2 0

<span style="color: #808000;"># Check symmetric</span>
<span style="color: #0000ff;">identical</span>(im, <span style="color: #0000ff;">t</span>(im))
[1] TRUE

<span style="color: #0000ff;">chol</span>(im)
Error in chol.default(im) :
  the leading minor of order 2 is not positive definite

<span style="color: #808000;"># Need to use pivot = TRUE</span>
<span style="color: #0000ff;">chol</span>(im, pivot = TRUE)
     [,1] [,2]
[1,]    1   -1
[2,]    0    0
attr(,"pivot")
[1] 1 2
attr(,"rank")
[1] 1
Warning message:
In chol.default(im, pivot = TRUE) :
  the matrix is either rank-deficient or indefinite
</code></pre>
<p>Note that you still get a warning!</p>
<p>The eigenvalues need to be <code>&gt;0</code> but there is a tolerance:</p>
<pre><code class="language-r" lang="r">mat &lt;- <span style="color: #0000ff;">matrix</span>(c(4,12,-16, 12,37,-43, -16,-43,98), ncol = 3)
mat
     [,1] [,2] [,3]
[1,]    4   12  -16
[2,]   12   37  -43
[3,]  -16  -43   98

<span style="color: #808000;"># Eigenvalues all &gt;0 (just)</span>
<span style="color: #0000ff;">eigen</span>(mat)$val
[1] 123.47723179  15.50396323   0.01880498

<span style="color: #808000;"># Check symmetry</span>
<span style="color: #0000ff;">identical</span>(mat, <span style="color: #0000ff;">t</span>(mat))
[1] TRUE

<span style="color: #0000ff;">chol</span>(mat)
     [,1] [,2] [,3]
[1,]    2    6   -8
[2,]    0    1    5
[3,]    0    0    3

a &lt;- <span style="color: #0000ff;">matrix</span>(c(2,3,1,11, 3,9,3,30, 1,3,1,10, 11,30,10,101))
a
     [,1] [,2] [,3] [,4]
[1,]    2    3    1   11
[2,]    3    9    3   30
[3,]    1    3    1   10
[4,]   11   30   10  101

<span style="color: #808000;"># Very small eigenvalue registers as effectively 0</span>
eigen(a)$val
[1] 1.120990e+02 9.009892e-01 1.681988e-14 1.831868e-15

<span style="color: #808000;"># Check symmetry</span>
<span style="color: #0000ff;">identical</span>(a, <span style="color: #0000ff;">t</span>(a))
[1] TRUE

<span style="color: #0000ff;">chol</span>(a)
Error in chol.default(a) :
  the leading minor of order 3 is not positive definite
</code></pre>
<p>If you try to use <code>pivot = TRUE</code> on a <code>matrix</code> that is not positive (i.e. non-negative definite), you&#8217;ll get a result with a warning. However, the result will be meaningless!</p>
<pre><code class="language-r" lang="r"><span style="color: #808000;"># Negative definite i.e. not non-negative definite</span>
b &lt;- <span style="color: #0000ff;">matrix</span>(c(5,-5,-5,3), 2, 2)
b
     [,1] [,2]
[1,]    5   -5
[2,]   -5    3

<span style="color: #808000;"># check symmetry -- okay</span>
<span style="color: #0000ff;">identical</span>(b, <span style="color: #0000ff;">t</span>(b))
[1] TRUE

<span style="color: #808000;"># negative eigenvalues, so no +ve</span>
<span style="color: #0000ff;">eigen</span>(b)$val
[1]  9.09902 -1.09902

<span style="color: #808000;"># pivot = TRUE runs but result is meaningless!
#.. not shown
</span></code></pre>
<h3 id="qr">QR Decomposition</h3>
<p>QR decomposition is used in many statistical methods, especially in regression. The <code>qr()</code> function carries out the calculations.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">qr</span>(x, tol = 1e-07 , LAPACK = FALSE, ...)
</code></pre>
<p>In <code>qr()</code>:</p>
<ul>
<li><code>x</code> is a <code>matrix</code> to be decomposed.</li>
<li><code>tol</code> is a tolerance used for detecting linear dependencies (you probably won&#8217;t need to tinker with this).</li>
<li><code>LAPACK</code> is which QR algorithm to use, the default uses <code>LINPACK</code>.</li>
</ul>
<p>The result of <code>qr()</code> is an object with various components:</p>
<pre><code class="language-r" lang="r">C
     [,1] [,2] [,3]
[1,]    3    1   -5
[2,]    2    0    4
[3,]    1    6    3

<span style="color: #0000ff;">qr</span>(C)
$qr
           [,1]      [,2]     [,3]
[1,] -3.7416574 -2.405351 1.069045
[2,]  0.5345225  5.586975 2.787095
[3,]  0.2672612 -0.983516 6.410089

$rank
[1] 3

$qraux
[1] 1.801784 1.180821 6.410089

$pivot
[1] 1 2 3

attr(,"class")
[1] "qr"
</code></pre>
<p>QR decomposition is a potentially large topic, which there is not space for here! In addition to the <code>qr()</code> function there are various <em>helper</em> functions. See the help entry in <code>qr()</code> for more information about those.</p>
<h3 id="det">Determinant</h3>
<p>You can calculate the of a square <code>matrix</code> using the <code>det()</code> function. A square <code>matrix</code> is one where <code>nrow</code> = <code>ncol</code> but it doesn&#8217;t have to be symmetrical (where <code>x = t(x)</code>).</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">det</span>(x)
<span style="color: #0000ff;">determinant</span>(x, logarithm = TRUE)
</code></pre>
<p>The <code>det()</code> function returns the determinant. The <code>determinant()</code> function returns the modulus of the determinant and the sign. The default for <code>determinant()</code> is to return the <code>log</code> of the determinant.</p>
<pre><code class="language-r" lang="r">A
     [,1] [,2]
[1,]    4   -1
[2,]    1    3

<span style="color: #808000;"># Matrix is square but no matter that it is not symmetric</span>
<span style="color: #0000ff;">identical</span>(A, <span style="color: #0000ff;">t</span>(A))
[1] FALSE

<span style="color: #0000ff;">det</span>(A)
[1] 13

<span style="color: #0000ff;">determinant</span>(A)
$modulus
[1] 2.564949
attr(,"logarithm")
[1] TRUE

$sign
[1] 1

attr(,"class")
[1] "det"

<span style="color: #0000ff;">determinant</span>(A, logarithm = FALSE)
$modulus
[1] 13
attr(,"logarithm")
[1] FALSE

$sign
[1] 1

attr(,"class")
[1] "det"
</code></pre>
<p>Note that the <code>logarithm</code> argument <strong>cannot</strong> be abbreviated.</p>
<h3 id="cross">Cross-products</h3>
<p>Cross products are where you multiply two <code>matrix</code> objects (inner product), but one is transposed. The <code>R</code> functions <code>crossprod()</code> and <code>tcrossprod()</code> can carry out the calculations.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">crossprod</span>(x, y = NULL)
<span style="color: #0000ff;">tcrossprod</span>(x, y = NULL)
</code></pre>
<p>You specify at least one <code>matrix</code> object <code>x</code>. Object <code>y</code> is optional, if not specified <code>x</code> is used. These functions are essentially equivalent to:</p>
<ul>
<li><code>t(x) %*% y</code> for <code>crossprod()</code></li>
<li><code>x %*% t(y)</code> for <code>tcrossprod()</code></li>
</ul>
<pre><code class="language-r" lang="r">b
     [,1] [,2]
[1,]    5   -5
[2,]   -5    3

<span style="color: #0000ff;">crossprod</span>(b)
     [,1] [,2]
[1,]   50  -40
[2,]  -40   34

B
     [,1] [,2]
[1,]    1    3
[2,]   -2   -4
[3,]    2    0

D
     [,1]
[1,]    3
[2,]    2
[3,]   -2

<span style="color: #0000ff;">crossprod</span>(B, D)
     [,1]
[1,]   -5
[2,]    1
</code></pre>
<p>You get an error if the dimensions are &#8220;wrong&#8221;:</p>
<pre><code class="language-r" lang="r">A
     [,1] [,2]
[1,]    4   -1
[2,]    1    3

D
     [,1]
[1,]    3
[2,]    2
[3,]   -2

<span style="color: #0000ff;">crossprod</span>(A, D)
Error in crossprod(A, D) : non-conformable arguments
</code></pre>
<p>If you specify a <code>vector</code> it will be &#8220;converted&#8221; to a one-column or one-row <code>matrix</code>:</p>
<pre><code class="language-r" lang="r">tcrossprod(1:4)
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    2    4    6    8
[3,]    3    6    9   12
[4,]    4    8   12   16
</code></pre>
<h2 id="misc-tools">Miscellaneous tools</h2>
<p>There are various other tools that you can use with <code>matrix</code> objects:</p>
<h3 id="transpose">Transposition</h3>
<p>Perhaps the simplest <code>matrix</code> operation is to transpose, that is switch the rows and columns. You can carry out <code>matrix</code>transposition using the <code>t()</code> function.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">t</span>(x)
</code></pre>
<p>Example: transpose matrix objects</p>
<pre><code class="language-r" lang="r">x
     [,1] [,2] [,3]
[1,]    1    3    1
[2,]    1    0    0

<span style="color: #0000ff;">t</span>(x)
     [,1] [,2]
[1,]    1    1
[2,]    3    0
[3,]    1    0

D
     [,1]
[1,]    3
[2,]    2
[3,]   -2

<span style="color: #0000ff;">t</span>(D)
     [,1] [,2] [,3]
[1,]    3    2   -2
</code></pre>
<h3 id="triangles">Upper and Lower triangles</h3>
<p>You can &#8220;see&#8221; the upper or lower triangles of a <code>matrix</code> (or any 2-D object) using <code>upper.tri()</code> and <code>lower.tri()</code>functions. These give you a <code>logical</code> <code>matrix</code> the same size as the original, i.e. <code>TRUE</code> or <code>FALSE</code> values.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">upper.tri</span>(x, diag = FALSE)
<span style="color: #0000ff;">lower.tri</span>(x, diag = FALSE)
</code></pre>
<p>You can optionally set <code>diag = TRUE</code> to return the diagonals as <code>TRUE</code>.</p>
<pre><code class="language-r" lang="r">A
     [,1] [,2]
[1,]    4   -1
[2,]    1    3

<span style="color: #0000ff;">upper.tri</span>(A)
      [,1]  [,2]
[1,] FALSE  TRUE
[2,] FALSE FALSE

B
     [,1] [,2]
[1,]    1    3
[2,]   -2   -4
[3,]    2    0

<span style="color: #0000ff;">lower.tri</span>(B, diag = TRUE)
     [,1]  [,2]
[1,] TRUE FALSE
[2,] TRUE  TRUE
[3,] TRUE  TRUE
</code></pre>
<h3 id="row-index">Row and Column index</h3>
<p>You can produce a <code>matrix</code> of <code>integer</code> values based on their position in a <code>matrix</code> object (or similar 2-D object). The <code>row()</code> and <code>col()</code> functions operate over rows and columns respectively.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">row</span>(x, as.factor = FALSE)
<span style="color: #0000ff;">.row</span>(dim)

<span style="color: #0000ff;">col</span>(x, as.factor = FALSE)
<span style="color: #0000ff;">.col</span>(dim)
</code></pre>
<p>Essentially you provide a <em>template</em> object and the functions return a new object with the appropriate values.</p>
<pre><code class="language-r" lang="r">C
     [,1] [,2] [,3]
[1,]    3    1   -5
[2,]    2    0    4
[3,]    1    6    3

<span style="color: #0000ff;">row</span>(C)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3
</code></pre>
<p>If you specify the argument <code>as.factor</code> you can get the result stored as a <code>factor</code> rather than a regular <code>numeric</code>.</p>
<pre><code class="language-r" lang="r">E
     [,1] [,2] [,3]
[1,]   -3    2    0
[2,]    0    5   -1

<span style="color: #0000ff;">col</span>(E, as.factor = TRUE)
     [,1] [,2] [,3]
[1,] 1    2    3
[2,] 1    2    3
Levels: 1 2 3
</code></pre>
<p>There are alternative versions <code>.row()</code> and <code>.col()</code>, which take a <code>vector</code> argument, giving the dimensions of the <code>matrix</code>for which the indices are required.</p>
<pre><code class="language-r" lang="r"><span style="color: #0000ff;">.row</span>(dim = c(3, 5))
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    2    2    2    2    2
[3,]    3    3    3    3    3
</code></pre>
<hr />
<h2>Summary</h2>
<p>The topic of <code>matrix</code> math is extensive! This article is not intended to be an exhaustive thesis on the subject but more of an introduction to some of the <code>R</code> tools that you can use to carry out <code>matrix</code> math, and other operations.</p>
<hr />
<p>This article is partly in support of my book <em>An Introduction to R</em> see the <a href="https://www.dataanalytics.org.uk/publications">publications page</a> for more information.</p>
<p>An Introduction to R will be published by <a href="https://pelagicpublishing.com/">Pelagic Publishing</a>. <a href="https://pelagicpublishing.com/pages/search-results-page?q=Mark%20Gardener">See all my books at Pelagic Publishing</a>.</p>
<p>For more articles visit the <a href="https://www.dataanalytics.org.uk/category/tips-and-tricks/">Tips and Tricks page</a> and look for the various categories or use the search box.</p>
<p>See also the <a>Knowledge Base</a> where there are other topics related to R and data science.</p>
<p>Look at our other books on the <a>Publications Page</a>.</p>
<p>Visit our other site at <a href="https://www.gardenersown.co.uk/">GardenersOwn</a> for a more ecological matters.</p>
<p>The post <a href="https://dataanalytics.org.uk/matrix-math-operations-using-r/">Matrix Math</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Naming rows and columns of a matrix in R</title>
		<link>https://dataanalytics.org.uk/naming-rows-and-columns-of-a-matrix-in-r/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=naming-rows-and-columns-of-a-matrix-in-r</link>
		
		<dc:creator><![CDATA[aJfsfjlser3f]]></dc:creator>
		<pubDate>Tue, 11 Jun 2019 17:13:45 +0000</pubDate>
				<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[columns]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[rows]]></category>
		<guid isPermaLink="false">https://www.dataanalytics.org.uk/?p=3983</guid>

					<description><![CDATA[<p>You make a matrix using matrix()rbind() or cbind() commands. The names of the rows and columns can be set after the matrix is [&#8230;]</p>
<p>The post <a href="https://dataanalytics.org.uk/naming-rows-and-columns-of-a-matrix-in-r/">Naming rows and columns of a matrix in R</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>You make a matrix using matrix()rbind() or cbind() commands. The names of the rows and columns can be set after the matrix is produced in various ways:</p>
<ul>
<li>rownames() – sets the row names</li>
<li>colnames() – sets the column names</li>
<li>dimnames() – sets both row and column names in one command</li>
</ul>
<p>The rownames() and colnames() commands set the row and column names respectively:</p>
<pre>&gt; m1 &lt;- matrix(1:12, nrow = 3)
&gt; m1
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12

&gt; rownames(m1)  &lt;- letters[26:24]
&gt; m1
  [,1] [,2] [,3] [,4]
z    1    4    7   10
y    2    5    8   11
x    3    6    9   12

&gt; colnames(m1)  &lt;- LETTERS[26:23]
&gt; m1
  Z Y X  W
z 1 4 7 10
y 2 5 8 11
x 3 6 9 12</pre>
<p>The commands can also query the names:</p>
<pre>&gt; rownames(m1)
[1] "z" "y" "x"

&gt; colnames(m1)
[1] "Z" "Y" "X" "W"</pre>
<p>Note that the basic names() command does not work for matrix objects:</p>
<pre>&gt; names(m1)
NULL</pre>
<p>If you use the rbind() command then the rows will be named according to the data names that you use unless you specify otherwise:</p>
<pre>&gt; d1 &lt;- 1:4 ; d2 = 5:8 ; d3 = 9:12

&gt; rbind(d1, d2, d3)
   [,1] [,2] [,3] [,4]
d1    1    2    3    4
d2    5    6    7    8
d3    9   10   11   12

&gt; rbind(Row1 = d1, Row2 = d2, Row3 = d3)
     [,1] [,2] [,3] [,4]
Row1    1    2    3    4
Row2    5    6    7    8
Row3    9   10   11   12</pre>
<p>Similarly with the cbind() command the columns take the names of the objects unless you specify them explicitly.</p>
<p>You can set the row and column names in one go using the dimnames() command. This requires a list() of two items (the row names and the column names):</p>
<pre>&gt; m1
  Z Y X  W
z 1 4 7 10
y 2 5 8 11
x 3 6 9 12

&gt; dimnames(m1) &lt;- list(letters[1:3], LETTERS[1:4])
&gt; m1
  A B C  D
a 1 4 7 10
b 2 5 8 11
c 3 6 9 12</pre>
<p>If you use the matrix() command you can incorporate the dimnames() command within it to set the names:</p>
<pre>&gt; m3 &lt;- <span style="color: #0000ff;">matrix</span>(1:12, nrow = 3, dimnames = list(month.abb[1:3], month.abb[4:7]))
&gt; m3
    Apr May Jun Jul
Jan   1   4   7  10
Feb   2   5   8  11
Mar   3   6   9  12</pre>
<p>Of course you can also use the dimnames() command to view the current names – more on this another time.</p>
<p>The post <a href="https://dataanalytics.org.uk/naming-rows-and-columns-of-a-matrix-in-r/">Naming rows and columns of a matrix in R</a> appeared first on <a href="https://dataanalytics.org.uk">Data Analytics</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
