---
title: "Math 141 Survey Wrangling"
author: "Nate Wells"
date: "2/8/2022"
output: html_document
---

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

survey <- read_csv("survey141.csv")
```


1. Sort students by Hogwarts House, and within house, by year.

```{r}
survey %>% 
  arrange(hogwarts, year) %>% 
  select(hogwarts, year, everything())
```

2. Find the number of students in each year who think hot dogs are sandwiches.

```{r}
survey %>% 
  group_by(year, hot_dog) %>% 
  summarize(n = n()) %>% 
  filter(hot_dog == "Yes")
```


3. Calculate the median number of college applications submitted by students of Herodotus.

```{r}
survey %>% 
  filter(historian == "Herodotus") %>% 
  summarize(median_apps = median(college_app))
```

4. Create a data set consisting only of categorical variables (ordered alphabetically), and with student responses ordered alphabetically, starting with the first variable.

```{r}
survey %>% 
  select(dog_pants, historian,  hogwarts,  hot_dog, year) %>% 
  arrange(dog_pants, historian,  hogwarts,  hot_dog, year)
```

5. Identify students whose social and economic views differ by 2 or more points.

```{r}
survey %>% 
  mutate(diff = abs(social - economic)) %>% 
  select(social, economic, diff) %>% 
  filter(diff > 2)
```

6. Create a data set consisting of two variables: Hogwarts House and Political Views, where the Political Views score is obtained by averaging a student's Social and Economic views scores.

```{r}
survey %>% 
  mutate(poli_view = (social+economic)/2) %>% 
  select(hogwarts, poli_view)
```

7. Count how many students think both that dogs should wear pants on their back legs and that hot dogs are sandwiches.

```{r}
survey %>% 
  filter(hot_dog=="Yes") %>% 
  filter(dog_pants == "Back legs") %>% 
  summarize(how_many = n())
```

8. Among students who drink who are not freshmen, create a data set that could be used make a scatterplot of alcohol use vs. social views.

```{r}
survey %>% 
  filter(alcohol > 0) %>% 
  filter(year != "Freshman") %>% 
  select(alcohol, social) %>% 
  ggplot(aes(x = alcohol, y = social))+
  geom_jitter()
```

