```{r setup}
library(tidyverse)
```
## Exercise 1. Converting vectors
What would be the converted vector? Write down your answers before running cells.
**Answer #1:**
```{r exercise 01-1}
character_vector <- c("TRUE", "123e0", "1.1", "12 and 3", "F", "5")
as.integer(character_vector)
```
**Answer #2:**
```{r exercise 01-2}
character_vector <- c("TRUE", "123e0", "1.1", "12 and 3", "F", "5")
as.double(character_vector)
```
**Answer #3:**
```{r exercise 01-3}
character_vector <- c("TRUE", "T", "TruE", "f", "F")
as.logical(character_vector)
```
**Answer #4:**
```{r exercise 01-4}
numeric_vector <- c(1.2, -0.5, 10e1)
as.integer(numeric_vector)
```
**Answer #5:**
```{r exercise 01-5}
logical_vector <- c(TRUE, F, T)
as.numeric(logical_vector)
```
**Answer #6:**
```{r exercise 01-6}
numeric_vector <- c(1.2, -0.5, 0)
as.logical(numeric_vector)
```
**Answer #7:**
```{r exercise 01-7}
numeric_vector <- c(1.2, -0.5, 0, 1e3, 1e10)
as.character(numeric_vector)
```
**Answer #8:**
```{r exercise 01-8}
logical_vector <- c(TRUE, F, T)
as.character(logical_vector)
```
## Exercise 2. Converting vectors to a common type
What would be the converted vector? Write down your answers before running cells.
**Answer #1:**
```{r exercise 02-1}
c(TRUE, 123, 1.1, F, 5)
```
**Answer #2:**
```{r exercise 02-2}
c(TRUE, 123, 1, F, 5)
```
**Answer #3:**
```{r exercise 02-3}
c(TRUE, 123, "1", F, 5)
```
**Answer #4:**
```{r exercise 02-4}
c(TRUE, 123, "1", NA, 5)
```
## Exercise 3. Simplifying and preserving subsetting
Run this cell first to create the list.
```{r exercise 03-0}
l <- list("a" = c(1L, 2L, 3L), "bb" = list(c(10L, 20L, 30L, 40L)), "cc" = list("c1"= 1:5, "c2"= 5L))
l
```
Write down your answers before running each cell. What would be extracted into variable `x`? Possible answers for `class(x)` are:
* a vector (`class(x)` returns `"integer"`)
* list (`class(x)` returns `"list"`)
* `NULL`
* `NA`
**Answer #1:**
```{r exercise 03-1}
x <- l[[1]]
print(x)
class(x)
```
**Answer #2:**
```{r exercise 03-2}
x <- l[[2]]
print(x)
class(x)
```
**Answer #3:**
```{r exercise 03-3}
x <- l[[2]][[1]]
print(x)
class(x)
```
**Answer #4:**
```{r exercise 03-4}
x <- l[[3]][1]
print(x)
class(x)
l[[3]][1]
```
**Answer #5:**
```{r exercise 03-5}
x <- l[[1]][[1]]
print(x)
class(x)
```
**Answer #6:**
```{r exercise 03-6}
x <- l[[3]][1][[1]]
print(x)
class(x)
```
**Answer #7:**
```{r exercise 03-7}
x <- l[["a"]][1][[1]]
print(x)
class(x)
```
**Answer #8:**
```{r exercise 03-8}
x <- l[["c"]]["c1"]
print(x)
class(x)
```
## Exercise 4. Creating and viewing a table
Create a table with several columns, run the cell and view it in the editor either by clicking on its name in the _Environment_ tab or by typing `View(name_of_your_table)` in the console.
```{r exercise 04}
```
## Exercise 5. Creating a table
What would be a table generated by this code? How many rows and which values in each column?
**Answer #1: **
```{r exercise 05-1}
data.frame(A = 1:9, B = TRUE, C = c("x", "y", "z"))
```
Which code first or second will generate an error?
**Answer #2: **
```{r exercise 05-2}
data.frame(A = 1:8, B = c(TRUE, FALSE), C = c("x", "y", "z"))
data.frame(A = 1:12, B = c(TRUE, FALSE), C = c("x", "y", "z"))
```
## Exercise 6. Subsetting a table
What would be result, will the code return a vector or a data frame?
**Answer #1:**
```{r exercise 06-1}
df <- data.frame(x = 1:6, y = c("A", "B"))
df$x
```
What would be result, will the code return a vector or a data frame?
**Answer #2:**
```{r exercise 06-2}
df <- data.frame(x = 1:6, y = c("A", "B"))
df[2, ]
```
What would be result, will the code return a vector or a data frame?
**Answer #3:**
```{r exercise 06-3}
df <- data.frame(x = 1:6, y = c("A", "B"))
df[, "y"]
```
Complete the code. You need to get second element of the first column. Do it two ways, first via `[]` notation, second using `$` notation.
```{r exercise 06-4}
df <- data.frame(x = 1:6, y = c("A", "B"))
# Your code for [] notation goes here
# Your code for $ notation goes here
```
Complete the code. You want to get 3rd till 5th rows in second and third columns.
```{r exercise 06-5}
df <- data.frame(x = 1:6, y = c("A", "B"), z = TRUE)
# Your code goes here
```
## Exercise 7. Tribble
Use `tribble()` function from `tibble` library to replicate the following table
| Participant | Block | Response |
| ----------- | ----- | -------- |
| A | 1 | Yes |
| B | 1 | No |
| A | 2 | No |
| B | 2 | No |
```{r exercise 07}
# Your code goes here
```
## Exercise 8. read_csv
Use `read_csv` function from `readr` library to read `face_rank.csv` file you downloaded, see instructions and a warning the main text. Specify the columns, so that the program runs without a warning (see main text). Experiment with modifying the column specifications to see how you get warned about mismatches in the data.
```{r exercise 08}
# Your code goes here
```
## Exercise 9. read_excel
Use `read_excel` function from `readxl` library to read `face_rank.xlsx` file you downloaded or use a direct URL. You can read the manual [here](https://readxl.tidyverse.org/reference/read_excel.html).
```{r exercise 09}
# Your code goes here
```
## Exercise 10, read.spss
Use `read.spss` function from `foreign` library to read `band_members.sav`.
```{r exercise 10}
# Your code goes here
```
## Exercise 11, save and read table with saveRDS and readRDS
Save table from the previous exercise via `saveRDS()` and read it into a _different_ variable via `readRDS()` (use `?saveRDS` for the manual). Visually check that two tables are identical
```{r exercise 11}
# Your code goes here
```