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

Some data in this lab comes from the OCS "Exploring CO2 emissions across time" activity (https://www.opencasestudies.org/ocs-bp-co2-emissions/. This dataset is available in the `dasehr` package.

Additional data about climate change disasters can be found at "https://daseh.org/data/Yearly_CC_Disasters.csv".

```{r message=FALSE}
library(tidyverse)
library(dasehr)
```

# Part 1

### 1.1

Open the `yearly_co2_emissions` dataset from the `dasehr` package and assign it to an object called `co2`. (You can also use `read_csv()` from the `readr` package and download the dataset directly from the daseh.org website: "https://daseh.org/data/Yearly_CO2_Emissions_1000_tonnes.csv")

-  Remember to use `read_csv()` from the `readr` package.
-  Do NOT use `read.csv()`.

```{r 1.1response}

```

### 1.2

Look at the column names using `colnames` - do you notice any patterns?

```{r 1.2response}

```

### 1.3

Let's rename the column "2011" in "co2" to "CO2_2011" using `rename`. Repeat this for the years 2012, 2013, and 2014. Make sure to reassign to `co2` here and in subsequent steps.

```
# General format
new_data <- old_data %>% rename(newname = oldname)
```

```{r 1.3response}

```

### 1.4

Select only the columns "country", and those that start with "CO2_". Use `select` and `starts_with("CO2_")`.

```
# General format
new_data <- old_data %>% select(colname1, colname2, ...)
```

```{r 1.4response}

```

### 1.5

Create a new dataset "co2_long" that does `pivot_longer()` on all columns except "country". Remember that `!country` means all columns except "country".

```
# General format
new_data <- old_data %>% pivot_longer(cols = colname(s))
```

```{r 1.5response}

```

### 1.6

Using `co2_long`, filter the "country" column so it only includes values from Indonesia and Canada. **Hint**: use `filter` and `%in%`.

```
# General format
new_data <- old_data %>% filter(colname %in% c(...))
```

```{r 1.6response}

```

### 1.7

Use `pivot_wider` to reshape "co2_long". Use "county" for the `names_from` argument. Use "value" for the `values_from` argument. Call this new data `co2_wide`. Look at the data. How do these years compare to one another?

```
# General format
new_data <- old_data %>% pivot_wider(names_from = column1, values_from = column2)
```

```{r 1.7response}

```


# Practice on Your Own!

### P.1

Take the code from Questions 1.1 and 1.3-1.7. Chain all of this code together using the pipe ` %>% `. Call your data `co2_compare`.

```{r P.1response}

```

### P.2

Modify the code from Question P.1:

-  Choose 4 different years to examine 
-  Select different countries to compare
-  Call your data `co2_compare2`

```{r P.2response}

```


# Part 2

### 2.1

Open the `Yearly_CC_Disasters` dataset using the url below. Save the dataset as an object called "cc". rename the column "Country" to "country".

"https://daseh.org/data/Yearly_CC_Disasters.csv"


```{r 2.1response}

```


### 2.2

Join the datasets "co2" and "cc" by "country". Keep all rows, even if there is no match. How many rows are there?

```
# General format
full_join(data1, data2, by = "")
```

```{r 2.2response}

```


### 2.3

Join the datasets "co2" and "cc" by "country". Keep only rows that match both datasets. How many rows are there?

```
# General format
inner_join(data1, data2, by = "")
```

```{r 2.3response}

```


### 2.4

What countries are present in "co2" that are not present in "cc"? Use `anti_join`. What countries are present in "cc" that are not present in "co2"?

```
# General format
anti_join(data1, data2, by = "") %>% select(index)
```

```{r 2.4response}

```


# Practice on Your Own!

### P.3

Take the code from 2.2 and save the output as an object "co2_cc". Filter the dataset. Filter so that you only keep Indonesia and Canada.

```{r P.3response}

```


### P.4

Select:

* the "country" column
* data from the years 2014 originally in BOTH DATASETS (columns "CO2_2014" and `2014`)
* the "Indicator" column

Rename:

* emissions = CO2_2014
* disasters = `2014`

Reassign to "co2_cc".

```{r P.4response}

```


### P.5

Use `stringr` to trim the piece of text, "Climate related disasters frequency, Number of Disasters: ", from the "Indicator" column. You will use the function `str_remove()` to do this. It works similarly to other `stringr` functions. Try to intuit how it works by using the documentation page (`?str_remove`).

```{r P.5response}

```


### P.6

Pivot the dataset so that there are columns for country, emissions, and a column for each "Indicator".

```{r P.6response}

```
