Directions

For this lab, you will create a single .R file called lab01.R. The following exercises will ask you to write code. Place all requested code in this .R file separated by comments which indicate which code corresponds to which exercise.

Note: A comment in R begins with a #. For example:

# this line is a comment, the following line is real code
print("This is code that will run!")

Copy-paste the above code into an R console. You’ll find that the first line has no effect (because it was commented) while the second line will create output.

• Note: Do not write your name or netID anywhere in the file or in the filename. To the best of our ability, we will try to grade labs anonymously.

As this is the first lab, it will largely be graded based on completion. For each exercise that you demonstrate a good-faith effort to complete, you will receive two points.

Exercise 1 (Creating Vectors)

In this exercise we will create, but not store, several vectors. Write code that creates the following vectors:

• A vector of the first nine Fibonacci numbers, starting from 0. Hint: Use the c() function.
• A vector of the integers from 1 to 25. Hint: Use the : operator.
• A vector of the even numbers from 2 to 20. Hint: Use the seq() function.
• A vector that stores the following strings: rock, paper, scissors. Hint: Strings must be surrounded by quotes, ". For example: "this_is_a_string".

Place this code in your .R file for this exercise. Also, run your code to make sure it does what you expect and does not produce any errors!

Exercise 2 (Creating and Storing Vectors)

Write R code that does the following:

• Creates a vector named vector_1 that stores the integers from 1 to 10.
• Creates a vector named vector_2 that stores the numbers 0 and 5.

After writing this code, run the following code, which you should also include in your .R file:

vector_1 + vector_2

Can you figure out what happens when this code is run?

Write an R function named add_maxes that takes two arguments, x and y which are assumed to be vectors of numbers. The function should return the sum of the maximum of the two vectors.

After writing this function, run the following code, which you should also include in your .R file:

add_maxes(x = 1:10, y = 1:20)
add_maxes(x = runif(100), y = runif(100))

See what happens when you repeatedly run this code. You’ll notice that the first line always returns the same result, but the output from the second is a bit less than 2, and always slightly different.

Exercise 4 (Rock, Paper, Scissors)

Write an R function called rock_paper_scissors which has no arguments. The function should return two of the strings "rock", paper", and "scissors" at random with replacement. (That is, it could return something like "rock" and "rock".) The goal of this function is to simulate a game of rock paper scissors.

After writing this function, run the following code, which you should also include in your .R file:

rock_paper_scissors()
rock_paper_scissors()
rock_paper_scissors()

Because the results are random, each time you might get a different result and you will likely not get the same results as other students.

Exercise 5 (Rock, Paper, Scissors, Etc)

Write an R function called rps_extended which has two arguments:

• shapes with a default value of c("rock", "paper", "scissors").
• n_players with a default value of 2.

The function should return n_players of the shapes at random with replacement.

After writing this function, run the following code, which you should also include in your .R file:

# running with default arguments. same as running rock_paper_scissors()
# standard game with two players
rps_extended()

# playing rock-paper-scissors-lizard-spock with two players
rps_extended(shapes = c("rock", "paper", "scissors", "lizard", "spock"))

# playing rock-paper-scissors with three players
rps_extended(n_players = 3)

# using frog-slug-snake with Japanese names and three players
rps_extended(shapes = c("namekuji", "kawazu", "hebi"), n_players = 3)

Note: Use of “rock paper scissors Spock lizard” is in no way an endorsement of the American television show The Big Bang Theory which the instructor believes to be at best bad, and at worst a lazy detriment to nerd culture. To each their own though. Bazinga!