---
title: 'Formative Exercise 06: MSc Data Skills Course'
author: "Psychology, University of Glasgow"
output: html_document
---

```{r setup, include=FALSE}
# please do not alter this code chunk
knitr::opts_chunk$set(echo = TRUE, message = FALSE, error  = TRUE)
library("tidyverse")
library("lubridate")
set.seed(1379) # makes sure random numbers are reproducible
```

Edit the code chunks below and knit the document. You can pipe your objects to `glimpse()` or `print()` to display them.

## Mutating joins

### Question 1A

Load the following data from the dataskills package (or access the linked CSV files online). Each participant is identified by a unique `user_id`.

* [`disgust_scores`](https://psyteachr.github.io/msc-data-skills/data/disgust_scores.csv)
* [`personality_scores`](https://psyteachr.github.io/msc-data-skills/data/personality_scores.csv)
* [`users`](https://psyteachr.github.io/msc-data-skills/data/users.csv)


```{r ex-1a}
data("disgust_scores", package = "dataskills")
data("personality_scores", package = "dataskills")
data("users", package = "dataskills")
```

### Question 1B

Add participant data to the disgust_scores table.

```{r ex-1b}
study1 <- left_join(disgust_scores, users, by = "user_id") %>%
  glimpse()
```


### Question 1C

Calculate the approximate age of each participant on the date they did the disgust_scores questionnaire and put this in a column called `age_years` in a new table called `study1_ages`. 

```{r ex-1-i}
study1_ages <- study1 %>%
  mutate(
    age_years = year(date) - birthyear
  ) %>%
  glimpse()
```

### Question 2A

Add the participant data to the disgust_scores data, but have the columns from the participant table first.

```{r ex-2}
study2 <- right_join(users, disgust_scores, by = "user_id") %>%
  glimpse()
```


### Question 2B

How many times was the disgust_scores questionnaire completed by each sex? Create a table called `study2_by_sex` that has two columns: `sex` and `n`.

```{r ex-2-i}
study2_by_sex <- study2 %>%
  count(sex) %>%
  print()
```


### Question 2C

*Advanced*: Make a graph of how many people completed the questionnaire each year.
    
```{r ex-2-a}
study2 %>%
  mutate(year = substr(date, 1, 4)) %>%
  count(year) %>%
  ggplot() +
  geom_col(aes(year, n, fill = year)) +
  labs(
    x = "Year",
    y = "Times Completed"
  ) +
  guides(fill = FALSE)
  
```
    

### Question 3A

Create a table with only disgust_scores and personality_scores data from the same `user_id` collected on the same `date`.

```{r ex-3}
study3 <- inner_join(disgust_scores, personality_scores, 
                     by = c("user_id", "date")) %>%
  glimpse()
```


### Question 3B

Join data from the same `user_id`, regardless of `date`. Does this give you the same data table as above?
    
```{r ex-3-i}
study3_nodate <- inner_join(disgust_scores, personality_scores, 
                            by = c("user_id")) %>%
  glimpse()
```


### Question 4

Create a table of the disgust_scores and personality_scores data with each `user_id:date` on a single row, containing _all_ of the data from both tables.

    
```{r ex-4}
study4 <- full_join(disgust_scores, personality_scores, 
                    by = c("user_id", "date")) %>%
  glimpse()
```

## Filtering joins

### Question 5

Create a table of just the data from the disgust_scores table for users who completed the personality_scores questionnaire that same day.
    
```{r ex-5}
study5 <- semi_join(disgust_scores, personality_scores, 
                    by = c("user_id", "date")) %>%
  glimpse()
```

    
### Question 6

Create a table of data from users who did not complete either the personality_scores questionnaire or the disgust_scores questionnaire. (_Hint: this will require two steps; use pipes._)

    
```{r ex-6}
study6 <- users %>%
  anti_join(personality_scores, by = "user_id") %>%
  anti_join(disgust_scores, by = "user_id") %>%
  glimpse()
```

## Binding and sets

### Question 7

Load new user data from [dataskills::users2](https://psyteachr.github.io/msc-data-skills/data/users2.csv). Bind them into a single table called `users_all`.

```{r ex-7}
data("users2", package = "dataskills")
users_all <- bind_rows(users, users2) %>%
  glimpse()
```


### Question 8

How many users are in both the first and second user table?
    
```{r ex-8}
both_n <- dplyr::intersect(users, users2) %>% nrow() %>% print()
```

### Question 9

How many unique users are there in total across the first and second user tables?

```{r ex-9}
unique_users <- dplyr::union(users, users2) %>% nrow() %>% print()
```

### Question 10

How many users are in the first, but not the second, user table?

```{r ex-10}
first_users <- dplyr::setdiff(users, users2) %>% nrow() %>% print()
```

### Question 11

How many users are in the second, but not the first, user table?

```{r ex-11}
second_users <- dplyr::setdiff(users2, users) %>% nrow() %>% print()
```

