-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-Exercise.qmd
186 lines (108 loc) · 3.35 KB
/
R-Exercise.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
---
author: Dr Wei Miao
date: "`r (lubridate::ymd('20240927'))`"
date-format: long
institute: UCL School of Management
title: "Weekly R Exercise"
format:
html: default
knitr:
opts_chunk:
echo: true
warning: true
message: true
error: true
execute:
freeze: auto
cache: true
editor_options:
chunk_output_type: inline
---
# Induction Week
```{r}
#| echo: false
question_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 1
c(1, 1, 2, 2, 3, 3, 3)
# solution 2
c(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().
### Answer
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.
```{r}
# solution
seq(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}
# solution
2^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}
# 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
```
:::
:::
::: {.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}
# solution
x <- 1:5 # create a vector of 5 numbers from 1 to 5
3 %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.
### Answer
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
```{r}
# 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
```
:::
:::
# Week 1
- The after-class exercise solutions are in the [Week 1 case study](Case-ProfitabilityAnalysis.qmd)