###Code for Lecture 1 # this is a comment - R ignores it! help(exp) #display help for file function in parantheses ?exp #display help for file function in parantheses ??”logistic regression” #search help files for phrase in quotes; quotes not needed for single words ### Basic Math exp(1.5) ### calculate e to the 1.5 power x=exp(1.5) ### make 'x' equal to exp(1.5) x ### tell us what x is - Note that x is a scalar (single value) x=2.718282^1.5 ### make x equal to e to the 1.5 power; ^ stands for power x ### tell us what x is y=log(x) ### make y equal to natural log of x y ### tell us what y is help(log) ### pull up the help file on log so we can figure out how to change the base y=log(x,base=10) ###calculate log (base 10) of x, call it 'y' ###Vectors (lists of values) x=c(1,2,3)) ### 'c' tells R you are making a list of values; also called a 'vector' #### single value is a 'scalar' 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 vector ###Fancy ways of making vectors x=(0:100) ###create a vector of values from 0 to 100 inclusive, call it x sequence=(0:100) ###create a vector of values from 0 to 100 inclusive, call is sequence sequence ### tell us what 'sequence' is sequenceByTen=(0:100)*10 ### create a vector from 0 to 1000 by 10, call it 'sequenceByTen' ###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 help(seq) ### display the help file for the function 'seq' x=seq(0,1000,10) ### create a vector from 0 to 1000 by 10, call it 'x' y=rep(c(1,2,3,4,5),20) ### create a vector of values 1,2,3,4,5 repeating 20 times; call it 'y' ###Make your own functions expTest=function(x){ ###create a function to calculate e to the power of x - bracket starts the calculations y=2.718282^x ###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 ###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 mean(datum) var(datum) ###variance-covariance matrix) sd(datum) plot(datum) ###note that x-axis will be first variable in matrix Plot(datum$year,plot$carbon) Plot(datum$year,datum$carbon,type=’l’) #### plot a line graph #### ways to save work in R #### can create a 'new script', run the script and/or save the script for later #### To save "console": ##### 'file', 'save workspace' saves the various objects ##### 'file', 'save history' saves commands that were run, but not the output ##### 'file', 'save file' saves all the text in the console including both commands and output ### Importing data ### Don't forget to change the directory first!!!! ### Don't call your data 'data' - that word already has a meaning in R help(read.csv) datum=read.csv("example.csv") ### creates a data frame named datum from imported csv file ### check data was imported properly names(datum) ### column headers of data file head(datum) ### column headers of data file plus first 6 rows of data head(datum,10) ### number of rows of data can be changed summary(datum) ### summary statistics for datum ### alternatively: datum=read.csv(file.choose())