### Title: ECOL592 Introduction to R Lecture 1 ### Date created: 20140105 ### Last updated: 20140120 ### ### ### Author: Michael Koontz ### ### ### Intention: Script file for ECOL592 Intro to R Lecture 1. We define basic terms, introduce functions, and talk about conditional statements ### ### ### Part 1: Introductions, syllabus, installing R, R Studio ### ### # Intro to class, class format, guest lectures, assignments (including the individual project) # Syllabus # Name, Advisor or Major, Research (if applicable), One specific goal you hope to achieve in this class # R intro # R Studio ### ### ### Part 2: Basic Vocabulary ### ### # Console # Script # 'running' commands (Ctrl+R for Windows, Cmd+Enter for Mac OSX) # syntax -- the "grammar" of R including punctuation, capitalization, code structure ## Formatting ## # comments/metadata (take notes using pound sign?) # spaces are ignored! (except in variable names; can't be used there) # capital letters count! ############ ## Protip ## ############ # Use visual structure to make your code easier to understand (line breaks, spaces, etc.) ## Variables## # "named" objects in R # Properties # # user defined, though there are some that are built in (e.g. pi) # don't change until you tell them to # don't affect any other variables/data that you used to specify them foo <- 5 #foo is assigned to be 5. '<-' is the 'assignment operator' foo # always look at the variable. see what R sees bar = 4 #can use the = sign, but better to stick to using that only in function calls bar dan <- foo * bar #using mathematical operations between variables dan dan <- foo + bar #dan is written over dan dan <- foo - bar dan dan <- foo / bar dan # you can "update" a variable by using it on both sides of the assignment dan <- dan + foo + 6 #Evaluates the math first, then does the assignment... uses the "previous" dan for the math part dan dan <- dan + foo + 6 dan <- dan + foo + 6 dan #always look at the variable! # Other mathematical functions ### ### ### Part 3: Introduction to functions ### ### # What is a function? # Does 3 things: takes in arguments, does something to those arguments, returns ONE thing # syntax: The name of the function, open parenthesis, "arguments" separated by commas, close parenthesis ############ ## Protip ## ############ # Use ? and then the function name to get info about that function ?exp exp(x=5) exp(x=dan) ############ ## Protip ## ############ # Run parts of a line by highlighting just that part. Useful for breaking the code down into smaller parts to see what it is doing log(x=1000) log(x=dan) # Look at the base of the script file to see what arguments are expected in R # In RStudio, put cursor after the parentheses and push 'tab' to see the list of arguments that the function needs # We see that the 'log' function expects 2 arguments: x and base # There needs to be a specification for all arguments, EXCEPT # We also see that base has a default: (base = exp(1)) # So if we don't specify something for base, R will assume it is the default log(x=1000) log(x=1000, base=exp(1)) log(x=1000, base=10) ### ### ### Part 4: Variables continued ### ### ############ ## Protip ## ############ # Use descriptive variable names num.of.tree.species <- 21 num.of.tree.species # different kinds of variables are possible (we'll explore this more later) fruit.1 <- "orange" fruit.2 <- "apples" fruit.3 <- "apple" # Be careful with the type of variable you are using num.of.tree.species area <- "55" num.of.tree.species/area plot.size <- 55 num.of.tree.species/plot.size ## In class check ## # How would you assign a variable to be the given number of species for the given plot size so that you could use it later? ### ### ### Part 5: Conditional statements ### ### 3 == 4 3 != 4 3 < 4 3 <= 4 3 > 4 3 >= 4 fruit.1 == fruit.2 # is equal to fruit.1 != fruit.2 # is not equal to foo > bar foo < bar foo >= bar foo <= bar (fruit.1 == fruit.2) | (foo > bar) # 'or' is the | operator (fruit.1 == fruit.2) & (foo > bar) # 'and' is the & operator ## In class check ## # How would you figure out if R treats capital letters in a character string as coming before or after lower case letters alphabetically? ###################### End first lecture