MH3511
MH3511
Exercise 1.1.
R can be used as a calculator. Make sure you are familiar with basic arithmetics by perform-
ing the computations below:
1. −3(287 + 359),
2. 14
4,
3. 34 − 43 ,
4. 37 (mod 5).
[1]: (-3)*(287+359)
-1938
[2]: 14/4
3.5
[3]: 3^4-4^3; 3**4-4**3
17
17
[4]: 37 %% 5
1
Exercise 1.2.
R has several classes, including: logical, integer, numeric, complex, character. This exercise
is to practice basic classes and variable assignment.
1. Assign ‘MH1234’ to ‘mycourse’.
2. Assign ‘A-’ to ‘mygrade’.
3. Assign 76 to ‘myscore’.
4. Assign TRUE to ‘mycore’.
Use class() to check the data type of the above variables. If you have no variable from a
given class, create a variable assignment such that the resulting variable is of the missing
class.
’character’
’character’
’numeric’
’logical’
’integer’
’complex’
2
Exercise 1.3.
Consider the following commands in R, to practice seq(), rep() and c(). Before trying them
out, see if you know what will be the output:
1. seq(10)
2. seq(0, 10, length=10)
3. seq(0, 10, by = 1)
4. rep(2, 3)
5. rep(-1, 4)
6. rep(1:3, 2)
7. rep(‘A’, 4)
8. c(rep(‘A’,3), rep(‘B’,3))
9. rep(c(‘A’, ‘B’), 3)
[12]: seq(10)
1 2 3 4 5 6 7 8 9 10
[13]: seq(0, 10, length=10)
0 1 2 3 4 5 6 7 8 9 10
[15]: rep(2,3)
2 2 2
[16]: rep(-1,4)
-1 -1 -1 -1
[17]: rep(1:3, 2)
1 2 3 1 2 3
[18]: rep('A', 4)
3
Exercise 1.4.
[21]: seq(3, 9, 2)
3 5 7 9
[22]: c(rep(2,2), rep(3,3), rep(4,4))
2 2 3 3 3 4 4 4 4
[23]: c(rep(TRUE, 2), rep(FALSE, 3))
4
Exercise 1.5.
Using R:
1. Create a matrix ‘matrix4x2’ with four rows and two columns with the values 1, 2, 3, 4
in the first column and 5, 6, 7, 8 in the second column.
2. Multiply the matrix matrix4x2 with the vector (1, 1, 1, 1) (on the left). Find another
way to obtain the same result using matrix manipulation instead of vector multiplica-
tion.
3. Create a matrix ‘matrix2x4‘ with four columns and two rows with the values 1, 2, 3, 4
in the first row and 11, 12, 13, 14 in the second row.
4. Multiply matrix4x2 with matrix2x4, and matrix2x4 with matrix4x2.
5. Select the first 2 rows of matrix4x2, and name it ‘matrix2x2‘.
6. Is the matrix2x2 invertible? if yes, find the inverse of matrix2x2.
7. Create a diagonal matrix ‘diag3’ with (−1, −2, −3) on the diagonal.
8. Mutiply the vector (2, 4, 8) with the diagonal matrix diag3. Find another wayt to
obtain the same result using vector operations instead of vector-matrix multiplication.
1 5
2 6
3 7
4 8
[31]: c(1,1,1,1)%*%matrix4x2
10 26
10
26
[35]: matrix2x4 <- matrix(c(1,2,3,4,11,12,13,14), byrow = TRUE, nrow=2, ncol=4)
matrix2x4
1 2 3 4
11 12 13 14
30 70
130 330
56 62 68 74
68 76 84 92
80 90 100 110
92 104 116 128
5
[38]: matrix2x2 <- matrix4x2[1:2,]
matrix2x2
1 5
2 6
[39]: det(matrix2x2)
-4
[40]: solve(matrix2x2)
-1.5 1.25
0.5 -0.25
-1 0 0
0 -2 0
0 0 -3
[45]: c(2,4,8)%*%diag3
-2 -8 -24
[46]: c(2,4,8)*c(-1,-2,-3)
-2 -8 -24
6
Exercise 1.6.
[49]: Days[Poker>0]
-314
-62.8
[54]: min(c(Poker, Roulette)); max(c(Poker, Roulette))
-350
240
[55]: PokerRoulette <- data.frame(Days, Poker, Roulette)
PokerRoulette
7
Days Poker Roulette
Monday 140 -24
Tuesday -50 -50
Wednesday 20 100
Thursday -120 -350
Friday 240 10