OCEA90 Fundamentals of Climate

Major Assignment #2 R Code:

In this assignment, you will use R to analyze data for Pacific North America (PNA) index during the winter months of December, January and February (DJF) from 1951-2021. Specifically, you will examine whether there are differences in the PNA index distribution between El Niño and La Niña winters.

Follow the directions below to run the provided R code and generate the .html file you will need to turn in. You will also refer to the figures you create and calculations you perform in this markdown file to answer the questions for Assignment 2 on Canvas.


1. First things first, make yourself an author of this markdown file.

Look at the very top of the page, where the title, author, date, etc. metadata are set for this document. Authors are people who have worked on this markdown file. Find where it says “Insert your name here!”, erase that, and type your name. This is how you will show us that you ran this code yourself.


2. Set your working directory:

Before running any of the code below, remember to set your working directory! To run the code below, R needs to access some data in the file assignment2data.RData. Setting your working directory tells R where to look for this file.

First, make sure assignment2data.RData and this markdown file Assignment2.Rmd are both saved in the same folder on your computer (preferably your designated folder for this class).

Then, at the top of this window, go to the Session drop down menu, mouse over Set Working Directory and select To Source File Location.


3. Then start running all the code chunks below to create and display the figures you will need for your assignment.

To run the code in a given chunk, click the green arrow in the top right corner of that chunk. You can also run all the chunks at once by finding the Run drop down menu in the top right of this window (white box with green arrow), and selecting Run all

This chunk sets the options for how this R Markdown file will be saved when we “knit” the file:

Read PNA index data from the .RData file for this assignment. It loads in three different datasets as separate variables:

-A dataset containing PNA index data from all years from 1951-2021 called pna_all_wtr

-A dataset containing PNA index data from El Niño winters only called pna_el_wtr

-A dataset containing PNA index data from La Niña winters only called pna_la_wtr

# Read the data
load("assignment2data.RData")

Plot the Probability Density Function (PDF) for the average Pacific North America (PNA) index during the winter months of December, January and February (DJF). This plot shows results for all years:

# Plot PDF of winter only data from all years
dT<-0.5
hist(pna_all_wtr,seq(-2.5,2.5,dT),freq=FALSE,
     main="1. PDF of DJF PNA Index, All Years",
     xlab="PNA Index",density=20,ylim=c(0,0.6),col="black")

Plot the PDF for the average DJF PNA index, but this time using data from El Niño winters only:

# Plot PDF for Niño winters only
hist(pna_el_wtr,seq(-2.5,2.5,dT),freq=FALSE,
     main="2. PDF of DJF PNA Index, El Niño Years",
     xlab="PNA Index",density=20,ylim=c(0,1),col="black")

Plot the PDF for the average DJF PNA index, but this time using data from La Niña winters only:

# Plot PDF for Niña winters only
hist(pna_la_wtr,seq(-2.5,2.5,dT),freq=FALSE,
     main="3. PDF of DJF PNA Index, La Niña Years",
     xlab="PNA Index",density=20,ylim=c(0,0.6),col="black")

Plot the Cumulative Distribution Function (CDF) for the average DJF PNA index using data from El Niño winters only:

# Plot CDF for Niño winters only
plot(ecdf(pna_el_wtr),xlab="PNA Index",
     ylab="Cumulative probability",
     main="4. CDF of DJF PNA Index: El Nino Winters",
     do.points=FALSE,verticals=TRUE,col="black")

#add grid to plot
grid(nx = NULL, ny = NULL, col = "black", lty = "dotted",
     lwd = par("lwd"), equilogs = TRUE)

Plot the Cumulative Distribution Function (CDF) for the average DJF PNA index using data from La Niña winters only:

# Plot CDF for Niña winters only
plot(ecdf(pna_la_wtr),xlab="PNA Index",
     ylab="Cumulative probability",
     main="5. CDF of DJF PNA Index: La Nina Winters",
     do.points=FALSE,verticals=TRUE)

#add grid to plot
grid(nx = NULL, ny = NULL, col = "black", lty = "dotted",
     lwd = par("lwd"), equilogs = TRUE)


4. For questions 2, 3, and 4 of your assignment, you are asked to calculate the mean of the PNA index during all winters, El Niño winters and La Niña winters. To make those calculations, type or copy-paste the following commands listed in bold into the code chunk below, each on their own line:

To calculate the mean for all winters: mean(pna_all_wtr)

To calculate the mean for El Niño winters: mean(pna_el_wtr)

To calculate the mean for La Niña winters: mean(pna_la_wtr)

When you run the code chunk, the answers will appear below the code chunk in the order you enter them.

# insert your calculations below:

mean(pna_all_wtr)
## [1] 0.1135681
mean(pna_el_wtr)
## [1] 0.6680263
mean(pna_la_wtr)
## [1] -0.4901471

5. “Knit” this markdown file to save it as a .html, which you will submit on Canvas.

Check that your name is set as an author of this document (at the very top after ‘author:’), so we can see who ran the code.

Then find the “File” dropdown menu on the top left corner of the screen. Hit File>Save to save the changes you made to this file.

Using the File dropdown menu again, select File>Knit Document to save the contents of this R markdown file as a .html. It will be saved in your course folder as Assignment2.html. You will submit this .html file on Canvas to show your work.


Nice work, you finished the coding part of Major Assignment 2! Now upload the Assignment2.html file you just made for question 1 of this assignment. You should also refer to the graphs and calculations shown in Assignment2.html to answer the rest of the Assignment 2 questions.