Version 1.0 released in July 2019 and coded by Dr. Samuel Shen, Distinguished Professor

San Diego State University, California, USA

Email:

Version 2.0 compiled and updated by Momtaza Sayd

San Diego State University May 2021

Version 3.0 compiled and updated by Joaquin Stawsky

San Diego State University June 2022

## Chapter 2: Basics of R Programming ### R as a smart calculator ```{r} 1+4 2+pi/4-0.8 x <- 1 y <- 2 z <- 4 t <- 2*x^y-z t u = 2 # "=" sign and "<-" are almost equivalent v = 3 # The text behind the "#" sign is comments u+v sin(u*v) # u*v = 6 in the sine function is considered a radian by R ```

### Define a sequence in R ```{r} #Enter temperature data in c() tmax <- c(77, 72, 75, 73, 66, 64, 59) #Show the data tmax #Generate same sequence using different methods seq(1,8) seq(8) seq(1,8, by = 1) seq(1,8, length = 8) seq(1,8, length.out = 8) ```

### Define a function in R ```{r} #Define a function samfctn <- function(x) x*x samfctn(4) fctn2 <- function(x,y,z) x+y-z/2 fctn2(1,2,3) ```

### Plot with R ```{r} #Plot temperature data plot(1:7, c(77, 72, 75, 73, 66, 64, 59)) #More plot examples plot(sin, -pi, 2*pi) #plot the curve of y = sin(x) from -pi to 2 pi square <- function(x) x*x #Define a function plot(square, -3, 2) # Plot the defined function # Plot a 3D surface x <- seq(-1, 1, length = 100) y <- seq(-1, 1, length = 100) z <- outer(x, y, function(x, y)(1-x^2-y^2)) # 'outer(x,y, function)' renders z function on the x, y grid persp(x,y,z, theta = 330) # yields a 3D surface with perspective angle 330 deg #Contour plot contour(x,y,z) #lined contours filled.contour(x,y,z) #color map of contours with default colors ```

### Symbolic calculations by R ```{r} D(expression(x^2,'x'), 'x') # Take derivative of x^2 w.r.t. x 2 * x #The answer is 2x fx <- expression(x^2,'x') #assign a function D(fx,'x') #differentiate the function w.r.t. x 2 * x #The answer is 2x fx <- expression(x^2*sin(x),'x') #Change the expression and use the same derivative command D(fx,'x') 2 * x * sin(x) + x^2 * cos(x) fxy <- expression(x^2+y^2, 'x','y') #One can define a function of 2 or more variables fxy #Renders an expression of the function in terms of x and y #expression(x^2 + y^2, "x", "y") D(fxy,'x') #yields the partial derivative with respect to x: 2 * x D(fxy,'y') #yields the partial derivative with respect to y: 2 * y square <- function(x) x^2 #Integrate x^2 from 0 to 1 equals to 1/3 with details below integrate(square,0,1) #0.3333333 with absolute error < 3.7e-15 #Integrate cos(x) from 0 to pi/2 equals to 1 with details below integrate(cos,0,pi/2) #1 with absolute error < 1.1e-14 ```

### Simple statistics by R ```{r} x <- rnorm(10) #generate 10 normally distributed numbers x mean(x) var(x) sd(x) median(x) quantile(x) range(x) #yields the min and max of x max(x) boxplot(x) #yields the box plot of x w <- rnorm(1000) hist(w) #yields the histogram of 1000 random numbers with a normal distribution summary(rnorm(12)) #statistical summary of the data sequence #Linear regression and linear trend line #2007-2016 data of the global temperature anomalies #Source: NOAAGlobalTemp data t <- 2007:2016 T <- c(.36,.30, .39, .46, .33, .38, .42, .50, .66, .70) lm(T ~ t) #Linear regression model of temp vs time #Temperature change rate is 0.03673 deg C/yr or 0.37 deg C/decade # expression(paste(...)) is one way of using the degree symbol # ...pasting the ° symbol directly is another way plot(t,T, type = "o",xlab = "Year",ylab = expression(paste("Temperature [",~degree,"C]")), main = expression(paste("2007-2016 Global Temperature Anomalies\n and Their Linear Trend [0.37 °C/decade]"))) abline(lm(T ~ t), lwd = 2, col = "red") #Regression line ```

### YouTube tutorial: Input data by reading a csv file into R ```{r} #The R packages and the datasets used in this book are #listed below and can be downloaded and installed first #before proceeding to the R codes in the rest of the book. #The R packages: #animation, chron, e1071, fields, ggplot2, lattice, #latticeExtra, maps, mapdata, mapproj, matrixStats, ncdf, #NLRoot, RColorBrewer, rgdal, rasterVis, raster, sp, TTR #To load the package "animation", you can do library(animation) #You can also load all these packages in one shot #using pacman #install.packages("pacman") library(pacman) pacman::p_load(animation, chron, e1071, fields, ggplot2, lattice, latticeExtra, maps, mapdata, mapproj, matrixStats, ncdf4, NLRoot, RColorBrewer, rgdal, rasterVis, raster, sp, TTR) #The zipped data file: # https://www.cambridge.org/climatemathematics/data.zip #On your computer, you can create a directory called #climmath under your user name. #The one used in the book is Users/sshen/climmath #You unzip the data and move the data folder under #the Users/sshen/climmath directory. #A data folder will created: #Users/sshen/climmath/data. #The data folder contains about 400 MB of data. #Place all the R codes in the directory Users/sshen/climmath. #Then, you can run all the codes in this book after replacing sshen #by your user name on your own computer. ```