---
title: "Week 3: Data Wrangling"
author: "PSYC 859 Data Management and Visualization"
date: "January 29, 2026"
format:
  html:
    toc: true
    toc-depth: 3
    toc-location: left
    code-fold: show
execute:
  echo: true
  message: false
  error: false
---

```{r}
library(tidyverse)

data("economics_long")
```

This assignment is optional and intended for practice. If you choose to submit it, you can
email it to the instructor.

## Instructions

Follow along with the tutorial and, for each question, write the code you would use
(or copy it from the RStudio syntax window). Additionally, paste any reasonable output
generated by your code. If there is a lot of output, paste just enough to demonstrate
that you reached the correct answer.

## Dataset for assignment

Use the `economics_long` dataframe from the `ggplot2` package for this assignment.
You can load it into your working environment with `data("economics_long")` after
you load the `tidyverse` package.

## Tidy the data

1) Format the data so it is considered tidy.
   **Hint: You will need to delete `value01`.**

2) Create a new dataframe that summarizes the data by year. Include at the very least
   `mean` and `sd` for each variable.

3) Filter your summary dataframe so that only data between the years of 1975 and 2015
   is included. You may have to make `year` numeric by using `mutate()`.
   **Hint: check `?dplyr::between`.**

## Joins

4) Use the provided housing dataset at
   `files/national-month.csv.gz`
   and tidy it if necessary. Summarize the data by year.

5) Perform a high-fidelity join between the housing dataset and the tidy economics dataset
   by year. How do you know the join was successful? Include a brief check (e.g., row counts,
   `anti_join()`, or `count()` of unmatched years).

## Core dplyr practice

6) Using the tidy economics dataset:

- **select** only `date`, `variable`, and `value`, then **rename** `value` to `series_value`.
- **arrange** the data by `variable`, then descending `series_value`.
- Use **slice_max** (or **slice_min**) to return the top 5 (or bottom 5) rows per variable.

7) Use **case_when** to create a categorical column that bins `series_value` into
   at least three levels (e.g., "low", "medium", "high"). Then use **count** to report
   how many observations fall in each category by `variable`.

8) Use **across** inside **summarize** to compute the mean and standard deviation of
   `series_value` for each `variable` after filtering to the years 1990 to 2015.

## Data exploration and processing

9) What would constitute invalid or outlier observations on the basis of rational criteria?
   Search the dataframe for any values that meet your outlier/invalid criteria by either
   writing a custom function or chaining together several tidyverse functions.
