Matrices

  1. Make a 5x3 matrix (5 rows, 3 columns) using matrix( ).

Ex: matrix(1:6,nrow=3,ncol=2) makes a 3x2 matrix using numbers between 1 and 6.

matrix(1:15, nrow = 5, ncol = 3)
##      [,1] [,2] [,3]
## [1,]    1    6   11
## [2,]    2    7   12
## [3,]    3    8   13
## [4,]    4    9   14
## [5,]    5   10   15

  1. What happens when you use byrow = TRUE in your matrix( ) as an additional argument? Assign the matrix to a variable m.
m = matrix(1:15, nrow = 5, ncol = 3, byrow = T)
m
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
## [5,]   13   14   15

  1. Extract the first 2 columns and first 3 rows of your matrix using [ ] notation.
m[1:3, 1:2]
##      [,1] [,2]
## [1,]    1    2
## [2,]    4    5
## [3,]    7    8

  1. Extract the last two rows of the matrix you created previously.
m[, c(ncol(m)-1, ncol(m))]
##      [,1] [,2]
## [1,]    2    3
## [2,]    5    6
## [3,]    8    9
## [4,]   11   12
## [5,]   14   15

  1. Multiplication table: write an R code to print out the 9x9 multiplication table.
x=rep(1:9, 9)
y=x[order(x)]

matrix(y*x, ncol=9, nrow = 9)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,]    1    2    3    4    5    6    7    8    9
##  [2,]    2    4    6    8   10   12   14   16   18
##  [3,]    3    6    9   12   15   18   21   24   27
##  [4,]    4    8   12   16   20   24   28   32   36
##  [5,]    5   10   15   20   25   30   35   40   45
##  [6,]    6   12   18   24   30   36   42   48   54
##  [7,]    7   14   21   28   35   42   49   56   63
##  [8,]    8   16   24   32   40   48   56   64   72
##  [9,]    9   18   27   36   45   54   63   72   81

  1. Create a vector x containing numbers from 1 to 15 and a vector y containing numbers from 24 to 35. Can you combine these vectors into a matrix? Explain the result.
x = 1:15
y = 24:35

mat = cbind(x, y)
## Warning in cbind(x, y): number of rows of result is not a multiple of vector
## length (arg 2)
mat
##        x  y
##  [1,]  1 24
##  [2,]  2 25
##  [3,]  3 26
##  [4,]  4 27
##  [5,]  5 28
##  [6,]  6 29
##  [7,]  7 30
##  [8,]  8 31
##  [9,]  9 32
## [10,] 10 33
## [11,] 11 34
## [12,] 12 35
## [13,] 13 24
## [14,] 14 25
## [15,] 15 26

  1. Create a new matrix called M by selecting all rows except the last 3 and all the columns from the previous matrix. Evaluate the numbers of rows and columns of the matrix.
M = mat[1:(nrow(mat)-3),]
nrow(M)
## [1] 12
ncol(M)
## [1] 2

  1. Assign rownames and colnames to M.
colnames(M) = c("col1","col2")
rownames(M) = c(1:12)

  1. Create a logical matrix (mix TRUE and FALSE values) L of same dimensions as M.

Sum L and M. What happened? Comment the result.

Finally, multiply L and M and explain the result.

dim(M)
## [1] 12  2
L = matrix(c(rep(FALSE, 3), rep(TRUE, 5), rep(FALSE, 9), rep(TRUE,7)), ncol = 2, nrow = 12)
dim(L)
## [1] 12  2
M+L
##    col1 col2
## 1     1   24
## 2     2   25
## 3     3   26
## 4     5   27
## 5     6   28
## 6     7   30
## 7     8   31
## 8     9   32
## 9     9   33
## 10   10   34
## 11   11   35
## 12   12   36
M*L
##    col1 col2
## 1     0    0
## 2     0    0
## 3     0    0
## 4     4    0
## 5     5    0
## 6     6   29
## 7     7   30
## 8     8   31
## 9     0   32
## 10    0   33
## 11    0   34
## 12    0   35

  1. Perform the algebric matrix multiplication of L and M
Lt = t(L)
M %*% Lt
##    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
## 1     0    0    0    1    1   25   25   25   24    24    24    24
## 2     0    0    0    2    2   27   27   27   25    25    25    25
## 3     0    0    0    3    3   29   29   29   26    26    26    26
## 4     0    0    0    4    4   31   31   31   27    27    27    27
## 5     0    0    0    5    5   33   33   33   28    28    28    28
## 6     0    0    0    6    6   35   35   35   29    29    29    29
## 7     0    0    0    7    7   37   37   37   30    30    30    30
## 8     0    0    0    8    8   39   39   39   31    31    31    31
## 9     0    0    0    9    9   41   41   41   32    32    32    32
## 10    0    0    0   10   10   43   43   43   33    33    33    33
## 11    0    0    0   11   11   45   45   45   34    34    34    34
## 12    0    0    0   12   12   47   47   47   35    35    35    35

  1. Generate different vectors:
    • 13 random numbers with mean = 6, sd = 3
    • 13 random numbers uniformly distributed between -30 and 140
    • a logical vector identifying the numbers in the first vector that are > 4.5
    • a logical vector identifying the numbers in the first vector that are, in absolute value, < 8
    • a logical vector identifying the numbers in the second vector that are, in absolute value, > 20 and negative
    • a vector containing the sum between the first vector and the second vector
    • a vector containing the multiplication between the third vector and the fourth vector

Create a matrix binding all the vectors by row, evaluate the dimensions, traspose it and evaluate the dimensions again.

one = rnorm(13, 6, 3)
two = runif(13, -30, 140)
three = one > 4.5
four = abs(one) < 8
five = abs(two) > 20 & abs(two) < 0
six = one + two
seven = three * four

mat = rbind(one, two, three, four, five, six, seven)

dim(mat)
## [1]  7 13
mat = t(mat)

dim(mat)
## [1] 13  7

  1. Create a numeric 5x5 matrix, containing numbers from 1 to 5. Transform the matrix in a upper triangular matrix by assigning to the lower triangle NA values. Then:
    • Extract the values on the diagonal
    • Extract the value in the third column and second row.
    • Extract values in the fourth column.
    • Extract values in the fifth row and assign them to a vector a.
    • Print the positions of the vector a in which the number 5 is found.
    • Create a logical vector b by testing whether the values in a are contained in c(1,3)
    • Use the vector b to extract from the fifth column of the matrix the values where b is TRUE
matrix = matrix(rep(c(1,2,3,4,5), 5), 5, 5)
matrix[lower.tri(matrix)] <- NA

diag(matrix)
## [1] 1 2 3 4 5
matrix[2,3]
## [1] 2
matrix[, 4]
## [1]  1  2  3  4 NA
a = matrix[5,]

which(a == 5)
## [1] 5
b = a %in% c(1,3)

b
## [1] FALSE FALSE FALSE FALSE FALSE
matrix[b,5]
## numeric(0)