7 Time Series

This week we’ll look at some Time-Series data from DEOS. Delaware has the highest concentration of environmental monitoring stations in the country thanks to DEOS (Delaware Environmental Observing System) which is controlled by University of Delaware’s CEMA (Center for Environmental Monitoring and Analysis). The data collected using this dense network is useful for a variety of purposes. Before we dive into the data, let’s cover an important skill - the for loop.

7.1 For Loops

For loops & conditional statements are a key skill in programming. They allow you to process through large datasets or multiple datasets thus minimizing the amount of manual work you need to do. The basic for loop looks like this…

## [1] 8
## [1] 16
## [1] 24
## [1] 32
## [1] 40
## [1] 48
## [1] 56
## [1] 64
## [1] 72
## [1] 80

Notice the general structure of R for loops. for signals to R you’re beginning a for loop, which requires the general structure to look like:

for (something in something inside these parentheses){ do something within these curly brackets }

Yes, you must have these parentheses and curly brackets present and surrounding the appropriate code. If you forget a parentheses or curly bracket you’ll have errors pop up…this happens to advanced programmers all the time, so don’t be discouraged if it takes time getting used to this.

The general structure is always: for(condition){do something}. If statements are set up the same way

## [1] 32

This is referred to as a nested loop, because there is a conditional statement within another one. Key takeaway here: in programming languages, = is an assignment (i.e. x = 4), whereas == is an equality test (i == 4). To put this loop in layman’s terms: for i in numbersList, if i is equal to 4, multiply i by 8 and then print temNumber.

We can also have nested for loops.

## [1] "1" "A"
## [1] "1" "B"
## [1] "1" "C"
## [1] "2" "A"
## [1] "2" "B"
## [1] "2" "C"
## [1] "3" "A"
## [1] "3" "B"
## [1] "3" "C"

You can name the object within the list whatever you want (i, j, num, let, etc.). Also, c() is the concatenate function that combines values into a vector or list. The order doesn’t matter in this for loop…

## [1] "1" "A"
## [1] "2" "A"
## [1] "3" "A"
## [1] "1" "B"
## [1] "2" "B"
## [1] "3" "B"
## [1] "1" "C"
## [1] "2" "C"
## [1] "3" "C"

But it does in this one…

Here’s one more example for multi conditional statement with an else…

## [1] "Not what we want"
## [1] "Not what we want"
## [1] "Not what we want"
## [1] "Not what we want"
## [1] "Not what we want"
## [1] "Not what we want"
## [1] "Not what we want"
## [1] "3" "B"
## [1] "Not what we want"

&& means “and” … || means “or”…these are useful in multi conditional statements. The else statement is an appendage of the if statement. It basically means if num == 3 and let == B is false, print "not what we want". Notice that the ‘else’ statement is outside of the if statement but immediately after it.

In Class Exercise: debug this for loop

And get this output…

## [1] 1
## [1] 0.09
## [1] 0.09
## [1] 2
## [1] 0.05
## [1] 0.05
## [1] 3
## [1] 0.03
## [1] 0.03
## [1] 4
## [1] 0.01
## [1] 0.01
## [1] 5
## [1] 0.005
## [1] 0.005

7.2 DEOS Data

The dataset we’ll be using is named - DEOS.csv . It contains a variety of meteorological variables collected at 5 minute intervals. Let’s dive into the coded example.

Note: When we load the file, your path might look something like this:

Downloads/geog473-673/datasets/DEOS.csv

Remember, we need to tell R where we stored the file we want to open. We can use getwd() to see where R is looking right now. From there, we can tell it to look in Downloads, or Documents, or Desktop, etc.

##    Timestamp..UTC. Air.Temperature.deg..C. Dew.Point.Temperature.deg..C.
## 1 2014-04-04 04:05                     7.1                           4.3
## 2 2014-04-04 04:10                     6.9                           4.3
## 3 2014-04-04 04:15                     6.8                           4.3
## 4 2014-04-04 04:20                     6.7                           4.4
## 5 2014-04-04 04:25                     6.6                           4.4
## 6 2014-04-04 04:30                     6.6                           4.4
##   Wind.Speed.m.sec. Wind.Direction.deg.. Barometric.Pressure..
## 1               3.7                351.7                    NA
## 2               3.5                352.9                    NA
## 3               3.9                357.5                    NA
## 4               3.9                356.8                    NA
## 5               3.3                  5.5                    NA
## 6               2.7                 18.4                    NA
##   Solar.Radiation.UNKNOWN. Wind.Gust.Speed..5..m.sec.
## 1                        0                        5.1
## 2                        0                        5.8
## 3                        0                        6.2
## 4                        0                        5.9
## 5                        0                        4.9
## 6                        0                        3.5
##   Gage.Precipitation..5..mm.
## 1                       0.00
## 2                       0.00
## 3                       0.25
## 4                       0.00
## 5                       0.00
## 6                       0.00
##  Timestamp..UTC.    Air.Temperature.deg..C. Dew.Point.Temperature.deg..C.
##  Length:182645      Min.   :-51.0           Min.   :-53.300              
##  Class :character   1st Qu.:  7.6           1st Qu.:  2.200              
##  Mode  :character   Median : 15.2           Median : 10.200              
##                     Mean   : 14.1           Mean   :  8.505              
##                     3rd Qu.: 21.2           3rd Qu.: 16.200              
##                     Max.   : 37.0           Max.   : 25.500              
##  Wind.Speed.m.sec. Wind.Direction.deg.. Barometric.Pressure..
##  Min.   : 0.100    Min.   :  0.0        Mode:logical         
##  1st Qu.: 1.800    1st Qu.: 99.3        NA's:182645          
##  Median : 2.600    Median :202.2                             
##  Mean   : 2.997    Mean   :187.0                             
##  3rd Qu.: 3.700    3rd Qu.:258.9                             
##  Max.   :16.700    Max.   :360.0                             
##  Solar.Radiation.UNKNOWN. Wind.Gust.Speed..5..m.sec. Gage.Precipitation..5..mm.
##  Min.   :   0.0           Min.   : 0.100             Min.   :0.00000           
##  1st Qu.:   0.0           1st Qu.: 2.900             1st Qu.:0.00000           
##  Median :   4.0           Median : 4.400             Median :0.00000           
##  Mean   : 192.7           Mean   : 4.907             Mean   :0.01354           
##  3rd Qu.: 313.0           3rd Qu.: 6.400             3rd Qu.:0.00000           
##  Max.   :1335.0           Max.   :24.400             Max.   :9.65000
## [1] "Timestamp..UTC."               "Air.Temperature.deg..C."      
## [3] "Dew.Point.Temperature.deg..C." "Wind.Speed.m.sec."            
## [5] "Wind.Direction.deg.."          "Barometric.Pressure.."        
## [7] "Solar.Radiation.UNKNOWN."      "Wind.Gust.Speed..5..m.sec."   
## [9] "Gage.Precipitation..5..mm."
## [1] "2014-04-04 04:05"
## [1] "character"

7.3 Assignment

  1. Subset dataset to January 2015 only
  2. Convert Wind Speed & Wind Gust data from m/s to mph
  3. Plot wind speed and wind gust on same plot - wind gust as points first, then wind speed as a line second
  • Hint: you’ll have to use an argument of type = "p" for wind gust.
  1. Add legend
  2. Compute correlation coefficient between wind gust and wind speed using cor() function - add to title of plot
  3. Deliver Resulting Plot to Canvas assignment week3

Aim for something like this (does not have to be exact)…