### Brief R Tutorial ### Set working directory ### setwd("C:/Users/sabaker/Documents/Meetings_Conferences/Gradstudent Meetings/RTutorial") ### Install libraries ### install.packages("xlsx") #or in Rstudio: Tools -> Install Packages... library(xlsx) ### Use R as Calculator ### 10^3 + 5 (121 + 27) / (2.5^2 ) # R will have a "+" in the command line to show that the statement is not complete # esc will quit the statement x=log(2) exp(x) ### workspace ### a = 11 # assign basic variables # notice this adds the variable to the environment tab in RStudio a a * 2 a = a + 1 ### Clear all variables from memory ### rm(list=ls()) ### Scalars, vectors, and matrices ### ## scalar = single number 0-D a = 1 ## vectors = row of numbers 1-D b = c(1, 2, 3) ## matrices = row and column 2-D c = matrix(data=c(9,2,3,4,5,6),ncol=3) ### Built in Functions - R has built in function ### # mean mean(x=b) # show how the function is shown in RStudio mean(b) ### Help & Documentation ### help(rnorm) # help for rnorm functoin example(rnorm) # example of how to use the function # also use the bottom right window in RStudio to look at packages, functions, ect. # create a normal distribution rnorm(10) # 10 random numbers with a normal distribution rnorm(10, mean=1.2, sd=3.4) # change the mean and standard dev ### Scripts ### # Open new and save with extention ".R" # Run lines scripts with CRTL+ENTER or click Run # Run entire script with CTRL+SHIFT + S # Can run script without opening source("BriefRTutorial.R") #need to set directory before hand ### Data Structures ### ## Vector = row of numbers, 1-D vec1 = c(1,4,6,8,10) vec1 vec1[5] vec1[3] = 12 vec2 = seq(from=0, to=1, by=0.25) vec2 sum(vec1) vec1 + vec2 ## Matrices = just a 2-D vector mat=matrix(data=c(9,2,3,4,5,6),ncol=3) # or specify nrow mat[1,2] #[row,column] mat[2,] mean(mat) ## Data frame = a matrix with names above the columns, eg. timeseries t = data.frame(x = c(11,12,14), y = c(19,20,21), z = c(10,9,7)) t mean(t$z) ## Lists = columns can be different lengths, unlike matrices and dataframes L = list(one=1, two=c(1,2), five=seq(0, 1, length=5)) L names(L) L$five + 10 ### Classes ### ## Numeric a = c(1,2,3) ## Character m = "pear" ## Date date1=strptime( c("20100225230000","20100226000000", "20100226010000"),format="%Y%m%d%H%M%S") ### Graphics ### ## plot par(mfrow=c(1,1)) x = rnorm(100) plot(x) plot(x, type="l", col="gold") # type = line, col = color plot(x, type="b", col="blue") ## histogram par(mfrow=c(2,2)) #change number of plots shown in the window hist(rnorm(100)) hist(rnorm(100)) #changes every time hist(rnorm(100)) hist(rnorm(100)) par(mfrow=c(1,1)) ## not doing##### #fancier plotting t = data.frame(a = c(1,10,5,7,8), b = c(2.5,1.5,9,1,0), c = c(4,3,2,1,0)) plot(t$a, type="l", ylim=range(t),lwd=3, col=rgb(1,0,0,0.3)) #rgb(red, green, blue, alpha lines(t$b, type="s", lwd=2,col=rgb(0.3,0.4,0.3,0.9)) # type = stair steps points(t$c, pch=20, cex=4,col=rgb(0,0,1,0.3)) #cex is scale of point, pch = popints used ##### ### Reading and writing data files ### getwd() setwd("C:/Users/sabaker/Documents/Meetings_Conferences/Gradstudent Meetings/RTutorial") d = data.frame(a = c(3,4,5), b = c(12,43,54)) write.table(d, file="tst0.txt", row.names=FALSE) d2 = read.table(file="tst0.txt", header=TRUE) ### Programming Tools ### ## If-statement w = 3 if( w < 5 ) { d=2 } else{ d=10 } d ## For-loop h = seq(from=1, to=8) s = c() for(i in 2:10) { s[i] = h[i] * 10 } s ## Write your own function fun1 = function(arg1, arg2 ) { w = arg1 ^ 2 return(arg2 + w) } fun1(arg1 = 3, arg2 = 5) ### Lees Ferry flow example ### url = "http://civil.colorado.edu/~balajir/r-session-files/Leesferry-mon-data.txt" # read data data = read.table(url) str(data) data[,1] #years data[1,] #year with flows # name columns and change units colnames(data) <- c("Year","Jan", "Feb", "Mar", "Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") data$Year #years data[2:13] = data[2:13]*(10^-6) #af to MAF # Calc the median and mean flows for each month median = apply(data[2:13], 2, FUN = median) mean = apply(data[2:13], 2, FUN = mean) # Calc the annual flow for each year sum = apply(data[2:13],1, FUN = sum) # Boxplot monthly flows with median/mean, add legend boxplot(data[2:13],xlab="Months",ylab="Monthly Flow (MAF)") lines(mean,col="red") points(median,col="blue") legend("topright",c("Mean","Median"), col=c("red","blue"), lwd=1, lty=c(1,0), pch=c(NA,1))