# Day 1 # Simple arithmetic using R 2 * 5 1 - 1 + 3 4 + 5 / 5 # Parentheses supercede other operations 4 + (5 + 5) 4 + (5 + 5) # This line won't be executed # scientific notation 2/10000 1e9 # introduction to functions # Think of a 3-part summary: what input does it take? # what does it do to the input? # what does it return? log(45) sin(3.14) exp(2) # nested functions; R evaluates from the # inside out log(exp(3)) # comparisons between values 2 > 3 # greater than 2 < 3 # less than 2 >= 3 # greater than or equal to 2 <= 3 # less than or equal to # R is waiting! (1 + 4) # Click into the console and press the ESC button to # cancel the operation and start fresh. # more comparisons 4 == 3 # is equal to 4 == 4 4 != 3 # The Assignment operator: <- x <- 4 + 3 # How to look at a variable that we have assigned: x bookshelves <- 4 == 3 bookshelves # What happens if you put an exclamation point in # front of a variable name? !bookshelves !ryan x # !x why does this return FALSE if x is 7? x as.logical(x) !x y <- 0 y !y # Using the = operator for assignment myVariable = 5 myVariable # we can reassign variables x x <- 5 x x <- x + 4 x # naming conventions surveyID <- 5 surveyID # periods in names survey.ID <- 12 survey.ID survey_ID <- 23 survey_ID # How to get help ?log # brings up the help file for a particular function ??base # Passing arguments to a function log(3) log(x = 3) log(x = 10) log(x = 3) log(x = 3, base = 10) # You must pass all required arguments log() log(base = 10) # Rearranging arguments when calling a function log(x = 3, base = 10) log(base = 10, x = 3) # R *can* take a bunch of unnamed arguments, and # it will assume that they are in the order # specified in the help file # But this makes your code harder to read and # understand log(3, 10) exp(1) # Example of use of log() from the help file ?log log(exp(3)) log10(1e7) # = 7 # MCQ log(x = 1000, base = 10) log10(1000) log(base = 10, x = 1000) log(10, 1000) # When R wants to tell you something ryan + 3 # An error means the code totally broke # Message means that the operation worked, # but R thinks that perhaps that's not what # you wanted it to do. log_of_a_negative <- log(-2) log_of_a_negative # These are ways that R might tell you something, # but they don't necessarily indicate anything is # amiss (even though the message() looks red) message("Hello, world!") cat("Hi again, world") print("Hello there, world!") # Vectors and data frames # vector: a collection of objects of the same type 1:10 numbers <- 1:10 numbers fruit <- c("apple", "orange", "lemon") fruit odds <- c(3, 5, 7) odds # Vectorization odds + 1 odds <- odds + 2 odds odds <- odds - 4 odds log(3) log(odds) # Intro to loading data. load("data/continents.RDA") continents # the head() function shows the first six rows head(continents) ?head head(continents, n = 3) # the str() function str(continents) # summary() summary(continents) # what is the mean population across all of the continents? 1:10 1:1000 # Accessing a data frame head(continents) # Use the $ operator to access individual columns continents$population # vectorization with data frames (same as with vectors!) continents$population / 2 log(x = continents$population) log(x = continents$population, base = 10) # vectors on vectors continents$population / continents$area_km2 pop_density <- continents$population / continents$area_km2 pop_density # add a new column to a data frame continents$population_density <- continents$population / continents$area_km2 continents load("data/continents.RDA") continents # anatomy of a vector odds odds[2] fruit fruit[3] # 3rd element fruit[2:3] # 2nd and 3rd elements fruit[-2] # whole vector without the second element fruit[c(1, 3)] fruit[10] # subsetting with data frames continents continents$population odds continents$population[3] continents$population[2:3] continents$population[odds] continents$population > 1e9 # subsetting with logical continents$population[continents$population > 1e9] # Subsetting a Dataframe: [ROWS, COLUMNS] continents[2, 3] continents[ , 3] continents[3, ] continents[1:3, ] continents$population > 1e9 continents[continents$population > 1e9, ] continents$population > 1e9 which(continents$population > 1e9) continents[continents$continent == "Africa", ]