```{r setup, include=FALSE}

library(tidyverse)
library(conflicted)
library(viridis)
library(broom)

filter <- dplyr::filter

knitr::opts_chunk$set(echo = TRUE)
```

# Week 7 Practice: Combine all your skills

## Pick a dataset

Pick a dataset to download from the website <https://fels-bioinformatics.github.io/fels_bioinformatics_meetup/> to work with (or if there's another data set you'd like to use go for it).

### Provided datasets

##### **sparrows.csv**

Briefly, in 1898, Hermon Bumpus, an American biologist working at Brown
University, collected data on one of the first examples of natural selection directly
observed in nature. Immediately following a bad winter storm, he collected 136 English
house sparrows, Passer domesticus, and brought them indoors. Of these birds, 64 had
died during the storm, but 72 recovered and survived. By comparing measurements of
physical traits, Bumpus claimed to detect substantial physical differences between the
dead and living birds. The tidy sparrows dataset contains the following columns:

- **Sex** = sex of the bird
- **Age** = whether the bird was adult or young
- **Survival** = whether the bird survived
- **Length** = body length of the bird (cm)
- **Wingspread** = length of the bird's wings from wingtip to wingtip (cm)
- **Weight** = weight of the bird (g)
- **Skull_Length** = length of the bird's skull (cm)
- **Humerus_Length** = length of the bird's long arm bone (cm)
- **Femur_Length** = length of the bird's long leg bone (cm)
- **Tarsus_Length** = length of the bird's ankle bones (cm)
- **Sternum_Length** = length of the bird's breastbone (cm)
- **Skull_Width** = width of the bird's skull (cm)

<br>

##### **wine.tsv**

The wine dataset contains the results of a chemical analysis of wines grown in a specific area of Italy. Three types of wine are represented in the 178 samples, with the results of 13 chemical analyses recorded for each sample. The Type variable has been transformed into a categoric variable. The tidy wine dataset contains the following columns:

- **Cultivar** = the number factor indicating the grape cultivar the wine was made from
- **Alcohol** = the alcohol concentration in the wine sample (g/L)
- **MalicAcid** = the malic acid concentration in the wine sample (g/L)
- **Ash** = the ash concentration in the wine sample (g/L)
- **Magnesium** = the magnesium concentration in the wine sample (g/L)
- **TotalPhenol** = the total amount of all phenol compounds in the wine sample (g/L)
- **Flavanoids** = the concentration of all flavanoids in the wine sample (g/L)
- **NonflavPhenols** = the concentration of all non-flavanoid phenols in the wine sample (g/L)
- **Color** = wine color (spectrophotometric measure?)

<br>

##### **rowan.csv**

This dataset is from a field experiment studying the diversity of Chinese Rowan, or Mountain Ash, trees from the genus Sorbus. Researchers randomly sampled and recorded characteristics of leaves from three different Rowan species, and they further noted whether birds were actively nesting in each tree (recorded as y/n for yes/no). Altitude is recorded in meters (m), respiration rate (resp.rate) is recorded in per unit leaf mass, and leaf length (leaf.len) is recorded in centimeters (cm). The tidy rowan dataset contains the following columns:

- **altitude** = the alitude the rowan was found at (m)
- **resp.rate** = the rowan's respiration rate (nmol/s)
- **species** = the rowan species
- **leaf.len** = the rowan's leaf length (cm)
- **nesting** = logial, was there a bird nesting in the rowan, yes or no

<br>

## Wrangle

### Import

Read in your dataset of choice (either from the list above or your own dataset) in the chunk below!

```{r}
### sparrows.csv
read_csv('sparrows2.csv') -> sparrows

# different parameters
read_csv('sparrows2.csv', skip = 1, col_names = FALSE)

# difference between read_csv() and read.csv()
# mostly underscore functions don't coerce to factors and are faster
read_csv('sparrows2.csv')

read.csv('sparrows2.csv')

# have to specify delimiter
read_delim('sparrows2.csv')
```

```{r}
### wine.tsv
read_tsv('wine2.tsv') -> wine
```

```{r}
### rowan.csv
read_csv('rowan2.csv') -> rowan
```

<br>

### Tidy

As ever, first thing is to look at your data. Use the chunk below.

```{r}
# sparrows
sparrows %>% separate(skull_width_length, 
                      into = c('skull_width', 'skull_length'), 
                      sep = ';') -> sparrows_tidy
```

```{r}
### wine
# look at what's in the phenol/flav column
wine %>% group_by(`phenol/flav`) %>% count()


# tidy
wine %>% spread(`phenol/flav`, value) -> wine_tidy
```

```{r}
### rowan
# separate
rowan %>% separate(1, into = c('altitude',	'resp.rate',	'nesting',
                               'microphylla',	'oligodonta',	'sargentiana'),
                   sep = '\t')

# or realize it's tab delimited and re-read in as a tsv
read_tsv('rowan2.csv')

# continue tidying
rowan %>% separate(1, into = c('altitude',	'resp.rate',	'nesting',
                               'microphylla',	'oligodonta',	'sargentiana'),
                   sep = '\t',
                   convert = TRUE) %>%
  gather(species, leaf_length, microphylla:sargentiana) %>%
  na.omit() -> rowan_tidy
```

<br>

Do you see any odd features that need to be tidied before continuing? If yes, tidy the table in the chunk below. Don't forget to save your tidied table to another variable/object before continuing.

```{r}

```

<br>

## Understand the Data

### Ask a Question

Looking at your dataset, what questions come to mind? For example, in everyone's favorite dataset iris, you might ask if petal width is different between the three iris species. Look at your dataset and come up with a question and write it down below.

**Write your question here:** Is there a difference in alcohol content between the different cultivars in the wine dataset?

<br>

### Visualize

Think about your question. How can you visually represent the relevant data columns? Plot your data in the chunk below.

```{r}
ggplot(wine_tidy, aes(x = as.factor(Cultivar), y = Alcohol)) + 
  geom_boxplot()
```

<br>

### Test

Use the appropriate hypothesis test (ex: `t.test()` or `chisq.test()`) to test your question.

```{r}
pairwise.t.test(wine_tidy$Alcohol, wine_tidy$Cultivar) %>% tidy()
```

<br>

### Ask a Question

Ask another question about your data!

**Write your question here:** 

<br>

### Visualize

Make another figure in the chunk below visualizing the variables you asked your second question about.

```{r}

```

<br>

### Test

And again, use an appropriate hypothesis test to test your idea.

```{r}

```

<br>

## Communicate

Write a few sentences here that explain what you tested and why.

**Erase me and put sentences here**

<br><br>
