-
Notifications
You must be signed in to change notification settings - Fork 6
/
MatrixSubsetting.html
96 lines (96 loc) · 6.66 KB
/
MatrixSubsetting.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<link rel="stylesheet" href="Class.css" type="text/css" />
</head>
<body>
<h2 id="section"></h2>
<p>We can subset a matrix by + row and column indices + a k x 2 matrix of row-column index pairs + a logical matrix with the same dimensions that</p>
<p>Consider a simple, toy matrix</p>
<pre class="sourceCode r"><code class="sourceCode r">m =<span class="st"> </span><span class="kw">matrix</span>(<span class="dv">1</span>:<span class="dv">15</span>, <span class="dv">5</span>, <span class="dv">3</span>, <span class="dt">byrow =</span> <span class="ot">TRUE</span>)
[,<span class="dv">1</span>] [,<span class="dv">2</span>] [,<span class="dv">3</span>]
[<span class="dv">1</span>,] <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>
[<span class="dv">2</span>,] <span class="dv">4</span> <span class="dv">5</span> <span class="dv">6</span>
[<span class="dv">3</span>,] <span class="dv">7</span> <span class="dv">8</span> <span class="dv">9</span>
[<span class="dv">4</span>,] <span class="dv">10</span> <span class="dv">11</span> <span class="dv">12</span>
[<span class="dv">5</span>,] <span class="dv">13</span> <span class="dv">14</span> <span class="dv">15</span></code></pre>
<p>Something this simple allows us to do the calculations in our head to see what R is doing.</p>
<p>Suppose we want to extract the elements from the upper triangular part of the matrix, include the diagonal itself. So we want the values 1, 2, 3, 5, 6, and 9.</p>
<p>We want all the elements where the column number is greater than or equal to the row index.</p>
<h2 id="section-1"></h2>
<p>What if we try</p>
<pre><code> m[c(1, 1, 1, 2, 2, 3), c(1, 2, 3, 2, 3, 3) ]</code></pre>
<p>This works as we discussed previously, but not necessarily as we want. Consider the first index vector, i.e. for the rows.</p>
<pre><code> m[c(1, 1, 1, 2, 2, 3), ]</code></pre>
<p>This will get the first row 3 times, then then second row twice and the third row once. So we will end up with six rows and 3 columns:</p>
<pre><code> [,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
[4,] 4 5 6
[5,] 4 5 6
[6,] 7 8 9</code></pre>
<p>Then if we apply the column subsetting <code>c(1, 2, 3, 2, 3, 3)</code>, we would get the first three columns, followed by the second and third, and then third, so again six columns with repeats.</p>
<p>In general, when we have <code>x[i, j]</code>, you can break this down into <code>x[i,][, j]</code>.</p>
<h2 id="subsetting-by-row-column-index-pairs">Subsetting by Row-Column Index Pairs</h2>
<p>We can create a k x 2 matrix in which each row contains the row and column number of an element we want. We can create this manually</p>
<pre><code>cbind(row = c(1, 1, 1, 2, 2, 3), column = c(1, 2, 3, 2, 3, 3))
row column
[1,] 1 1
[2,] 1 2
[3,] 1 3
[4,] 2 2
[5,] 2 3
[6,] 3 3</code></pre>
<p>We are specifying all the elements on the first row, the last 2 elements on the second row, and the third element of the third row.</p>
<p>We can use this to subset m:</p>
<pre class="sourceCode r"><code class="sourceCode r">m[<span class="kw">cbind</span>(<span class="dt">row =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">3</span>), <span class="dt">column =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">3</span>)) ]
[<span class="dv">1</span>] <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span> <span class="dv">5</span> <span class="dv">6</span> <span class="dv">9</span></code></pre>
<h2 id="subsetting-a-matrix-by-a-logical-matrix.">Subsetting a Matrix by a Logical Matrix.</h2>
<p>One way to get this is to create a 5 x 3 matrix (same dimensions as m) of logical values with TRUE for the cells we want to extract and FALSE for the others. We want</p>
<pre><code>i
[,1] [,2] [,3]
[1,] TRUE TRUE TRUE
[2,] FALSE TRUE TRUE
[3,] FALSE FALSE TRUE
[4,] FALSE FALSE FALSE
[5,] FALSE FALSE FALSE</code></pre>
<p>and then we can use this to subset m to get what we want.</p>
<pre><code>m[i]
[1] 1 2 5 3 6 9</code></pre>
<p>One way to create the matrix i is to use the functions row() and col(). row() creates a matrix with all the values in each row equal to that row number. Similarly, col() creates a matrix with all the elements in each column equal to the column number, e.g.,</p>
<pre class="sourceCode r"><code class="sourceCode r"> <span class="kw">col</span>(m)
[,<span class="dv">1</span>] [,<span class="dv">2</span>] [,<span class="dv">3</span>]
[<span class="dv">1</span>,] <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>
[<span class="dv">2</span>,] <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>
[<span class="dv">3</span>,] <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>
[<span class="dv">4</span>,] <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span>
[<span class="dv">5</span>,] <span class="dv">1</span> <span class="dv">2</span> <span class="dv">3</span></code></pre>
<p>So</p>
<pre><code>i = ( col(m) >= row(m) )</code></pre>
</body>
</html>