---
title: "In Class 3-5"
output: html_document
---

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

```{r}
data(mtcars)

mtcars <- mtcars %>% mutate(am = as.factor(am))


```

```{r}
#lin model for mpg ~ am
my_mod <- lm(mpg ~ am, data = mtcars)
get_regression_table(my_mod)
```

Mean mpg for am = 0 is 17.147.

Mean mpg for am = 1 is 17.147 + 7.245

```{r}
17.147 + 7.245
```

```{r}
mtcars %>% group_by(am) %>% summarize(mean = mean(mpg))
```

## Problem 4.d Midterm Review

Create a new categorical variable fuel in the mtcars dataframe that takes the value efficient if mpg is greater than or equal to 20, and inefficient if mpg is less than 20.

```{r}
mtcars <- mtcars %>% mutate(
  fuel = ifelse(mpg >= 20,
    "efficient",
    "inefficient"
  )
)
```

What proportion of cars are efficient?
```{r}
mtcars %>% group_by(fuel) %>% summarize(number = n()) %>% mutate(total = sum(number)) %>% mutate(proportion = number/total)

```


Difference between filter and group_by
```{r}
mtcars %>% filter(mpg >= 20)
```

```{r}
mtcars %>% group_by(am)
```

