
---
title: "Lec 13: Pre-class Exercise: Single Table Analysis"
output:
  html_document
---

```{r, include=FALSE}
# Do not edit this code chunk
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE)
```

# Exercise 1

Follow the code in our pre-assigned reading ModernDive 3.1-3.6, 3.8.1

```{r}
library(tidyverse)
library(nycflights13)
```


# Exercise 2

The following exercise is the same as Lec 13 lecture slides P5-19

Hospital Cost Report, 2018

Run the code below to load the dataset we will be working with today. 

```{r}
library(tidyverse)
hospitals <- read.csv("data/Hospital_Cost_Report_2018.csv")
```

```{r}
view(hospitals)
```

# `select()`

## `select()` enables us to select variables (columns) of interest.

```{r}
hospitals |>
  select(Provider.CCN, State.Code, Number.of.Beds) |>
  head()
```

## `:` enables us to select consecutive variables (columns) of interest.

```{r}
hospitals |>
  select(Provider.CCN, State.Code:County) |>
  head()
```

## `!` or `-` enables us to negate selection of certain variables (columns). `last_col()` serves as a reference to the last column.

```{r}
hospitals |>
  select(!(Hospital.Name:last_col()))|>
  head()
```

```{r}
hospitals |>
  select(-(Hospital.Name:last_col()))|>
  head()
```

## `starts_with()` is a helper function that enables us to select columns by matching string patterns at the start of their names.

```{r}
hospitals |>
  select(Provider.CCN, starts_with("Fiscal")) |>
  head()
```

## `ends_with()` is a helper function that enables us to select columns by matching string patterns at the end of their names.

```{r}
hospitals |>
  select(Provider.CCN, ends_with("Improvements")) |>
  head()
```

# `filter()`

## `filter()` subsets observations (rows) according to a certain criteria that we provide. `==` checks for equivalence and != checks for inequivalence.

```{r}
hospitals |>
  filter(City == "SPRINGFIELD") |>
  select(Provider.CCN, City, State.Code)
```

## `&` returns rows that meet both criteria.

```{r}
hospitals |>
  filter(City == "SPRINGFIELD" & State.Code == "MA") |>
  select(Provider.CCN, City, State.Code)
```

## `|` returns rows that meet either criteria.

```{r}
hospitals |>
  filter(City == "SPRINGFIELD" & (State.Code == "MA" | State.Code == "VT")) |>
  select(Provider.CCN, City, State.Code)
```

## `>`, `<`, `>=`, `<=` checks whether values are greater/less/equal to than other values.

```{r}
hospitals |>
  filter(Number.of.Beds > 1500) |>
  select(Provider.CCN, City, State.Code, Number.of.Beds)
```

## `%in%` checks for values that match at least one value in a vector of values.

```{r}
hospitals |>
  filter(CCN.Facility.Type %in% c("STH", "LTCH", "CAH")) |>
  select(Hospital.Name, City, State.Code) |> 
  head()
```

## `is.na()` checks for NA values and `!is.na()` checks for non-NA values.

```{r}
hospitals |>
  filter(is.na(Number.of.Beds)) |>
  nrow()
```

## `str_detect()` checks for values with strings that match a pattern we provide.

```{r}
hospitals |>
  filter(str_detect(Hospital.Name, "SHRINERS")) |>
  select(Hospital.Name, City, State.Code) |> 
  head()
```

# `arrange()`

## `arrange()` sorts rows according to values in a column

```{r}
hospitals |>
  filter(City == "SPRINGFIELD" & State.Code == "MA") |>
  arrange(Number.of.Beds) |>
  select(Hospital.Name, Number.of.Beds)
```

## `desc()` sorts from largest to smallest (numeric) or last character to first character (character)

```{r}
hospitals |>
  filter(City == "SPRINGFIELD" & State.Code == "MA") |>
  arrange(desc(Number.of.Beds)) |>
  select(Hospital.Name, Number.of.Beds)
```
