---
title: "PA 5: Identify the Mystery College"
format: html
embed-resources: true
code-tools: true
toc: true
editor: source
execute: 
  echo: true
  eval: true
  message: false
  warning: false
---

**Names**


Today you will use the `dplyr` package to clean some data. We will then use that
cleaned data to figure out what college Monte has been accepted to.

***This task is complex. It requires many different types of abilities. Everyone will be good at some of these abilities but nobody will be good at all of them. In order to solve this puzzle, you will need to use the skills of each member of your group.***

## Groupwork Protocols

During the Practice Activity, you and your partner will alternate between two roles---Computer and Coder.

<!-- The partner whose birthday is closest to January 1st starts as the Computer (typing and listening to instructions from the Coder).  -->

**Starting Coder:**  

**Starting Computer:**  

## Goals for the Activity

- Solve issues in your data as you read it in specifying NA values and cleaning up variable names
- Apply data verbs from `dplyr` to extract specific information from a large data set.  

**THROUGHOUT** the Activity be sure to follow the Style Guide by doing the following:

-   load the appropriate packages at the beginning of the Quarto doc\
-   use proper spacing\
-   name all code chunks\
-   comment at least once in each code chunk to describe why you made your coding decisions\
-   add appropriate labels to all graphic axes

# Part One: Data Import and Cleaning

This section will clean today's data so that you can use it more easily in Part
Two.

## Data Download & Package Loading

First, we declare our package dependencies and load the data.

*The data loading function `read_csv()` will give you an outpouring of helpful information about the dataset. If you do not see the word "error", there is nothing to be concerned about.*

```{r}
#| label: setup
#| include: 
## update the include arguement to hide all output, but still run the setup chunk

```

```{r}
#| label: add-label
#add comment
colleges <- read_csv("https://raw.githubusercontent.com/statistical-computing-r/spring-2026/refs/heads/main/group-activities/week-6/data/colleges.csv")
```

Take a look at the variables in your downloaded data by running the following
code. This tells reports the data type for each column in the dataset.

```{r}
#| label: glimpse-data
#set this to code chunk to not evaluate in the future
glimpse(colleges)
```

## Data Cleaning

Now we will clean the data. Alas, each of the R chunks in this section will
cause an error and / or do the desired task incorrectly. Even the chunks that
run without error **are not** correct! You will need to find the mistake and
correct it to complete the intended action.

**Step 1:** Instead of a blank, they have "NULL" as the missing value. Add an argument to the data import code above to specify the missing values as "NULL".  

**Step 2** Clean the variable names to be `snake_case` (Hint: use `janitor`) by adding an additional piped line in the data import code chunk.   

**Step 3** Write the cleaned data set to a subfolder called `data` (you will need to make this in your project first.)  


<!-- Swap roles -- Computer becomes Coder, Coder becomes Computer! -->


# Practice with `dplyr` Verbs

In each of the following actions there are errors in the code - fix the errors to run the code.  


## Choosing variables with `select`  

There are too many variables in this data set. We don't need all of
them. Narrow your data set down to only:

-   `instm` name of the institution
-   `city` city, `stabbr` state, and `zip` ZIP code of the institution
-   `adm_rate` admissions rate
-   `sat_avg` average SAT score
-   `ugds` number of undergraduate students
-   `tuitionfee_in` in- and `tuitionfee_out` out-of-state tuition
-   `control` Whether the school is public or private
-   `region`` region of the school.

```{r}
#| label: add-label-in-kebab-case

#add comment
colleges_clean1 <- colleges | >
  select(instnm, 
         city, 
         stabbr, 
         zip,
         adm_rate, 
         sat_avg, 
         ugds,
         tuitionfee_in, 
         tuitionfee_out
         control,
         region) 
```

## Choosing rows with `filter`

Remove the schools that are private (category 2) and for-profit (category 3).

```{r}
#| label: add-label-in-kebab-case

#add comment
colleges_clean2 <- colleges_clean1 |> 
  filter(control = 1) 
```


## Learn a new function to create a variable

Create a new variable called `tuition_diff` which contains the
difference between out-of-state and in-state costs. Look at the cheatsheet to identify the new function you need to use.  

```{r}
#| label: add-label-in-kebab-case

#add comment

colleges_clean5 <- colleges_clean3 |> 
   _______(tuition_diff = tuitionfee_out - tuitionfee_in)  
```

<!-- Swap roles -- Computer becomes Coder, Coder becomes Computer! -->

## Learn a new function to drop missing values

Identify the right function name that removes every row with missing data as the function provided `drop.na()` is incorrect.  

```{r}
#| label: add-label-in-kebab-case

#add comment
colleges_clean6 <- colleges_clean5 |> 
  drop.na()
colleges_clean6
```


## Combine everything into one pipe

At this point we've made six different datasets, but notice each of these steps
started with `colleges_clean <- colleges_clean |>`. That is pretty redundant! 
Instead, we could have performed all these tasks as one long "pipeline."

Combine your (fixed) code chunks into a **single** code chunk that
carries out all of the steps necessary to clean the data. 

*Think about coding efficiency -- you __should not__ have multiple calls to the __same__ function!*

```{r}
#| label: add-label-in-kebab-case

#add comment

colleges_clean <- colleges |>
  

colleges_clean |> 
  arrange(adm_rate) #check the instructions - did you get the same output?
```

<!-- Swap roles -- Computer becomes Coder, Coder becomes Computer! -->

# Part Two: Identify the Mystery College

Wow! Your best friend Monte has been accepted to his top choice college!
Unfortunately, Monte is an otterly mysterious person, and he won't tell you
directly which college this is. You'll have to use his clues to figure out which
school is his dream school.

**Clues:**

1.  This college is located in Region 8.

2.  This college's admission rate is less than 85% (0.85).

3.  This college **does not** charge the same for in- and out-of-state tuition. You will need the code that created the new variable `tuition_diff` from above.  

<!-- Swap roles -- Computer becomes Coder, Coder becomes Computer! -->

4.  Less than 15,000 undergraduates attend this college.  

5.  This college is located in the contiguous 48 states. This should be one argument that excludes the two states that are not connected to the other 48 states (Hint: you can add `!` in front of arguments to negate them).

<!-- Swap roles -- Computer becomes Coder, Coder becomes Computer! -->

6.  The average SAT score of this college is an even number. (Hint: an even number modulo (`%%`) 2 is equal to 0). 

7.  Of the five options remaining at this step, Monte will attend the college
that has the lowest admission rate - organize the data so it is the first listed of the five remaining colleges.  

*Think about coding efficiency -- you __should not__ have multiple calls to the __same__ function! In addition, you should only start with the variables you need (Hint: see `select` code above).*

```{r}
#| label: add-label-in-kebab-case

#add comment
colleges |> 
  
```

Submit the college Monte will attend to the Canvas Quiz.
