-
Notifications
You must be signed in to change notification settings - Fork 46
/
chapter1.Rmd
302 lines (232 loc) · 7.31 KB
/
chapter1.Rmd
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
---
title: R Basics
description: >-
In this course we introduce you to the basics of computing and analyzing data
in the user-friendly and helpful R interface. This first chapter starts with
the very basics of functions, objects to get us acquainted with the world of
R.
free_preview: true
---
## Using variables 1
```yaml
type: NormalExercise
key: 0cec712ce0
lang: r
xp: 100
skills:
- 1
```
We are going to use the formula $ n(n+1)/2 $ to quickly compute the sum of the first $n$ positive integers, without adding them up individually.
`@instructions`
- Define `n<-100`
- Write code to compute the sum of the first $n$ integers (in this case, the integers from 1 to 100) using the formula $ n(n+1)/2$
- Make sure you do not erase or change the sample code on DataCamp exercises
`@hint`
- Define `n` to be 100 in one line and simply type the formula using R code in the second line.
- Remember that in R you multiply using `*`.
`@pre_exercise_code`
```{r}
# no pec
```
`@sample_code`
```{r}
# Here is how you compute the sum for the first 20 integers
20*(20+1)/2
# However, we can define a variable to use the formula for other values of n
n <- 20
n*(n+1)/2
n <- 25
n*(n+1)/2
# Below, write code to calculate the sum of the first 100 integers
```
`@solution`
```{r}
# Here is how you compute the sum for the first 20 integers
20*(20+1)/2
# However, we can define a variable to use the formula for other values of n
n <- 20
n*(n+1)/2
n <- 25
n*(n+1)/2
# Below, write code to calculate the sum of the first 100 integers
n <- 100
n*(n+1)/2
```
`@sct`
```{r}
test_error()
test_object("n", incorrect_msg = "Make sure that you use `n` as your variable name and that you have assigned the correct value to `n`.")
test_output_contains("5050", incorrect_msg = "You are not providing a formula that gives the correct answer. Look at the example code.")
success_msg("Good job! Let`s apply this to another question.")
```
---
## Using variables 2
```yaml
type: NormalExercise
key: 213f198401
lang: r
xp: 100
skills:
- 1
```
What is the sum of the first 1000 positive integers?
We can use the formula $ n(n+1)/2 $ to quickly compute this quantity.
`@instructions`
- Use the same formula as the last exercise but change the value of `n`. Make sure you use the variable name `n` to store the value 1000.
- Instead of typing the result, use the formula and defined variable.
`@hint`
Use the same R code as you used in the first question after changing the value of `n`.
`@pre_exercise_code`
```{r}
# no pec
```
`@sample_code`
```{r}
# Below, write code to calculate the sum of the first 1000 integers
```
`@solution`
```{r}
# Below, write code to calculate the sum of the first 1000 integers
n <- 1000
n*(n+1)/2
```
`@sct`
```{r}
test_object("n", incorrect_msg = "Make sure that you use `n` as your variable name and that you have assigned the correct value to `n`.")
test_output_contains("500500", incorrect_msg = "You are not providing a formula that gives the correct answer. Compare your code with the previous question.")
success_msg("Good job! Let`s get to work on another question!")
```
---
## Functions
```yaml
type: MultipleChoiceExercise
key: 0e2a0ce37a
lang: r
xp: 50
skills:
- 1
```
Run the following code in the R console:
```
n <- 1000
x <- seq(1,n)
sum(x)
```
Based on the result, what do you think the functions `seq` and `sum` do? You can use the help system.
`@possible_answers`
- sum creates a list of numbers and seq adds them up.
- seq creates a list of numbers and sum adds them up.
- seq computes the difference between two arguments and sum computes the sum of 1 through 1000.
- sum always returns the same number
`@hint`
Go to R console and type `seq(1,5)`. See what you get. Then got the R console again and type `sum(seq(1,5))`. Change the `5` to other numbers.
You can also type `?` before a function to get help.
`@pre_exercise_code`
```{r}
# no pec
```
`@sct`
```{r}
msg1 = "Try again! Read the choices carefully. Try again."
msg2 = "Well done. Proceed to the next exercise."
msg3 = "There's no specification of 1 to 1000 for sum. Try again."
msg4 = "Read the help file for sum by typing `?sum` in the R console."
test_mc(correct = 2, feedback_msgs = c(msg1,msg2,msg3,msg4))
```
---
## Nested function calls 1
```yaml
type: NormalExercise
key: 7df9567ad1
lang: r
xp: 100
skills:
- 1
```
In math and programming we say we evaluate a function when we replace arguments with specific values. So if we type `log2(16)` we evaluate the `log2` function to get the log base 2 of `16` which is `4`.
In R it is often useful to evaluate a function inside another function.
For example, `sqrt(log2(16))` will calculate the log to the base 2 of 16 and then compute the square root of that value.
So the first evaluation gives a 4 and this gets evaluated by `sqrt` to give the final answer of 2.
`@instructions`
- Use one line of code to compute the log, to the base 10, of the square root of 100.
- Make sure your code includes the `log10` and `sqrt` functions.
`@hint`
The `log10` function call should be the argument for the `sqrt` function.
`@pre_exercise_code`
```{r}
# no pec
```
`@sample_code`
```{r}
# log to the base 2
log2(16)
# sqrt of the log to the base 2 of 16:
sqrt(log2(16))
# Compute log to the base 10 (log10) of the sqrt of 100. Do not use variables.
```
`@solution`
```{r}
# log to the base 2
log2(16)
# sqrt of the log to the base 2 of 16:
sqrt(log2(16))
# Compute log to the base 10 (log10) of the sqrt of 100. Do not use variables.
log10(sqrt(100))
```
`@sct`
```{r}
test_error()
test_output_contains("log10(sqrt(100))", incorrect_msg = "Make sure you use the right base for the log and put the sqrt function inside of the log function.")
success_msg("Very good!")
```
---
## Nested functions call 2
```yaml
type: MultipleChoiceExercise
key: b755d1a929
lang: r
xp: 50
skills:
- 1
```
Which of the following will always return the numeric value stored in `x`? You can try out examples and use the help system in the R console.
`@possible_answers`
- `log(10^x)`
- `log10(x^10)`
- `log(exp(x))`
- `exp(log(x, base = 2))`
`@hint`
You are looking for the case in which a function and its inverse are called sequentially. You can also try out each one on the console: assign a numeric value to `x` then try out each of the lines of code given in the choices.
`@pre_exercise_code`
```{r}
# no pec
```
`@sct`
```{r}
msg1 = "Try again! Note that `log(10^1)` is not 1 but the natural log of 10."
msg2 = "Try again! Note that `log(1^10)` is 0 not 1."
msg3 = "Well done. Proceed to the next exercise"
msg4 = "Try again! Note that `exp(log(2, base = 2)` is 0 not 2."
test_mc(correct = 3, feedback_msgs = c(msg1,msg2,msg3,msg4))
```
---
## End of Assessment 1
```yaml
type: PureMultipleChoiceExercise
key: 5d1f828748
lang: r
xp: 50
skills:
- 1
```
This is the end of the programming assignment for this section. Please DO NOT click through to additional assessments from this page. Please DO answer the question on this page. If you do click through, your scores may NOT be recorded.
Click on "Awesome" to get the "points" for this question and then return to the course on edX.
You can now close this window to go back to the <a href='https://www.edx.org/course/data-science-r-basics-2'>course</a>.
`@hint`
- No hint necessary!
`@possible_answers`
- [Awesome]
- Nope
`@feedback`
- Great! Now navigate back to the course on edX!
- Now navigate back to the course on edX!