0% found this document useful (1 vote)
57 views8 pages

MH3511

This document contains exercises to practice basic R programming concepts including arithmetic operations, variable assignment, data types, vectors, matrices, and data frames. The exercises involve calculating expressions, assigning variables of different classes, creating vectors using sequences and repetitions, performing matrix and vector operations, and creating a data frame from poker and roulette game data.

Uploaded by

Alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
57 views8 pages

MH3511

This document contains exercises to practice basic R programming concepts including arithmetic operations, variable assignment, data types, vectors, matrices, and data frames. The exercises involve calculating expressions, assigning variables of different classes, creating vectors using sequences and repetitions, performing matrix and vector operations, and creating a data frame from poker and roulette game data.

Uploaded by

Alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1 Labs for MH3511: Week 1

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.

[5]: mycourse <- 'MH1234'

[6]: mygrade <- 'mygrade'

[7]: myscore <- 76

[8]: mycore <- TRUE

[9]: class(mycourse); class(mygrade); class(myscore); class(mycore)

’character’
’character’
’numeric’
’logical’

[10]: # creates an integer


myinteger <- 3L; class(myinteger)

’integer’

[11]: # creates a complex number


mycomplex <- 3i; class(mycomplex)

’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.11111111111111 2.22222222222222 3.33333333333333 4.44444444444444 5.55555555555556


6.66666666666667 7.77777777777778 8.88888888888889 10
[14]: seq(0, 10, by =1)

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)

’A’ ’A’ ’A’ ’A’


[19]: c(rep('A',3), rep('B',3))

’A’ ’A’ ’A’ ’B’ ’B’ ’B’


[20]: rep(c('A','B'), 3)

’A’ ’B’ ’A’ ’B’ ’A’ ’B’

3
Exercise 1.4.

Use seq(), rep() and c() to create the following vectors:


1. (3, 5, 7, 9),
2. (2, 2, 3, 3, 3, 4, 4, 4, 4),
3. (TRUE, TRUE, FALSE, FALSE, FALSE),
4. (‘High’, ‘Medium’, ‘Low’, ‘High’, ‘Medium’, ‘Low’).

[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))

TRUE TRUE FALSE FALSE FALSE


[24]: rep(c('High', 'Medium', 'Low'), 2)

’High’ ’Medium’ ’Low’ ’High’ ’Medium’ ’Low’

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.

[30]: matrix4x2 <- matrix(c(1,2,3,4,5,6,7,8), byrow = FALSE, nrow=4, ncol=2)


matrix4x2

1 5
2 6
3 7
4 8

[31]: c(1,1,1,1)%*%matrix4x2

10 26

[34]: sum(matrix4x2[,1]); sum(matrix4x2[,2])

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

[36]: matrix2x4%*%matrix4x2; matrix4x2%*%matrix2x4

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

[42]: diag3 <- diag(c(-1,-2,-3))


diag3

-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.

This table summarizes a week of gain/loss at poker and roulette.


Monday Tuesday Wednesday Thursday Friday
Poker 140 -50 20 -120 240
Roulette -24 -50 100 -350 10
1. Assign three 5-dimensional vectors: one for Poker, one for Roulette, and one for Days.
2. Using (some of) these three 5-dimensional vectors, compute the days for which poker
gave gains.
3. Using (some of) these three 5-dimensional vectors, compute the days for which poker
and roulette combined gave gains.
4. What is the total gain (or loss) at roulette? What is the average gain (or loss) at
roulette?
5. What is the highest gain and the worst loss across both poker and roulette?
6. Create a dataframe containing the above table.

[48]: Poker <- c(140, -50, 20, -120, 240)


Roulette <- c(-24, -50, 100, -350, 10)
Days <- c('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday')

[49]: Days[Poker>0]

’Monday’ ’Wednesday’ ’Friday’

[51]: PokerandRoulette <- Poker + Roulette


PokerandRoulette

116 -100 120 -470 250


[52]: Days[PokerandRoulette >0]

’Monday’ ’Wednesday’ ’Friday’

[53]: sum(Roulette); mean(Roulette)

-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

You might also like