-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchapter6.Rmd
392 lines (275 loc) · 11.7 KB
/
chapter6.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
---
courseTitle : Introduction to R v2
chapterTitle : Lists
description : List have components of different types just like your to-do list at home or at work. Learn how to create, name and subset in this chapter!
framework : datamind
mode: selfcontained
---
## Lists, why would you need them?
Congratulations! At this point in the course you are alreade a (semi-)expert in:
- **Vectors** (one dimensional array) Holds numeric, character or logical values. The elements in one vector all have the same datatype.
- **Matrices** (two dimensional array) Holds numeric, character or logical values. The elements in one matrix all have the same datatype.
- **Data frames** (two-dimensional objects) Holds numeric, character or logical values. Within a column all elements have the same datatype, but between columns not necessarily.
Pretty sweet for an R newbie don't you think? ;-)
*** =instructions
1. Click Submit Answer to start learning everything about lists!
*** =hint
Just click the Run button.
*** =sample_code
```{r eval=FALSE}
# Just click the Run button!
```
*** =solution
```{r eval=FALSE}
# Just click the Run button.
```
*** =sct
```{r eval=FALSE}
DM.result <- TRUE
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Lists, why would you need them? (2)
A list in R is similar to your to-do list at work or school: The different items on your to-do list most likely differ in length, characteristic, type of to do...
A list in R allows you to gather a variety of objects under one name (the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required these objects are related to each other. Just like with your to-do list :-).
Maybe you can even say that a list is a kind of super data type ;-)
*** =instructions
1. Click Submit Answer to start the first exercise on lists.
*** =hint
Click Run to start the first exercise on lists.
*** =sample_code
```{r eval=FALSE}
# Click Run to start the first exercise on lists.
```
*** =solution
```{r eval=FALSE}
# Click Submit Answer to start the first exercise on lists.
```
*** =sct
```{r eval=FALSE}
DM.result <- TRUE
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Creating a list
Let's create our first list! To construct a list you use the function `list()`:
`my.list <- list(component1, component2...)`
The arguments to the list function are the list components. Remember, these components can be a collection of matrices, vectors...`
*** =instructions
1. Construct a list with `my.vector`, `my.matrix` and `my.df` as list components.
*** =hint
Just use the `list()` function with `my.vector`, `my.matrix` and `my.df` as arguments separated by a comma.
*** =sample_code
```{r eval=FALSE}
# Vector with numerics from 1 upto 10
my.vector <- 1:10
# Matrix with numerics from 1 upto 9
my.matrix <- matrix(1:9,ncol=3)
# First 10 elements of the built-in data frame mtcars
my.df <- mtcars[1:10,]
# Construct list with these different elements:
my.list <-
# Show list:
my.list
```
*** =solution
```{r eval=FALSE}
# Vector with numerics from 1 upto 10
my.vector <- 1:10
# Matrix with numerics from 1 upto 9
my.matrix <- matrix(1:9,ncol=3)
# First 10 elements of the built-in data frame mtcars
my.df <- mtcars[1:10,]
# Construct list with these different elements:
my.list <- list(my.vector,my.matrix,my.df)
# Show list:
my.list
```
*** =sct
```{r eval=FALSE}
names <- c("my.vector","my.matrix","my.df","my.list")
values <- c("my.vector","my.matrix","my.df","list(my.vector,my.matrix,my.df)")
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Creating a named list
Well done! Let's keep this train going!
Like on your to-do list, you want to avoid not knowing (or remembering) what the components of your to-do list stand for. That is why you should give names to them:
`list(name1=your.component1, name2=component2,?)`
This creates for you a list with the components `"name1"` and `"name2"` and so on.
*** =instructions
1. Change the code of the previous exercise (see editor) by adding names to the components. Use for `my.matrix` the name `MATRIX`, for `my.vector` `VECTOR` and for `my.df` `DATAFRAME`. Look at the console output to see how R prints a list.
*** =hint
No hints, you can do this!
*** =sample_code
```{r eval=FALSE}
# Vector with numerics from 1 upto 10
my.vector <- 1:10
# Matrix with numerics from 1 upto 9
my.matrix <- matrix(1:9,ncol=3)
# First 10 elements of the built-in data frame mtcars
my.df <- mtcars[1:10,]
# Construct list with these different elements:
my.list <- list(my.vector,my.matrix,my.df)
# Show list:
my.list
```
*** =solution
```{r eval=FALSE}
# Vector with numerics from 1 upto 10
my.vector <- 1:10
# Matrix with numerics from 1 upto 9
my.matrix <- matrix(1:9,ncol=3)
# First 10 elements of the built-in data frame mtcars
my.df <- mtcars[1:10,]
# Construct list with these different elements:
my.list <- list(VECTOR=my.vector,MATRIX=my.matrix,DATAFRAME=my.df)
# Show list:
my.list
```
*** =sct
```{r eval=FALSE}
name <- "my.list"
value <- "list(VECTOR=my.vector,MATRIX=my.matrix,DATAFRAME=my.df)"
DM.result <- closed_test(name,value)
```
*** =pre_exercise_code
```{r eval=FALSE}
```
---
## Creating a named list (2)
Being a huge movie fan (remember your job @ LucasFilms), you decide to start storing information on good movies with the help of lists.
Start by creating a list for the movie "The Shining". We already created the variables `actors` and `reviews` in your R workspace. Type `actors` or `reviews` in the console to check these.
*** =instructions
1. Create the variable `shining.list`. The list contains the following components:
- moviename: "The Shining"
- actors: a vector with main actors names
- reviews: a data frame containing some reviews
Do not forget to name the list components accordingly!
*** =hint
No hints, you can do this!
*** =sample_code
```{r eval=FALSE}
# Create the named vector shining.list
```
*** =solution
```{r eval=FALSE}
# Create the shining.list
shining.list <- list(moviename="The Shining",actors=actors, reviews=reviews )
```
*** =sct
```{r eval=FALSE}
names <- c("actors","reviews","shining.list")
values <- c("actors","reviews","list(moviename='The Shining',actors=actors, reviews=reviews )")
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
actors <- c("Jack Nicholson","Shelley Duvall","Danny Lloyd","Scatman Crothers","Barry Nelson")
sources <- c("IMDb1","IMDb2","IMDB3")
comments <- c("Best Horror Film I've Ever Seen","A truly brilliant and scary film from Stanley Kubrick","A masterpiece of psychological horror")
scores <- c(4.5,4,5)
reviews <- data.frame(scores,sources,comments)
```
---
## Selecting elements from a list
Often, your list will be build out of numerous elements and components. Therfore, getting a single element, multiple elements, or a component out of it is not always straightforward.
One way to select a component, is using the numbered position of that component. For example, to "grab" the first component of `shining.list` you type `shining.list[[1]]`. (Remember, to select elements out of a data set you use square brackets `[ ]` ) A quick way to check this out is by typing it in the console.
Another way is referring to the names of the components. `shining.list[["reviews"]]` selects the `reviews` data frame. The same is true for the shorter version `shining.list$reviews`.
Besides selecting components, you often need to select specific elements out of these components. For example, with `shining.list[[2]][1]` you select from the second component actors (= `shining.list[[2]]` ) the first element ( `[1]` ). When you type this in the console, you will see the answer is Jack Nicholson.
*** =instructions
1. Select from the `shining.list` the last actor and assign to the `last.actor`.
2. Select from the `shining.list` all information regarding the second review.
*** =hint
- If you want to do things nicely: `length(shining.list$actors])` gives you the number of actors, and thus the element to select.
- You can select the information of the second review with `shining.list$reviews[2,]`.
*** =sample_code
```{r eval=FALSE}
# Create named list
shining.list <- list(moviename="The Shining",actors=actors, reviews=reviews )
# Select from the shining.list:
last.actor <-
second.review <-
# Show results:
last.actor
second.review
```
*** =solution
```{r eval=FALSE}
# Create named list
shining.list <- list(moviename="The Shining",actors=actors, reviews=reviews )
# Select from the shining.list:
last.actor <- shining.list$actors[ length(shining.list$actors) ]
second.review <- shining.list$reviews[2,]
# Show results:
last.actor
second.review
```
*** =sct
```{r eval=FALSE}
names <- c("shining.list","last.actor","second.review")
values <- c("list(moviename='The Shining',actors=actors, reviews=reviews )","shining.list$actors[ length(shining.list$actors) ]","shining.list$reviews[2,]")
DM.result <- closed_test(names,values)
```
*** =pre_exercise_code
```{r eval=FALSE}
actors <- c("Jack Nicholson","Shelley Duvall","Danny Lloyd","Scatman Crothers","Barry Nelson")
sources <- c("IMDb1","IMDb2","IMDB3")
comments <- c("Best Horror Film I've Ever Seen","A truly brilliant and scary film from Stanley Kubrick","A masterpiece of psychological horror")
scores <- c(4.5,4,5)
reviews <- data.frame(scores,sources,comments)
shining.list <- list(moviename="The Shining",actors=actors, reviews=reviews )
```
---
## Adding more movie information to the list
Being proud of your first list, you shared it with the members of your movie hobby club. However, one of the senior members (a guy named M. McDowell) noted you forgot to add the release year. Given your ambitions to become next years president of the club, you decide to add this info to the list.
To conviently add elements to lists, you use the concatenate function `c()`:
`c(list1,some.object)`
If you want to give the new list item a name, you just add this to the function: `c(list1,new.item.name=some.object)`.
*** =instructions
1. Complete the code below such that an item named `year` is added to the `shining.list` with the value 1980.
*** =hint
You're on your own now!
*** =sample_code
```{r eval=FALSE}
actors <- c("Jack Nicholson","Shelley Duvall","Danny Lloyd","Scatman Crothers","Barry Nelson")
sources <- c("IMDb1","IMDb2","IMDB3")
comments <- c("Best Horror Film I've Ever Seen","A truly brilliant and scary film from Stanley Kubrick","A masterpiece of psychological horror")
scores <- c(4.5,4,5)
reviews <- data.frame(scores,sources,comments)
# Create named list
shining.list <- list(moviename="The Shining",actors=actors, reviews=reviews )
# We forgot something: Add the year to shining.list ! Add your code below:
# Have a look at shining.list. Remember str()?
str(shining.list)
```
*** =solution
```{r eval=FALSE}
actors <- c("Jack Nicholson","Shelley Duvall","Danny Lloyd","Scatman Crothers","Barry Nelson")
sources <- c("IMDb1","IMDb2","IMDB3")
comments <- c("Best Horror Film I've Ever Seen","A truly brilliant and scary film from Stanley Kubrick","A masterpiece of psychological horror")
scores <- c(4.5,4,5)
reviews <- data.frame(scores,sources,comments)
# Create named list
shining.list <- list(moviename="The Shining",actors=actors, reviews=reviews )
# We forgot something: Add the year to shining.list !
shining.list <- c(shining.list,year=1980)
# Have a look at shining.list. Remember str()?
str(shining.list)
```
*** =sct
```{r eval=FALSE}
name <- "shining.list"
value <- "list(moviename='The Shining',actors=actors, reviews=reviews, year=1980 )"
DM.result <- closed_test(name,value)
```
*** =pre_exercise_code
```{r eval=FALSE}
```