Author
Affiliation

Dr Wei Miao

UCL School of Management

Published

September 27, 2024

1 Induction Week

Create a sequence of {1,1,2,2,3,3,3}.

Code
# solution 1

c(1, 1, 2, 2, 3, 3, 3)
[1] 1 1 2 2 3 3 3
Code
# solution 2

c(rep(1, 2), rep(2, 2), rep(3, 3))
[1] 1 1 2 2 3 3 3

Create a geometric sequence {2,4,8,16,32} using seq().

We can see that the sequence is a geometric sequence with a common ratio of 2.

  • The seq() function generates a sequence from 1 to 5 with a step of 1.
Code
# solution

seq(1, 5, 1) # this generates a sequence from 1 to 5 with a step of 1
[1] 1 2 3 4 5
  • The ^ operator calculates the power of 2 raised to the power of the sequence generated by seq(). Remember that R is vectorized, so the ^ operator will apply to each element in the sequence.
Code
# solution

2^seq(1, 5, 1) # this generates a geometric sequence {2^1, 2^2, 2^3, 2^4, 2^5}
[1]  2  4  8 16 32

Create a vector of 10 numbers from 1 to 10, and extract the 2nd, 4th, and 6th elements.

Code
# solution

x <- 1:10 # create a vector of 10 numbers from 1 to 10

x[c(2, 4, 6)] # use [] to extract the 2nd, 4th, and 6th elements
[1] 2 4 6

Create a vector of 5 numbers from 1 to 5, and check if 3 is in the vector.

Code
# solution

x <- 1:5 # create a vector of 5 numbers from 1 to 5

3 %in% x # check if 3 is in the vector
[1] TRUE

Now the interest rate is 0.1, and you have 1000 pounds in your bank account. Calculate the amount in your bank account after 1 year, 2 years, and 3 years, respectively.

First, set the interest rate to 0.1 and the initial amount to 1000.

Then, calculate the amount in your bank account after 1 year, 2 years, and 3 years, respectively.

Since the interest is compounded annually, the formula is:

[ A = P(1 + r)^n ]

where:

  • (A) is the amount in your bank account after (n) years
  • (P) is the initial amount
  • (r) is the interest rate
  • (n) is the number of years
Code
# solution

interest_rate <- 0.1 # set the interest rate to 0.1

initial_amount <- 1000 # set the initial amount to 1000

# calculate the amount in your bank account after 1 year, 2 years, and 3 years, respectively

# generate the geometric sequence from 1 to 3 years

initial_amount * (1 + interest_rate)^(1:3) # use the formula A = P(1 + r)^n
[1] 1100 1210 1331

2 Week 1