Introduction

This document and code will introduce you to R, a very powerful open-source and free statistical programming language.

Specifically, you will use R to run a few simple functions that wil introduce you to the functionalailty of R.

A few basic functions

Perform arithmetic

1+2+3
## [1] 6
3*(4+7.3)
## [1] 33.9
#Type your own mathematical expression below:

#Best Jersey Number!
(5*10)+4
## [1] 54

Assign and work with variables

g=9.81
m=30
f=m*g
f*m
## [1] 8829

Combine values into a vector

a=c(1,2,3,4)
b=a*2
b/3
## [1] 0.6666667 1.3333333 2.0000000 2.6666667

List the objects in your workspace

ls()
## [1] "a" "b" "f" "g" "m"

Remove objects from your workspace to clean up

rm(f,m,a,b,g)

Loops

This script is designed to import a simple excel type file (csv) with precipitation and runoff for several basins located around the globe. The goal of the excersice is to calculate evapotranspiration using a water balance approach by using a loop. In addition, the script classifies the basins as humid, semi humid, or dry using a condition statement based on the ratio of atmospheric deman (ET) and water supply (precipitation). Thresholds to assign the type of basin are arbitrary and for in class demonstration purposes only.

In this exercise, you will read in a *.csv file, “waterbalance_exercise.csv”, which has been preloaded in your project.

The goal is to calculate evapotranspiration (ET) for several large basins located around the world. As you have learned, ET can be solved for be rearrangein the water balance to solve for the unkown term, in our case, ET.

The code below will cycle through the basins dataset and calculate ET for each basin. Furthermore, we will characterize ach basin as ‘Humid, ’Semi-Humid’ or ‘Dry’ based on the aridity index, which is the ratio of ET to P, in other words, how much precipitation is lost to the atmopshere is evaporation

Import data and identify components of dataset

# We will call this csv file "info"
info<-read.csv("waterbalance_exercise.csv",header=TRUE) 

# You can call your object to see what information it includes
info 
# We will asign the name 'nbas' to denote the number of basins 
nbas<-dim(info)[1] 

# And use "bracket, 1" to pull the information in the first column, which contains basin names
info[,1] #Display basin names
## [1] "Amazon"      "Congo"       "Lena"        "Mackenzie"   "Niger"      
## [6] "Murray"      "Orange"      "Brahmaputra"

Run loops to cycle through dataset

Here we will use a loop to cycle through the basins dataset and calculate ET for each basin.

#Begin for statement loop
for (i in 1:nbas){ 

  #Calculates evapotranspiration using the water balance
    info[i,4]<-info[i,2]-info[i,3] 

    if(info[i,4]/info[i,2]<0.6){ #start condition, E/P<0.6 humid basin

        info[i,5]<-"Humid" #assign type of basin to last column of info variable

    } else if(info[i,4]/info[i,2]<0.9) { #condition, E/P<0.9 semi-humid basin

        info[i,5]<-"Semi-Humid" #assign type of basin to last column of info variable

    } else { #condition, E/P>0.9 dry basin

        info[i,5]<-"Dry" #assign type of basin to last column of info variable

    } #end of condition for basin type classification

} #End for statement loop

Call the completed table to see how the data has been populated

info 

Write and export file as .csv

write.csv(info,"waterbalance_ouput.csv")