---
title: Working with Data Frames
author: YOUR NAME HERE
date: today
format: pdf
execute:
  fig-height: 4
  message: false
  warning: false
  error: true
---

In this lesson we will learn how to work with data in a data frame.

# Meet the Penguins

The `palmerpenguins` data contains size measurements for three penguin species observed on three islands in the Palmer Archipelago, Antarctica.


```{r}
library(______)         # contains data wrangling and plotting functions
library(______)         # nicer plotting functions
library(______)         # for some data to work with
```

## Terminology

**Structured Data** is spreadsheet-like data, rectangular with rows and columns.

* Each row is a single **______**
* Each column is a **______** or characteristic, of the observation

**Unstructured Data** is text, audio and images.

## Data Frames

A `data frame` is R's version of structured data.

* All columns are vectors that have the same number of entries
* Because columns are vectors, each column must contain a single type of data (e.g., characters, integers, factors).

```{r}
pen <- ______::penguins
```

## Let's look at this data

This area also tells us a little bit about the data set, specifically that it has 344 rows (observations) and 8 variables (columns).

```{r}
______(pen)
```

### Inspecting `data.frame` objects

### Working with variables inside data frames

Variables inside data frames are typically accessed using `$` notation and their variable name.

```{r}
pen$______
```

This allows us to perform calculations on an individual variable. Below is an example of creating a frequency table to see how many penguins were recorded each year.

```{r}
table(pen$______)
```

### Subsetting data using logical statements

```{r}
______ <- ______(pen, ______ == ______)
table(pen07$______) # look again at a table to confirm my filter worked. 
```

## Data Types

* Quantitative / Numeric (continuous or discrete) data
* Categorical  (e.g. nominal, ordinal)

### Frequency Tables

You can create a basic frequency table by using the `table()` function.

```{r}
______(pen$species)
```

Relative frequencies (proportions or percentages) are calculated by putting the results of the `table` function inside the `prop.table` function.

```{r}
______(table(pen$species))
```

The variable `pen$species` has ______ (44.2%) records with a value of `Adelie`, ______ (19.8%) records with a value of `Chinstrap`, and ______ (36.0%) records with the value of `Gentoo`.


# Visualizing categorical data

## Barcharts

A Barchart or barplot takes these frequencies, and draws bars along the X-axis where the height of the bars is determined by the frequencies seen in the table.

### `ggplot`

Using `ggplot2` with the `geom_bar()` geometry layer gives us actual wide bars, and better axis labels.
```{r}
ggplot(pen, aes(x=species)) + ______
```

### `sjPlot`

Using the `plot_frq` function from the `sjPlot` package builds on the `geom_bar()` type plot from `ggplot`, but adds frequencies and relative percentages on the plot.
```{r}
plot_frq(pen, "______")
```

This single graph provides a lot of good information and is a recommended choice to use.


# Code style

## Chaining commands

This code is read as

```{r}
pen$species |> ______ 
```

1.  Get the `species` variable from the `pen` data set
2.  *and then* create a frequency table on that variable

