X = 34
Y = 3
X + Y
X - Y
X * Y
X ^ Y
X %% Y
class(X)
A = 'Helsinki'
B = 'Warsaw'
C = 'Stockholm'
D = 'Budapest'
class(A)
v <- c(3, 6, 9, 15)
w <- c(4, 6, 8, 12)
v + w
cities <- c(A, B, C, D)
cities
names(w) <- c(A, B, C, D)
w
w[3]
w['Stockholm']
w[c(2, 3)]
w[c(2:4)]
mean(w)
w > 7
w[w>7]
matrix(1:12, byrow=TRUE, nrow=3)
1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
matrix(1:12, byrow=FALSE, nrow=3)
1 | 4 | 7 | 10 |
2 | 5 | 8 | 11 |
3 | 6 | 9 | 12 |
v <- c(3, 6, 9, 15)
w <- c(4, 6, 8, 12)
v_w <- c(v, w)
v_w
v_w_mat <- matrix(v_w, byrow=TRUE, nrow=4)
v_w_mat
3 | 6 |
9 | 15 |
4 | 6 |
8 | 12 |
mat_A <- matrix(1:9, byrow=TRUE, nrow=3)
print(mat_A)
[,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9
rownames(mat_A) <- c('Row 1', 'Row 2', 'Row 3')
colnames(mat_A) <- c('Col 1', 'Col 2', 'Col 3')
print(mat_A)
Col 1 Col 2 Col 3 Row 1 1 2 3 Row 2 4 5 6 Row 3 7 8 9
rowSums(mat_A)
colSums(mat_A)
mat_B = matrix(2:10, byrow = FALSE, nrow = 3)
mat_B
2 | 5 | 8 |
3 | 6 | 9 |
4 | 7 | 10 |
mat_A + mat_B
Col 1 | Col 2 | Col 3 | |
---|---|---|---|
Row 1 | 3 | 7 | 11 |
Row 2 | 7 | 11 | 15 |
Row 3 | 11 | 15 | 19 |
mat_A * mat_B
Col 1 | Col 2 | Col 3 | |
---|---|---|---|
Row 1 | 2 | 10 | 24 |
Row 2 | 12 | 30 | 54 |
Row 3 | 28 | 56 | 90 |
mat_A / mat_B
Col 1 | Col 2 | Col 3 | |
---|---|---|---|
Row 1 | 0.500000 | 0.4000000 | 0.3750000 |
Row 2 | 1.333333 | 0.8333333 | 0.6666667 |
Row 3 | 1.750000 | 1.1428571 | 0.9000000 |
mat_A %*% mat_B # matrix multiplication
cbind(mat_A, mat_B)
Col 1 | Col 2 | Col 3 | ||||
---|---|---|---|---|---|---|
Row 1 | 1 | 2 | 3 | 2 | 5 | 8 |
Row 2 | 4 | 5 | 6 | 3 | 6 | 9 |
Row 3 | 7 | 8 | 9 | 4 | 7 | 10 |
rbind(mat_A, mat_B)
Col 1 | Col 2 | Col 3 | |
---|---|---|---|
Row 1 | 1 | 2 | 3 |
Row 2 | 4 | 5 | 6 |
Row 3 | 7 | 8 | 9 |
2 | 5 | 8 | |
3 | 6 | 9 | |
4 | 7 | 10 |
ls()
mat_C = matrix(1:30, byrow=TRUE, nrow=5)
mat_C
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 |
mat_C[,5]
mat_C[3,]
mat_C[2:5,4]