###Code for Lab 0 # 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(1,2,3) ### '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.... ###Make your own functions expTest=function(zed){ ###create a function to calculate e to the power of x - bracket starts the calculations ###arguments in parentheses are arguments that must be specified when calling the function ###in this case, first argument will be assigned to 'x'; note that x only exists in function y=2.718282^zed ###run a calculation using the argument x return(y) #### return the answer of the calculation } ###bracket ends the calculations expTest(1.5) ###run the expTest function zed ### proof that zed only exists in the function ###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) ###gives column names plus first 6 rows of data names(datum) ### gives just the column names ### 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 ###Saving in R ###'File', 'Save Workspace' saves the objects in memory ###'File', 'Save History' saves the commands your ran, but not the objects ###'File', 'Save to File' saves all text in the console: commands you ran and the output ### Doesn't save objects ###Packages ###'Packages', 'Install Package(s)' installs library of code library(MASS) ### loads the package 'MASS' ###Loading data into R datum=read.csv(file.choose()) ### loads a data-file that you select and creates object 'datum' ### can call data anything you want, but don't call 'data' - function already exists #### Note that data must be in a comma-delimited format ### avoid funny characters in column headers or text entries