Vector and Matrix
For a positive integer \(n\), an \(n\)-dimensional vector is a \(1\)-dimensional \(n\) array.
For positive integers \(m\) and \(n\), an \(m \times n\) matrix is a \(2\)-dimensional \(m \times n\) array.
## Example 1
v <- c(907,220,625,502)
v
## Example 8
M <- matrix(c(1,1,1,2,1,3,1,4),nrow=2,ncol=4)
M
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 3 4
r1 <- c(1, 1, 1, 1)
r2 <- c(1, 2, 3, 4)
M <- rbind(r1, r2)
M
[,1] [,2] [,3] [,4]
r1 1 1 1 1
r2 1 2 3 4
c1 <- c(1, 1)
c2 <- c(1, 2)
c3 <- c(1, 3)
c4 <- c(1, 4)
M <- cbind(c1,c2,c3,c4)
M
c1 c2 c3 c4
[1,] 1 1 1 1
[2,] 1 2 3 4
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
The Smarket data set from the ISLR package
Information on variables of the Smarket data set:
library(ISLR)
data(Smarket)
#Smarket
head(Smarket)
Year Lag1 Lag2 Lag3 Lag4 Lag5 Volume Today Direction
1 2001 0.381 -0.192 -2.624 -1.055 5.010 1.1913 0.959 Up
2 2001 0.959 0.381 -0.192 -2.624 -1.055 1.2965 1.032 Up
3 2001 1.032 0.959 0.381 -0.192 -2.624 1.4112 -0.623 Down
4 2001 -0.623 1.032 0.959 0.381 -0.192 1.2760 0.614 Up
5 2001 0.614 -0.623 1.032 0.959 0.381 1.2057 0.213 Up
6 2001 0.213 0.614 -0.623 1.032 0.959 1.3491 1.392 Up
Year Lag1 Lag2 Lag3 Lag4 Lag5 Volume Today Direction
1245 2005 0.252 -0.024 -0.584 -0.285 -0.141 2.06517 0.422 Up
1246 2005 0.422 0.252 -0.024 -0.584 -0.285 1.88850 0.043 Up
1247 2005 0.043 0.422 0.252 -0.024 -0.584 1.28581 -0.955 Down
1248 2005 -0.955 0.043 0.422 0.252 -0.024 1.54047 0.130 Up
1249 2005 0.130 -0.955 0.043 0.422 0.252 1.42236 -0.298 Down
1250 2005 -0.298 0.130 -0.955 0.043 0.422 1.38254 -0.489 Down
## Example 9
#head(Smarket)
Smarket[2,]
Year Lag1 Lag2 Lag3 Lag4 Lag5 Volume Today Direction
2 2001 0.959 0.381 -0.192 -2.624 -1.055 1.2965 1.032 Up
M2 <- rbind(Smarket[3,], Smarket[4,], Smarket[7,], Smarket[9,], Smarket[10,])
M2
Year Lag1 Lag2 Lag3 Lag4 Lag5 Volume Today Direction
3 2001 1.032 0.959 0.381 -0.192 -2.624 1.4112 -0.623 Down
4 2001 -0.623 1.032 0.959 0.381 -0.192 1.2760 0.614 Up
7 2001 1.392 0.213 0.614 -0.623 1.032 1.4450 -0.403 Down
9 2001 0.027 -0.403 1.392 0.213 0.614 1.1640 1.303 Up
10 2001 1.303 0.027 -0.403 1.392 0.213 1.2326 0.287 Up
### Practical Applications
dim(Smarket)
x <- 1:1250
plot(x, Smarket[,2],type="l")

[1] 0.381 0.959 1.032 -0.623 0.614 0.213 1.392 -0.403 0.027 1.303
[11] 0.287 -0.498 -0.189 0.680 0.701 -0.562 0.546 -1.747 0.359 -0.151
[21] -0.841 -0.623 -1.334 1.183 -0.865 -0.218 0.812 -1.891 -1.736 -1.851
[31] -0.195 -0.556 1.749 -0.766 -1.431 0.104 -0.568 0.586 0.998 0.645
[41] 0.226 -2.476 -4.318 1.483 -2.584 0.587 -1.962 1.763 -2.408 -1.792
[51] -0.406 1.991 1.128 2.557 -2.443 -0.463 1.078 -1.246 -3.439 -0.290
[61] 4.368 -1.998 0.812 2.707 -0.213 1.510 -0.323 1.028 3.889 1.254
[71] -0.854 -1.498 -1.216 1.594 0.470 1.501 -0.287 1.359 0.078 -1.487
[81] 1.444 -0.245 -0.183 -0.449 -0.029 -0.758 0.261 0.042 2.845 0.272
[91] 0.269 1.615 -0.263 -1.553 0.320 -1.182 -0.779 -1.566 0.620 0.386
[101] 0.511 1.299 -1.055 0.546 -0.940 -0.836 0.116 -1.135 -1.750 -0.452
[111] -0.488 0.343 0.871 1.136 -0.945 -0.551 -0.151 -0.468 1.249 -0.148
[121] 1.008 -0.184 -1.232 -2.350 0.688 -1.440 -0.113 2.369 0.624 -1.088
[131] 0.997 -0.554 0.605 -0.343 -1.637 -1.627 1.608 1.045 0.240 -0.108
[141] 0.557 0.388 0.396 -0.524 -1.142 0.327 -1.733 -0.008 0.569 0.095
[151] -0.383 -0.734 0.309 -1.666 0.812 -1.208 0.696 -0.276 1.965 -0.483
[161] -1.501 -1.115 -1.700 0.403 -0.056 -0.106 -2.239 -1.864 0.623 -4.922
[171] -0.580 -1.611 -3.106 -1.903 3.898 0.879 -0.517 1.149 2.192 -0.230
[181] 1.231 1.993 -0.247 0.164 -0.834 -0.536 2.294 1.521 -0.527 -0.153
[191] 0.694 -1.863 -0.787 0.456 1.530 -0.470 0.039 1.372 0.411 -2.382
[201] -1.717 -0.001 2.295 0.286 1.439 1.453 -0.273 0.246 0.158 -0.177
[211] 1.856 0.186 0.090 -0.314 1.090 -0.730 -0.493 1.171 0.615 -0.684
[221] -1.825 1.035 -0.066 -0.838 1.319 2.232 -0.278 -0.753 -1.587 -0.278
[231] 0.027 -1.556 0.331 1.003 0.755 0.581 -0.838 0.435 -0.021 0.412
[241] 0.675 0.336
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[19] 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
[37] 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
[55] 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
[73] 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[91] 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
[127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
[163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
[181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
[199] 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
[217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
[235] 235 236 237 238 239 240 241 242
plot(x[1:242], Smarket[1:242,2],type="l")
Exercise
Create a \(12 \times 12\) matrix from the following table:
Extract the following sub-matrices:
- March to Sep \(\times\) Aug
- March to Sep \(\times\) Aug to Dec
- March to Sep \(\times\) c(May, Aug, Dec)