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

## Data sumo

&nbsp;

<center>

> Wrangling a big dataset is like sumo wresting.

> you have to use leverage.

```{r, echo=FALSE, out.width="50%", fig.cap=""}
knitr::include_graphics("img/sumo.png")
```

</center>

## What you will learn

&nbsp;

- Indexing concept
- Using `which()` and subsetting
- Selection on `data.frame` objects
- Using `aggregate()`
- Practice exercises


## "Tidy Data" concept

&nbsp;

<center>

```{r, echo=FALSE, out.width="80%", fig.cap=""}
# knitr::include_graphics("img/wickham-tidy.png")
```

</center>

## Indexing concept

&nbsp;

<center>

**Vectors** of data are like houses on a street.  Each house contains a data value and each house has an address.

```{r, echo=F, out.width="60%", fig.cap=""}
knitr::include_graphics("img/houses.jpg")
```

</center>

## Indexing concept

&nbsp;

A **vector** of data has 'addresses' 1 to *i* 

`my_vector[1:i]` indicates all the addresses in a vector

```{r, echo = T}
my_vec <- c(1,7,3,5) # 4 addresses
length(my_vec)
my_vec[1:4]
```

##  Matrix indices

&nbsp;

<center>

**Matrices** of data are like houses on multiple streets.  Each house has an address and so does each "row" of houses.

```{r, echo=F, out.width="50%", fig.cap=""}
knitr::include_graphics("img/matrix-houses.jfif")
```

</center>


##  **Matrix** indices

A **matrix** of data has rows and columns. 

Row "addresses" 1 to *i*, column addresses 1 to *j*. `my_vector[1:i, 1:j]` indicates all the addresses in a matrix.

```{r, echo = T}
my_mat <- matrix(c(1,7,3,5), nrow = 2) # 2 rows
dim(my_mat)
my_mat[1:2, 1:2]
```

##  Array indices

&nbsp;

<center>

**Arrays** of data are like skyscrapers (well, ones with 3 dimensions are...).  Each array viewed from above has rows and columns of rooms, but also depth consisting of floors.

```{r, echo=F, out.width="50%", fig.cap=""}
knitr::include_graphics("img/array-skyscraper.jfif")
```

</center>


##  Array indices

&nbsp;

<center>


So, **arrays** have more than 2 dimensions. 

A 3 dimensional array has row "addresses" 1 to *i*, column addresses 1 to *j*, and depth of 1 to *k*. 

`my_arr[1:i, 1:j, 1:k]` indicates all the addresses in the array.


```{r, echo=F, out.width="50%", fig.cap=""}
knitr::include_graphics("img/1.5-array.png")
```

</center>



##  Array indices

&nbsp;

```{r, echo = T}
my_arr <- array(c(1,7,3,5,
                  2,5,3,4,
                  3,6,7,8), 
                dim = c(2,2,3)) # i,j,k
dim(my_arr)
```




##  Array indices `my_arr[1:2, 1:2, 1:3]`

```{r, echo = F}
my_arr[1:2, 1:2, 1:3]
```


##  `data.frame` indices

<center> 

Data frames are like matrices, where the columns have names

</center> 

```{r, echo = T}
data("OrchardSprays")
head(OrchardSprays)
```

##  `data.frame` indices

<center> 

Access data with numerical indices

</center> 

```{r, echo = T}
# rows 1:6 of column 1
OrchardSprays[1:6, 1]

# by column name
OrchardSprays[1:6, "treatment"]

```


## The `which()` function

`which()` returns addresses of data objects based on some conditional value

```{r, echo = T}
OrchardSprays$treatment == "A"
which(OrchardSprays$treatment == "A")

```


## Powerful Sumo with `which()` 

```{r, echo = T}
OrchardSprays$decrease

(my_selec <- which(OrchardSprays$treatment == "A"))

OrchardSprays$decrease[my_selec]
```


## The `aggregate()` function

Summarizes a data object with FUN (a function you choose).

E.g. mean of variable for each factor level

```{r, echo = T}
aggregate(x = OrchardSprays$decrease,
          by = list(OrchardSprays$treatment),
          FUN = mean)

```


## Live coding

&nbsp;

<center>

```{r, echo=FALSE, out.width="60%", fig.cap=""}
knitr::include_graphics("img/cat-laptop.jfif")
```

</center>

