# This is an R script: a bunch of code that can be executed at the same time. # Usually we'll only want to run a few lines of this code at a time. # To run the code, select the lines you want to execute and hit "Run." # If you only want to run a single line, you can just put your cursor on that line and hit "Run." # Lines beginning with the character # are comments. # These lines appear in the code, but don't do anything. # They're useful for describing what your code does or what the output means. # Good code is commented so that other people reading it can see what you're trying to do. # VARIABLES AND ASSIGNMENT # To declare a variable in R, use "<-" # Assigns the integer value 23 to the variable x. x <- 23 # Assigns the string value "test" to the variable y. y <- "test" # To print the value of a variable or equation, use the print() function. # Prints the value of x. print(x) # Prints the value of y. print(y) # You can reference variables in other mathematical operations. x - 2 7*x^2 # You can use the values of old variables to create new ones. v <- 2*x - 4 print(v) # You can also overwrite old variables with new values. x <- x+1 print(x) # COMPARISONS # It is often useful to compare two variables to each other, # or to compare a variable to another value. # Some comparison operators: >, <, >=, <=, ==, != # Note that != means "is not equal to" and == means "is equal to" # When use properly, these return a Boolean value TRUE or FALSE 14 == 14 13 == 14 13 < 14 14 >= 13 13 != 14 # FLOW CONTROLS # Comparison operators are useful for making R do stuff. # There are three commonly-used control types: if/else, for, and while # IF/ELSE statements take a comparison and decide what to do from there. # If the comparison is true, it executes the code. # If not, and there's an ELSE condition, it executes this other code. # The 'else' part of if/else is optional. if (6 != 7) { print('incorrect') } else { print('correct') } # Curly braces are needed for these types of operations. # Use Shift+Enter to create a line break between braces. # The RStudio interface attempts to place curly braces for you, but sometimes, # they can end up in the wrong place. # The WHILE statement executes a command as long as its input is true # Prints the squares of the even numbers from 10 down to 2 z <- 10 while (z >= 1) { print(z^2) z <- z-2 } # The FOR statements iterates through a list. lis <- c(1, 3, 5, 7, 9, 11, 13, 15) for(i in lis) { print(i^3 - (i-1)) } # You can create a list by using the <- c() command # If you use the c() command on an already existing list, # it will change the list. numbers <- c(1, 2, 3, 4, 5) print(numbers) numbers <- c(0, numbers, 6, 7) print(numbers) # FUNCTIONS # Functions are useful for executing several lines of code at once # Some are already stored within R's libraries, and you can also create your own # One of the most useful functions in probability is the sample function # sample(n, r) returns r randomly selected elements (without replacement) # from the positive integers between 1 and n sample(15, 10) # You can force the function to sample WITH replacement by adding the argument TRUE as a third variable sample(15, 10, TRUE) # This is a function that prints "Hello world!" hello_world <- function() { print("Hello world!") } # This calls the hello_world function written above # Notice that this call contains the '()' characters even though the function doesn't have any variables hello_world() # Usually functions have several lines and do something more complicated. # Find the maximum of three elements # Note: This function doesn't care about ties, and && means "and" (both must be true) max3 <- function(a, b, c) { if ((a > b) && (a > c)) { return(a) } else { if (b > c) { return(b) } else return(c) } } max3(8, 4, 9) max3(9, 8, 4) # It turns out this function already exists in R's libraries, and works for a more general list of numbers! max(4, 8, 9) max(4, 8, 9, 7.5, 22, 40) # Another useful function in R's libraries is the SUM function, which does what you'd expect. addlist <- c(1, 3, 5, 7, 9) sum(addlist) # Example: Function that roll a 6-sided die until it gets a 6. Count the number of times it takes. roll <- function() { # Initiates a variable that will take the value of the die roll. x <- 0 # Initiates a variable that will count the number of die rolls it takes before we get a 6. counter <- 0 # This while loop checks to see if the last roll was a 6. If not, it rolls again and increments the counter. while (x != 6) { x <- sample(6, 1) counter <- counter + 1 } # At the end, RETURN the value you're interested in -- the number of times you rolled the dice. return(counter) } roll() # Let's run this function a bunch of times and see what the results look like. # This function will run roll() n times, creating a list (results) of the number of times it took to get a 6. # Then it returns a table of values, where the top row is the number of rolls needed and the bottom row # is the number of times this number appeared in the list. many_rolls <- function(n) { # Create an empty list results <- c() # Append a roll of the dice to the list n times for (i in 1:n) { results <- c(results, roll()) } return(results) } many_rolls(15) # Assign the result of the many_rolls function to the variable L L <- many_rolls(10000) # Create a frequency table of results (top row: number of rolls; bottom row: frequency) table(L) # Plot the table to visualize results plot(table(L)) # YOU TRY! # Like most things in life, the best way to get good at coding is to practice! # Try to write programs that do the following: # 1) Given a list of positive numbers, the HARMONIC mean is the reciprocal of the average of the reciprocals. # Write a program harmonic_mean that takes a list as input and outputs the computes the harmonic mean of the list # 2) When you've completed 1), write a new peogram random_harmonic which takes as input two integers n and k # and computes the harmonic mean of a randomly chosen k-element subset of the positive integers between 1 and n harmonic_mean <- function(L) { run <- 0 count <- 0 for (i in L) { run <- run + 1/i count <- count+1 } return(count/run) } random_harmonic <- function(n, k){ L <- sample(n, k) return(harmonic_mean) }