-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path06-base-r.Rmd
243 lines (146 loc) · 7.69 KB
/
06-base-r.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
# Base R
## R Syntax & Data Types
### Slides
```{r embed-xaringan-1, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#18", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #18)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#18)
### Video
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=S8zTmEvpYYk")
```
### Exercise 1
1. Do the following calculation in R: one plus five, divided by nine.
2. Assign the result of the calculation to a variable.
3. Test if the result of your calculation is larger than 1.
4. Round off the result to 1 decimal. Tip: use the Maths Functions section of the Base R cheat sheet!
### Solution
#### Slides {-}
```{r embed-xaringan-2, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#27", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #27)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#27)
#### Video {-}
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=V8za6GR7u8Q&feature=youtu.be")
```
## Vectors in R
### Slides
```{r embed-xaringan-3, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#28", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #28)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#28)
### Video
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=XMFjteCdHbQ")
```
### Exercise 2
Meet Ann, Bob, Chloe, and Dan.
1. Make a character vector with their names, using the function c(). Save the vector as "name".
2. How old are Ann, Bob, Chloe, and Dan? You decide! Design a numeric vector with their respective ages. Save it as "age".
3. What is their average age? Use a function in R to calculate this. Tip: use the Maths Functions section of the Base R cheat sheet!
### Solution
#### Slides {-}
```{r embed-xaringan-4, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#40", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #40)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#40)
#### Video {-}
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=rIieuoYT4z0")
```
## Data Structures
### Slides
```{r embed-xaringan-5, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#41", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #41)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#41)
### Video
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=Ffk2Kxa_b_M")
```
### Exercise 3
1. Create a vector country containing four countries (use at least one duplicate!).
2. Create a data frame combining name, age, and country, and save it as df.
3. Check your dataframe with the function summary(). Does it contain a factor?
4. Make sure your column country is a factor, and confirm this with summary().
5. Create a list with your vectors name and age, and save it as mylist.
### Solution
#### Slides {-}
```{r embed-xaringan-6, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#50", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #50)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#50)
#### Video {-}
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=A1GsHC6pIio")
```
## Missing Data
### Slides
```{r embed-xaringan-7, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#53", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #53)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#53)
### Video
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=4gVvlg1Itzs")
```
### Exercise 4
Predict the results before you run the code. Does the real answer make sense to you?
### Solution
#### Slides {-}
```{r embed-xaringan-8, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#63", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #63)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#63)
## Indexing Vectors & Lists
### Slides
```{r embed-xaringan-9, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#66", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #66)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#66)
### Video
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=e10nO2swYIE")
```
### Exercise 5
1. Return only the first number in your vector age.
2. Return the 2nd and 4th name in your vector name.
3. Return only ages under 30 from your vector age.
4. Return the name "Chloe" from your list mylist (see exercise 3).
### Solution
#### Slides {-}
```{r embed-xaringan-10, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#79", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #79)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#79)
#### Video {-}
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=4BZGGq-nBos")
```
## Indexing a Data Frame
### Slides
```{r embed-xaringan-11, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#80", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #80)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#80)
### Video
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=m15hbXG6I-Y")
```
### Exercise 6
1. From your dataframe df, return the entries for everyone living in a country of your choice.
2. Return only the names of everyone in your data frame df under 40.
(Hint: what information should you use for row indexing?
What information should you use for column indexing?)
3. Return the columns name and age together.
### Solution
#### Slides {-}
```{r embed-xaringan-12, echo=FALSE}
xaringanExtra::embed_xaringan(url = "https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#91", ratio = "4:3")
```
[Use this link to open slides in a new tab (refer to slide #91)](https://utrechtuniversity.github.io/workshop-introduction-to-R-and-data/slides/slides_introduction.html#91)
#### Video {-}
```{r echo=FALSE}
vembedr::embed_url("https://www.youtube.com/watch?v=DUk9bfcSwbA")
```