## Data Frames

- Data Frames are the work horse of R objects

- Structured by rows and columns and can be indexed

- Each column is a variable of one type

- Column names can be used to index a variable

- Advice for naming variables applys to naming columns

- Can be specified by grouping vectors of equal length as columns

---
## Data Frame Indexing

- Elements indexed similar to a vector using `[` `]`

- `df[i,j]` will select the element in the $i^{th}$ row and $j^{th}$ column

- `df[ ,j]` will select the entire $j^{th}$ column and treat it as a vector

- `df[i ,]` will select the entire $i^{th}$ row and treat it as a vector

- Logical or integer vectors can also be used in place of i and j used to subset the row and columns

---
## Adding a new Variable to a Data Frame

- Create a new vector that is the same length as other columns

- Append new column to the data frame using the `$` operator

- The new data frame column will adopt the name of the vector


---
## Data Frame Demo

Use [Edgar Anderson's Iris Data](https://en.wikipedia.org/wiki/Iris_flower_data_set):

```{r}
flower <- iris
```

Select Species column (5th column):

```{r}
flower[,5]
```

---
## Demo (Continued)

Select Species column with the `$` operator:

```{r}
flower$Species
```

---
## Demo (Continued)

```{r}
flower$Species == "setosa"
```

---
## Demo (Continued)

```{r}
flower[flower$Species=="setosa", ]
```

---
## Creating our own Data Frame

Create our own data frame using `data_frame` function

```{r message=FALSE, warning=FALSE}
library(tidyverse)

mydf <- data_frame(NUMS = 1:5, 
                   lets = letters[1:5],
                   vehicle = c("car", "boat", "car", "car", "boat"))
mydf
```

---
## Renaming columns

We can use the `names` function to set that first column to lowercase:

```{r}
names(mydf)[1] <- "nums"
mydf
```

---
class: inverse
## Your Turn 

1. Make a data frame with column 1: `1,2,3,4,5,6` and column 2: `a,b,a,b,a,b`

2. Select only rows with value `"a"` in column 2 using logical vector

3. `mtcars` is a built-in data set like `iris`: Extract the 4th row of the `mtcars` data.

---
## Lists

- Lists are a structured collection of R objects

- R objects in a list need not be the same type

- Create lists using the `list` function

- Lists indexed using double square brackets `[[ ]]` to select an object

- Use single square brackets to select two or more list elements. e.g. `[c(2,4)]`

- For named lists, can select a list element with `$` like data frames

---
## List Example

Creating a list containing a vector and a matrix:

```{r}
mylist <- list(matrix(letters[1:10], nrow = 2, ncol = 5),
               seq(0, 49, by = 7))
mylist
```

Use indexing to select the second list element:

```{r}
mylist[[2]]
```

---
class: inverse
## Your Turn 

1. Create a list containing a vector and a 2x3 data frame

2. Use indexing to select the data frame from your list

3. Use further indexing to select the first row from the data frame in your list

---
## Examining Objects

- `head(x)` - View top 6 rows of a data frame

- `tail(x)` - View bottom 6 rows of a data frame

- `summary(x)` - Summary statistics   

- `str(x)` - View structure of object  

- `dim(x)` - View dimensions of object 

- `length(x)` - Returns the length of a vector

---
## Examining Objects Example

We can examine the first two values of an object by passing the `n` parameter to the `head` function:

```{r}
head(iris, n = 2)
```

What's its structure?

```{r}
str(iris)
```

---
class: inverse
## Your Turn 

1. View the top 8 rows of mtcars data

2. What type of object is the mtcars data set?

3. How many rows are in iris data set? (try finding this using `dim` or indexing + length)

4. Summarize the values in each column in iris data set

---
## Working with Output from a Function

- The output from a function  can be saved as an object 

- The object can be any type (data frame, vector, etc.) but is often a list object 

- Items from the output can be extracted for further computing

- The output object can be examined using functions like `str(x)`

---
## Saving Output Demo

- t-test using iris data to see if petal lengths for setosa and versicolor are the same

- `t.test` function can only handle two groups, so we subset out the virginica species

```{r}
t.test(Petal.Length ~ Species, data = iris[iris$Species != "virginica", ])
```

---
## Demo (Continued)

Save the output of the t-test to an object

```{r}
tout <- t.test(Petal.Length ~ Species, data = iris[iris$Species != "virginica", ])
```

Let's look at the structure of this object:

```{r}
str(tout)
```

---
## Demo: Extracting the P-Value

Since this is simply a list, we can use our regular indexing:

```{r}
tout$p.value
tout[[3]]
```

---
## Importing Data

We often need to import in our own data rather than just using built-in datasets.

- First need to tell R where the data is saved using `setwd()`

- Data read in using R functions such as:

    - `read_table()` for reading in .txt files

    - `read_csv()` for reading in .csv files

- Assign the data to new R object when reading in the file

---
## Importing Data Demo

We first create a csv file (We can use a text editor or MS Excel)

Then we load it in:

```{r, eval=FALSE}
littledata <- read_csv("PretendData.csv")
```

---
class: inverse
## Your Turn 

- Make 5 rows of data in an excel spreadsheet and save it as a *tab-delimited txt file*.

- Import this new .txt file into R with `read_table`. You may need to look at the help page for `read_table` in order to properly do this.
