---
title: 'Lab 5: Summarizing Data'
format:
  html:
    embed-resources: true
editor: source
knitr:
  opts_chunk:
    message: false
---

###### Question

::: question
Load the `mosaicData` R package in your Quarto document, and then visit the documentation page for the `SaratogaHouses` data set. 
:::


###### Question

::: question
Import the Saratoga Houses data set into your environment using the command `SaratogaHouses <- mosaicData::SaratogaHouses` in your Quarto document.
Use `nrow()` to compute the number of rows in `SaratogaHouses`. 
:::


###### Question

::: question
How old was a "typical" home in Saratoga County during 2006?. To find out, use `summarize()` to compute the median *age* of all the homes in the `SaratogaHouses` data set. 
Be sure to think about what "ingredients" you need to supply for your code to work!
:::


###### Question

::: question
What was the average amount of living space of a home for sale in Saratoga County during 2006? 
Write your own code "from scratch" to compute this summary. 

Be sure to think about:

1. What variable in the data set measures home size?
2. What function in R calculates the average?
3. What are the units in the output
4. What a good name for your summary would be!
:::


###### Question

::: question
Does the price of a home depend on the type of heating system it has?
To find out, use the incomplete code below as a template and compute the median price of all a homes, taking into the type of heating system the home has. 
Be sure to look back at the variables in the data set to figure out which one can tell you what type of heating system a home uses.
:::


```{r unnamed-chunk-11}
#| eval: false
#| code-fold: false
SaratogaHouses |>
  group_by(______) |>
  summarize(median_price = median(price))
```

###### Question

::: question
How does the average size of a home depend on the number of bedrooms in the home? Write your own code "from scratch" to compute this summary.
Be sure to think about 

1) Which variable should be the "grouping" variable (i.e., which variable you should look at to figure out which group a home belongs to) and 
2) Which variable should be the summarized variable?

:::


###### Question

::: question
Get some practice using the `n()` function to summarize the frequency of different category values by using the incomplete code below as a template to figure out how many homes use electric, gas, or oil as their heating fuel.
Be sure to look back in the documentation for the `SaratogaHouses` data set to figure out which variable measures what kind of fuel a home uses, so you know which variable to group the data by!
:::


```{r unnamed-chunk-15}
#| eval: false
#| code-fold: false
SaratogaHouses |>
  group_by(____) |>
  summarize(num_homes = ____)
```

###### Question

::: question
Write your own code "from scratch" to calculate how many homes in the data set do and do not have a waterfront on their property.
:::


###### Question

::: question
Use the `is.na` function and the `penguins` dataset to see only penguins with missing body mass.
:::


You should see that there are only 2 penguins missing body mass observations, one Adelie and one gentoo, compared to 1728 total penguins. With so few missing values, it's unlikely that the mean mass would change much even if the masses of these two penguins were serious outliers. In this type of case, it's often okay to exclude the `NA` values and take the average of all *known* observations. If a large portion of the data (either overall or within a group) were missing, we might not want to do this.

We tell R to exclude NA values by using the `na.rm` argument of the `mean()` function. This argument removes `NA` values before the calculation. It's an optional argument, meaning you don't need to include it unless you want the default behavior of the function to change.


###### Question

::: question
Use the `na.rm` argument of the `mean()` function to calculate the average body mass in grams for each species of penguin with `NA` values removed. If you're not sure how to add this argument, refer to the documentation for the `mean()` function.
:::


You should now see numeric values for all three penguin species.

Many other functions that you use within `summarize()` will behave similarly to the `mean()` function here and will also have the `na.rm` optional argument.


