```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Introduction

This is a basic introduction to some of the features of the `tidyverse`.

The tidyverse is a series of packages built on top of R, maintained by Hadley Wickham (and others), with a unified syntax which is good for working with data.

```{r}
library(tidyverse) #Load into memory, show list of packages
```

As you can see, it contains the following packages as "core Tidyverse":

1. ggplot2
    - Advanced plotting tools.
    - Solid defaults
2. tibble
    - data type that improves in many small ways over `data.frame`
3. tidyr
    - Basic data cleanring: unnest, pivot, missing values, NAs
4. readr
    - importing data: CSVs, TSVs, other flat files
5. purrr
    - toolkit for manipulating functions and their outputs
6. dplyr
    - Basic data manipulation. filter rows, select columns, make new variables, etc.
7. stringr
    - tools for working with strings
8. forcats
    - tools for working with factors
  
When you install the `tidyverse` package, it also installs these supplemental packages:

1. Packages for importing data:
    - DBI: for relational databases
    - haven: for SPSS, SAS, and Stata data
    - httr: for web APIs
    - readxl: for XLSX and XLS files
    - rvest: for web scraping
    - jsonlite: for reading JSON files
    - xml2: for reading XML files
2. Packages for data manipulation:
    - lubridate: for working with dates and times
    - hms: for time of day values
    - blob: for binary data
3. Packages for function work:
    - magrittr: for the pipe `%>%`
    - glue: for combining strings
4. As well as a few others.


# Identifying tidyverse in the wild
Most of the time, the identifying features of tidyverse code is the use of the pipe `%>%`, which prevents us from having to save each intermediate step of a calculation.

E.g.
```{r, message=F}
oj = read_csv("oj/oj.csv")
# Non tidyverse styling:
df1 = oj[oj$brand=="dominicks",1:6] #Pain to read, unclear
#Tidyverse style
df2 = oj %>% 
  filter(brand == "dominicks") %>% #filter for rows with a brand
  select(store,brand,week,logmove,feat,price)  #choose some variables
# Easy to read, very clear, step by step.
identical(df1,df2) #Same results.
```

Tidyverse makes it easy to make code that is interpretable, clear, and straightforward. This is *critical* for re-using code, as well as for working on code in groups.

# Using tidyverse
The simplest way to get a grasp on tidyverse is to explore the *excellent* documentation online. There are great cheatsheets, and there are also just great websites. See [tidyverse homepage](https://www.tidyverse.org/packages/) and click on a package for more. 

# Each package -- in a touch more depth

## tibble
This package changes data frames. From a user-facing perspective, it mostly changes how they print, producing a useful summary of columns, and not printing 10k rows.

```{r}
df2 #tibble
```

```{r, eval=F}
as.data.frame(df2) #df print -- I didn't run because it prints 10k rows. You're welcome to test it though.
```

There are also performance changes under the hood, but we can ignore those for now

## readr

`readr` makes it easy to read csv type files into tibbles.
```{r}
#oldschool
oj = read.csv("oj/oj.csv") #output is a data.frame
# tidyverse
oj = read_csv("oj/oj.csv") #output is a tibble
```
This is minor. I usually use `read.csv` and `as_tibble` separately.

## tidyr

Makes cleaning the data fairly easy. Do you want each brand to be its own column, with the sales at each store for that brand in a week to be a row? We can do that. Examples may come in the future. 

## dplyr

Makes manipulating data easy. Want to select only a few variables? or filter the rows by some constraint? or add variables? It is easy. 

```{r}
#Look only at dominicks sold at store 2. 
#Select only 4 variables, make two more from those.
oj %>% 
  filter(brand=="dominicks") %>%
  filter(store == "2") %>%
  select(logmove,week,price,feat) %>%
  mutate(sales = exp(logmove),logprice = log(price))
```

This is what the tidyverse is known for. Very straightforward data manipulation. You will see me use `%>% filter()` and `%>% mutate()` a lot. The dplyr [overview page](https://dplyr.tidyverse.org) is very helpful.

## purrr

Functions for improving how your functions work. This is a functional programming toolkit and it is very valuable -- but not really a beginner toolkit. 

## ggplot2

This is a tool for making great plots with ease. Want to plot price against sales, color by brand, and show ads and no-ads in two side-by-side plots with the axes matched? Come and see:
```{r}
# Make a plot using OJ data. X-axis is log(price), y is sales, color by brand.
ggplot(oj,aes(x=log(price),y=logmove,col=brand)) + 
  #Make it a scatter plot, and make each point fairly transparent
  geom_point(alpha=0.2) +
  #And separate it into two plots based on ad-presence
  facet_grid(cols = vars(feat))
```

This is a power tool for making nice plots, which I will use constantly. The [reference page](https://ggplot2.tidyverse.org/reference/index.html) is almost always open on my computer, and each function on that page has a bunch of great examples at the bottom of its own page. 


## stringr and forcats
These packages provide nice tools for working with data that are factors (an anagram of forcats) and for data that are strings. 

I won't go through examples right now. 

