---
title: "4 Fulton Fish Market with dplyr"
format:
  html:
    self-contained: TRUE
---

To review the dplyr skills we've learned, we'll complete a data project in this classwork and the next, replicating Kathryn Graddy’s (2006) paper *Markets: The Fulton Fish Market*. Graddy hand-collected the data by observing the Fulton Fish Market in New York for three months, getting a whiting fish seller to report to her negotiated prices.

A key factor influencing the fish market is weather conditions: wind and waves affect how much fish fishermen can catch. On stormy days, fewer fish are brought to market, leading to higher prices. On calm days, more fish are available, and prices drop.

Another interesting aspect is the difference in how Asian buyers and White buyers interact with the market. Asian buyers, who often resell fish in low-income neighborhoods or use it for products like fishballs, tend to have more elastic demand, meaning they are very sensitive to price changes and will bargain hard to get lower prices. In contrast, White buyers often work for high-end restaurants or suburban retailers, and can pass on higher costs to their customers. As a result, sellers sometimes charge White buyers higher prices for the same fish.

Here's your tool set for the classwork, along with how many times you should use them here:

- Functions to use on tibbles:
  - nrow: the number of rows in a tibble. Use 2 times.
  - ncol: the number of columns in a tibble. Use once.
  - names: the variable names in a tibble. Use once.
- dplyr functions to use on tibbles:
  - filter: pare down the rows of a tibble based on a logical condition. Use 0 times.
  - select: pare down the columns of a tibble by name. Use 0 times.
  - slice: select rows of a tibble by integer position. Use 4 times.
  - arrange: sort a tibble based on a variable: ascending by default; descending with the helper desc(). Use 4 times.
  - mutate: create a new variable that is a transformation of one or more existing variables. Use 2 times.
  - summarize: apply a summary function like min and median to a variable. Use 8 times.
  - group_by: add a grouping attribute. Use 3 times.
  - count: the same as group_by with summarize with n() to create groups and count group size. Use 2 times.
- functions to use on vectors in tibbles: 
  - max, min, median, mean, sum.
  - n(): gives the current group size.

Run this to attach the tidyverse to your current session and load the data set `fish` to your global environment:

```{r, message = F}
library(tidyverse)

fish <- read_csv("https://raw.githubusercontent.com/cobriant/teaching-datasets/refs/heads/main/fish.csv")
```

This is the first time you've worked in a .qmd file (quarto document) instead of an R Script. .qmd files let us mix text and *code chunks*. Just like in an R script, to run a line of code, you can put your cursor at the end of the line and run it in your console using Ctrl/Cmd + Return.

One annoying thing about .qmd files: when you run a line of code, it shows you the output *at the bottom of the code chunk* instead of in the console. You can change that behavior by:

`Tools > Global Options > R Markdown > Uncheck "Show output inline for all R Markdown documents"`

Answer all these questions using the data set `fish`.

```{r}
# 1) How many observations are there in this data set?



# 2) How many variables are in this data set?



# 3) What are the variable names in this data set?



# 4) Which transaction had the highest price? What were the wind speed and wave height that day?



# 5) Which transaction had the largest quantity sold?



# 6) What are the five lowest prices observed?



# 7) What are the five highest wave heights observed?



# 8) What is the minimum, maximum, and median price for whiting?



# 9) What is the minimum, maximum, and median quantity sold for whiting?



# 10) How many buyer races are represented in this data set?



# 11) How many transactions were made by White buyers versus Asian buyers?



# 12) What is the minimum, maximum, and median wind speed?



# 13) What is the minimum, maximum, and median wave height?



# 14) Create a variable for total revenue (price * quantity_sold). What is the average revenue per transaction?



# 15) What is the total revenue by buyer race?



# 16) Do Asian buyers pay less for whiting? If so, by how much?



# 17) How does the price of whiting change depending on the wind speed? Also include the number of times we observed each wind speed.



```

## Homework for Tuesday

Complete [Koans 7-9 on ggplot2](https://github.com/cobriant/tidyverse-koans-part3) individually. Submit your 3 html files to Canvas before class on Thursday.

