##### Training Course on Economic Assessment Methods for Policy
#### Support of Climate Change Adaptation in the Agricultural Sector in Lao PDR ######
###################################################################### Worshop 1 ########################################################
# Introduction to Statistical Computing with R
#############################################################################################################################################
# Let's Begin!
# [1] Use the menu to navigate to the working directory where you saved your data files
# [2] Use getwd() to see the path on your computer
# [3] Copy and paste the path for your computer into the command below:
setwd("PATH TO WorkshopMaterials_Day1 DIRECTORY ON YOUR COMPUTER HERE")
## [0] Installing and Opening R
#In order to use a packages there are two steps:
#[1] Install the package. This only has to be done once on a computer.
#[2] Load the packages for use. This has to be done every time you start a new R session.
install.packages("sp") #[1]
require(sp) #[2]
install.packages("maptools")
require(maptools)
#Note: # is for comments. Anytime R reads a # it will skip over everything after it on the same line.
# [1] Objects in R:
# R stores things called "objects" in its memory which can include numbers, imported data sets, and functions that operate on numbers/data, among other things.
# You can type any numbers in R and they will print out the result but nothing is stored.
3
3+4
7/3
# Use the equals sign (=) to create new objects by giving names to numbers, data, or the output of functions.
w=3
x=4
y=w+x
z=y/x
# You can see what the object is by typing its name (this is equivalent to using the print() function).
w
x
y
z
print(z)
# [2] Functions
# Almost every command you execute in R uses one or more functions. Functions are called by their name followed by parentheses ().
# For most functions, there are arguments that need to be specified inside the parentheses that tell the function more about
# what you want it to do. Even if there are no arguments, you still need to use parenthese.
ls()
list.files()
help()
help(plot)
?plot
# You can remove objects with the rm() function.
x=3
x
rm(x)
x
rm(list = ls()) # Clears memory for the entire workspace
# Most functions have specific arguments that can be used with that function. For example, the sequence function creates an object
# that is a sequence of numbers.
seq(from=1,to=10,by=2)
seq(1,10,2)
seq(2,10,1)
seq(by=2, to=10, from=1)
# [3] Storage Types
# [i] Vectors:
# The c() function concatonates, or combines, the arguments that you list inside the parentheses into one vector object.
y1=c(1,10,2)
y2=c("a","b","c")
y3=c("a","b",1,2,3)
y1
y2
y3
# You can test the size of the vector with length()
length(y1)
length(y3)
# [ii] Matrices:
# Matrices can store multiple vectors (of the same type and length) in one object.
# The function matrix() is used to create matrices.
matrix(nrow=2,ncol=2)
z_matrix=matrix(nrow=5,ncol=3,data=y2, byrow=T)
z_matrix
#Right now, y3 is a vector, but we can force it to be a matrix:
y3 = as.matrix(y3)
# you can test the size of a matrix with dim()
dim(z_matrix)
# [iii] Data Frames
# Data frames are similar to matrices but differ in at least two important ways.
# (1) Data frames can store characters and numbers in the same object.
# (2) Data frames can have row and column names. Matrices cannot. This is useful for data sets with variable names.
# Because of these differences, most data sets that you import into R will be imported as data frames.
z_df=data.frame(y1,y2,y1)
z_df
# and we can give names to the columns
names(z_df)
names(z_df) = c("column1", "column2", "column3")
z_df
# For both matrices and data frames, you can access rows(columns) of the data by using object.name[row,column] index.
# For example,
matrix = matrix(nrow = 3, ncol = 3, data = 1:9)
matrix
matrix[1,1]
matrix[3,3]
matrix[1,]
matrix[,2]
z_df
z_df[,"column2"]
z_df[1,"column2"]
# [3] Reading and writing data
# It is simple to read/write Excel from R.
# csv files
read.csv(file="Data/ClimateData_1975.csv")
data=read.csv(file="Data/ClimateData_1975.csv")
data
names(data)
# We can also subset data by columns
tmp.data = data[,c("tmp_1","tmp_2","tmp_3","tmp_4","tmp_5","tmp_6","tmp_7","tmp_8","tmp_9","tmp_10","tmp_11","tmp_12")]
# and take averages of columns (2) or rows(1)
ave.tmp.data = apply(tmp.data, 2, mean)
# [4] Plots in R:
# R has very powerful graphics packages that can be used to make of almost any type of graphic you can think of.
# We can plot the averages from the previous exercise:
plot(1:12, ave.tmp.data)
# and make the plots pretty
plot(1:12, ave.tmp.data, type="l", axes = F, col = "red", lwd=2, xlab = "", ylab = "", ylim = c(15,28))
axis(side = 1, at = 1:12, labels = c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"), tick = F)
axis(side = 2, at = seq(15,25,5), tick = F, las=2)
abline(h = c(15,20,25), col= "gray", lwd=0.5)
points(1:12, ave.tmp.data, col="red", pch=20)
mtext(side = 3, text = "Average Monthly Temperature in Lao PDR in 1975")
# In the next workshop session, we will take these plots further and also make maps.