###Basic R Code # this is a comment - R ignores it! # I highly encourage you to use comments in your code as code is much like a foreign language # If you go back to code several months after you've written it, it won't make much sense # unless you have lots of comments ### Basic Math 3+5 # add 3 and 5 2*3 4^2 exp(1.5) ### calculate e to the 1.5 power x=exp(1.5) ### make 'x' equal to exp(1.5), doesn't report anything x ### tell us what x is y=log(x) ### make y equal to natural log of x y ### tell us what y is #### How to repeat commands x=exp(1.5 #this code won't work as it is incomplete. R will give a '+' on the next line ) ### to complete the code, just type ')' or escape to get out. ### you can scroll through old commands using up and down arrows #### How to use the help files help(exp) # brings up the help file for the 'exp' function ??'exponentiate' # searches for functions that include the words 'exponentiate'. ###Vectors x=c(4,5,6) ### 'c' tells R you are making a list of values; also called a 'vector' x ### tell us what x is x[2] ### tell us what the 2nd value of x is exp(x) ### calculate exp to the power of each value in x; return a list of scalars ###Functions – what they are and how they are used ###a function is any command that takes arguments (in parentheses) and returns results based on a calculation y=log(15) # gives us the natural log of '15' y=log(15, base=10) #gives us the log base 10 of '15' y=log(15,10) ### calculates the log base-10 of '15'; careful not specifying arguments.... ###Matrices year=c(1800,1850,1900,1950,2000) ### create a vector called 'year' with 5 values carbon=c(8,54,534,1630,6611) ###create a vector called 'carbon' with 5 values ### A matrix is a block of numbers with multiple rows and columns datum=data.frame(Year=year,Carbon=carbon) #create a matrix with two columns (year and carbon) and 5 rows datum #### show us the matrix 'datum' datum[2] ### show just the second column of 'datum' datum$carbon ### show just second column of 'datum' - the one named 'carbon' ###Easy summaries/analysis of data summary(datum) ###give mean and quartile of datum head(datum) ###checks that data was imported properly names(datum) ### gives you attributes of data or other objects ### Making simple plots in R plot(datum) ###note that x-axis will be first variable in matrix plot(Carbon~Year) ###This line of code wont work. 'Carbon' doesn't exist outside the datum data.frame plot(Carbon~Year,data=datum) #One way of doing plots (Y~X) plot(x=datum$Year,y=datum$Carbon) #Another way of doing plots (X,Y) plot(Carbon~Year,data=datum,type=’l’) #### plot a line graph - note that 'l' is an small L ###Cleaning up R ### Clear console by 'Edit', 'Clear Console' ### Does not eliminate the objects you created ls() ### list all of the objects in memory rm() ### remove objects in memory ### 'Misc', 'Remove all objects' to clear memory ### Packages ###'Packages', 'Install Package(s)' installs library of code library(nlme) ### loads the package 'nlme'; note package must be installed. ### Code to load data into R datum=read.csv(file.choose()) ### loads a csv file (that must be found in an explorer window) and calls it 'datum'