-
Notifications
You must be signed in to change notification settings - Fork 7
/
chapter2.Rmd
435 lines (302 loc) · 13 KB
/
chapter2.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
---
title: 'Data types and variable types'
description: 'Quick overview to R data and variable types. What are the objects? You are the subject.'
---
## Data frames
```yaml
type: NormalExercise
key: 8718babe2e
lang: r
xp: 100
skills: 1
```
In R, the basic idea is to use *functions* to operate on objects. The most common object to operate on is the `data frame` object, which stores data in tabular form.
The columns in data frames are vectors. Data frames can contain multiple vectors with different data types. In this chapter you will learn more about vectors and data types.
Here you will get to apply some functions that are helpful for inspecting data: `head()` and `str()`. You will also learn how to access the columns of a data frame using the $-sign.
`@instructions`
- Execute the function `head()` and see the results
- Inspect the data types in the `students2014` data with the function `str()`
- Use the $-sign to access the students shoe size (`kenka`) in `students2014` data
- Note that `students2014$ika` prints out all the ages of the students in `students2014`
`@hint`
- The data is stored in object `students2014`
`@pre_exercise_code`
```{r}
students2014 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS-data.txt", sep="\t", header=TRUE)
# keep a couple background variables
students2014 <- students2014[,c("sukup","toita","ika","pituus","kenka","kone")]
# recode kone -variable NA values as factor levels
students2014$kone <- addNA(students2014$kone)
# choose rows without missing values
students2014 <- students2014[complete.cases(students2014),]
# integers to numeric
students2014$ika <- as.numeric(students2014$ika)
students2014$pituus <- as.numeric(students2014$pituus)
students2014$kenka <- as.numeric(students2014$kenka)
```
`@sample_code`
```{r}
# students2014 is available
# Let's view the first 6 students from the course participants data
head(students2014)
# View the structure of students2014
str(students2014)
# You can access the variables in the students2014 data by using the $-sign
students2014$ika
# Access the shoe sizes of the students
```
`@solution`
```{r}
# students2014 is available
# Let's view the first 6 students from the course participants data
head(students2014)
# View the structure of students2014
str(students2014)
# You can access the variables in the students2014 data by using the $-sign
students2014$ika
# Access the shoe sizes of the students
students2014$kenka
```
`@sct`
```{r}
test_output_contains("students2014$kenka", incorrect_msg = "Did you use the $-sign to access the shoe size variable?")
# test if the students code produces an error
test_error()
# Final message the student will see upon completing the exercise
success_msg("Good work!")
```
---
## Data types (1)
```yaml
type: NormalExercise
key: e4360dcfec
lang: r
xp: 100
skills: 1
```
In this section you learn about the data types in R. You will also get familiar with vectors and their usage. What does data types and vectors have to do with each other? A lot, you'll see!
There are different data types in R. The most commonly used are:
- Numbers and decimal values are called **numeric**
- Text or character values are called **characters**
- [Boolean values](https://en.wikipedia.org/wiki/Boolean_data_type) (`TRUE` or `FALSE`) are called **logical**
When creating character values, one needs to use quotation marks (both `' '` and `" "` will work). For example `"Statistics<3"` would be a character value in R. Numeric and boolean values do not need quotation marks.
You can use the function `str()` (str is short for structure) to find out what data type the object is.
`@instructions`
- Create character object `my_character` by assigning your initials in it.
- Assign the number 10.5 to `my_numeric`. Note that the decimal separator in R is dot instead of comma.
- Create the object `my_logical` by executing the line. Note that with boolean values, no quotation marks are needed.
- Change the value of `my_logical` to `TRUE`
- Inspect the data types of the objects with `str()`.
`@hint`
- When creating a character object, remember to use quotation marks.
- Dot is the decimal mark in R: `2.5` is decimal number but `2,5` means a different thing!
- Remember that `Ctrl + Enter` executes a single row.
- You can use `str()` by putting the object name inside the parenthesis.
`@pre_exercise_code`
```{r}
```
`@sample_code`
```{r}
# Create three objects with different data types
my_character <-
my_numeric <-
my_logical <- FALSE
# See how each object looks with the function str()
```
`@solution`
```{r}
# Create three objects with different data types
my_character <- "A.B.C."
my_numeric <- 10.5
my_logical <- TRUE
# See how each object looks with the function str()
str(my_character)
str(my_numeric)
str(my_logical)
```
`@sct`
```{r}
test_object("my_character", eval=FALSE, incorrect_msg = "Did you assign your initials to `my_character`?")
test_object("my_numeric", incorrect_msg = "Did you assign the correct value to `my_numeric`?")
test_object("my_logical", incorrect_msg = "Did you change the value of `my_logical`?")
test_output_contains("str(my_character)", incorrect_msg = "Did you use `str()` on `my_character`?")
test_output_contains("str(my_numeric)", incorrect_msg = "Did you use `str()` on `my_numeric`?")
test_output_contains("str(my_logical)", incorrect_msg = "Did you use `str()` on `my_logical`?")
# test if the students code produces an error
test_error()
# Final message the student will see upon completing the exercise
success_msg("You're making great progress, well done!")
```
---
## Data types (2)
```yaml
type: NormalExercise
key: 9ee1ba578e
lang: r
xp: 100
skills: 1
```
One of R's data types is called factor. Factors contain numbers, but for every number there is a character value associated to it. A variable measured by the [Likert scale](https://en.wikipedia.org/wiki/Likert_scale) could for example be coded as a factor: the values are numbers from 1 to 5, but the numbers also have words attached to them such as:
- "Strongly disagree" = 1
- "Strongly agree" = 5
In R is is also possible to create factors from numerical objects using the function `cut()`, which splits a variable into categories. It has a mandatory argument `breaks`, which defines the cutting points of the categories.
`@instructions`
- Create objects `student_age` and `student_age_cut`
- use `str()` to inspect `student_age_cut`
- Use `head()` to print the first six values of `student_age`
- Use `head()` to print the first six values of `student_age_cut`
`@hint`
- Give the object `student_age_cut` as the first argument to `str()` and `head()`.
`@pre_exercise_code`
```{r}
students2014 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS-data.txt", sep="\t", header=TRUE)
# keep a couple background variables
students2014 <- students2014[,c("sukup","toita","ika","pituus","kenka","kone")]
# recode kone -variable NA values as factor levels
students2014$kone <- addNA(students2014$kone)
# choose rows without missing values
students2014 <- students2014[complete.cases(students2014),]
# integers to numeric
students2014$ika <- as.numeric(students2014$ika)
students2014$pituus <- as.numeric(students2014$pituus)
students2014$kenka <- as.numeric(students2014$kenka)
```
`@sample_code`
```{r}
# students2014 is available
# Let's access the (numerical) age vector of the students2014 and save it to another object.
student_age <- students2014$ika
# Let's use the cut()-function to cut the variable in categories
student_age_cut <- cut(student_age, breaks = c(15, 25, 35, 100))
# Inspect the data type of student_age_cut
# Print the first 6 elements of student_age and student_age_cut
```
`@solution`
```{r}
# students2014 is available
# Let's access the (numerical) age vector of the students2014 and save it to another object.
student_age <- students2014$ika
# Let's use the cut()-function to cut the variable in categories
student_age_cut <- cut(student_age, breaks = c(15, 25, 35, 100))
# Inspect the data type of student_age_cut
str(student_age_cut)
# Print the first 6 elements of student_age and student_age_cut
head(student_age)
head(student_age_cut)
```
`@sct`
```{r}
test_function("str", args="object", incorrect_msg = "Did you use the `str()` to look at the object 'student_age_cut'?")
test_output_contains("head(student_age)", incorrect_msg = "Did you use `head()` and look at the first six elements of `student_age`?")
test_output_contains("head(student_age_cut)", incorrect_msg = "Did you use `head()` to look at the first six elements of `student_age_cut`?")
# test if the students code produces an error
test_error()
# Final message the student will see upon completing the exercise
success_msg("Good work!")
```
---
## Vectors
```yaml
type: NormalExercise
key: 7cc56d2158
lang: r
xp: 100
skills: 1
```
A vector is an object containing multiple elements of the same data type. Vectors are quite important in R. The columns of a data frame (such as `student2014`) are vectors.
You can create vectors by combining values of the same data type, using the function `c()`.
In this exercise you will learn how to create your own vector.
`@instructions`
- See and execute the examples on how to create a vector with `c()`.
- Create a character vector `character_vector`.
- Create a vector `logical_vector` with boolean values `TRUE, FALSE, TRUE, FALSE`.
- Print the contents of `logical_vector`.
`@hint`
- When creating boolean values, no quotation marks are needed.
- You can print the contents of an object by typing it's name.
`@pre_exercise_code`
```{r}
```
`@sample_code`
```{r}
# Here we create a vector with numeric elements
c(2, 3, 4, 5)
# Another one
c(0.1, 0.2, 5.84, 0.7)
# Let's create one with characters and save it to a object
character_vector <- c("a", "b", "c", "d")
# Create a logical vector and print it
```
`@solution`
```{r}
# Here we create a vector with numeric elements
c(2, 3, 4, 5)
# Another one
c(0.1, 0.2, 5.84, 0.7)
# Let's create one with characters and save it to a object
character_vector <- c("a", "b", "c", "d")
# Create a logical vector and print it
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)
logical_vector
```
`@sct`
```{r}
test_object("logical_vector", incorrect_msg = "Did you create `logical_vector` with same values as instructed?")
test_output_contains("logical_vector", incorrect_msg = "Did you print the contents of `logical_vector`?")
test_error()
# Final message the student will see upon completing the exercise
success_msg("Great work!")
```
---
## Data types and measurement scales
```yaml
type: MultipleChoiceExercise
key: 3c0486635f
lang: r
xp: 50
skills: 1
```
Data types and measurement scales are somewhat related. When asking simple "Yes / No " -questions logical values are useful but you would not use them for measuring heights. Here is a table for the data types and measuring scales:
| Scale | Numeric | Character | Logical | Factor |
|----------------|---------|-----------|---------|--------|
| Categorical | x | x | x | x |
| Ordinal | x | x | | x |
| Measurement | x | | | |
Use the R Console to inspect the `students2014` data with the function `str()` and select the correct answer from below. The data is already loaded for you. Remember that in the console `Enter` executes lines.
Choose one.
`@possible_answers`
- Variable `kone` is from ordinal scale.
- Variables `ika` and `pituus` are character values.
- You could use the logical data type for variables `ika` and `pituus`.
- Variable `kone` is from categorical scale.
- It is meaningful to use factors for variables from measurement scale.
`@hint`
- There is no natural order in categorical scale.
- If you can put the values in a meaningful order, then it's ordinal scale.
- Using factors probably loses its purpose if the variable can have a lot of different values.
- Character values have quotations marks. `chr` is short for character.
`@pre_exercise_code`
```{r}
students2014 <- read.table("http://www.helsinki.fi/~kvehkala/JYTmooc/JYTOPKYS-data.txt", sep="\t", header=TRUE)
# keep a couple background variables
students2014 <- students2014[,c("sukup","toita","ika","pituus","kenka","kone")]
# recode kone -variable NA values as factor levels
students2014$kone <- addNA(students2014$kone)
# choose rows without missing values
students2014 <- students2014[complete.cases(students2014),]
# integers to numeric
students2014$ika <- as.numeric(students2014$ika)
students2014$pituus <- as.numeric(students2014$pituus)
students2014$kenka <- as.numeric(students2014$kenka)
```
`@sct`
```{r}
msg1 = "Sorry, I know you feel like Mac is better than Windows put in this case there is no order. Try again!"
msg2 = "I would use numbers to measure height and age. Try again!"
msg3 = "Maybe you can do that but it makes no sense. Guess again!"
msg4 = "Well done. Proceed to the next exercise"
msg5 = "Well technically you CAN do that, but you'll have as many factors as there are different numbers. Why not just use numerical values? Guess again!"
test_mc(correct = 4, feedback_msgs = c(msg1,msg2,msg3,msg4,msg5))
# Final message the student will see upon completing the exercise
success_msg("Good work!")
```