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
# solutionseq(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
# solution2^seq(1, 5, 1) # this generates a geometric sequence {2^1, 2^2, 2^3, 2^4, 2^5}
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
# solutioninterest_rate <-0.1# set the interest rate to 0.1initial_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 yearsinitial_amount * (1+ interest_rate)^(1:3) # use the formula A = P(1 + r)^n
---author: Dr Wei Miaodate: "`r (lubridate::ymd('20240927'))`"date-format: longinstitute: UCL School of Managementtitle: "Weekly R Exercise"format: html: defaultknitr: opts_chunk: echo: true warning: true message: true error: trueexecute: freeze: auto cache: trueeditor_options: chunk_output_type: inline---# Induction Week```{r}#| echo: falsequestion_index <-1```::: {.content-visible when-format='html'}::: {.panel-tabset}### **Question `r question_index<-question_index+1; question_index-1`**Create a sequence of {1,1,2,2,3,3,3}.### Answer```{r}# solution 1c(1, 1, 2, 2, 3, 3, 3)# solution 2c(rep(1, 2), rep(2, 2), rep(3, 3))```::::::::: {.content-visible when-format='html'}::: {.panel-tabset}### **Question `r question_index<-question_index+1; question_index-1`**Create a geometric sequence {2,4,8,16,32} using seq().### AnswerWe 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.```{r}# solutionseq(1, 5, 1) # this generates a sequence from 1 to 5 with a step of 1```- 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.```{r}# solution2^seq(1, 5, 1) # this generates a geometric sequence {2^1, 2^2, 2^3, 2^4, 2^5}```::::::::: {.content-visible when-format='html'}::: {.panel-tabset}### **Question `r question_index<-question_index+1; question_index-1`**Create a vector of 10 numbers from 1 to 10, and extract the 2nd, 4th, and 6th elements.### Answer```{r}# solutionx <-1:10# create a vector of 10 numbers from 1 to 10x[c(2, 4, 6)] # use [] to extract the 2nd, 4th, and 6th elements```::::::::: {.content-visible when-format='html'}::: {.panel-tabset}### **Question `r question_index<-question_index+1; question_index-1`**Create a vector of 5 numbers from 1 to 5, and check if 3 is in the vector.### Answer```{r}# solutionx <-1:5# create a vector of 5 numbers from 1 to 53%in% x # check if 3 is in the vector```::::::::: {.content-visible when-format='html'}::: {.panel-tabset}### **Question `r question_index<-question_index+1; question_index-1`**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.### AnswerFirst, 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```{r}# solutioninterest_rate <-0.1# set the interest rate to 0.1initial_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 yearsinitial_amount * (1+ interest_rate)^(1:3) # use the formula A = P(1 + r)^n```::::::# Week 1- The after-class exercise solutions are in the [Week 1 case study](Case-ProfitabilityAnalysis.qmd)