PA 1: Find the Mistakes

Working with R Objects

Author

YOUR NAME

Today you will be creating and manipulating vectors, lists, and data frames to uncover a top secret message.

Some advice:

Access

You can access PA1: Find the Mistakes in RStudio in one of two ways:

  1. Click here to access a Posit Cloud project.
  • Note: if you do not have a Posit Cloud account, you will be asked to create one.
  • Note: make sure you save a permanent version!
  1. If you have already installed R, RStudio, and Quarto, you can download the practice activity template here.
  • Make sure to move this from your Downloads folder into your stat-331/practice-activities folder (or equivalent)!

Part One: Setup

Each of the following R chunks will cause one or more errors and / or do the desired task incorrectly. Find the mistake, and correct it to complete the intended action.

  1. Create vectors containing the upper case letters, lower case letters, and some punctuation marks.
lower_case <- c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
                "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")
upper_case <- c("A", "B", "C", "D", "E", "F", "G", "H" "I", "J", "K", "L", "M",
                "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
punctuation <- c(".", ",", "!", "?", "'", '"', "(", ")", " ", "-", ";", ":")
Error in parse(text = input): <text>:3:56: unexpected string constant
2:                 "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z")
3: upper_case <- c("A", "B", "C", "D", "E", "F", "G", "H" "I"
                                                          ^
  1. Make one long vector containing all the symbols.
my_symbols <- cbind(lower_case, upper_case, punctuation)
Error: object 'lower_case' not found
  1. Turn the my_symbols vector into a data frame, with one column named “Symbol”.
my_symbols <- dataframe(Symbol = my_symbols)
Error in dataframe(Symbol = my_symbols): could not find function "dataframe"
  1. Find the total number of symbols we have in our data frame.
len <- length(my_symbols)
Error: object 'my_symbols' not found
  1. Create a new variable in your dataframe that assigns a number to each symbol.
my_symbols%Num <- 1:len
Error in parse(text = input): <text>:1:11: unexpected input
1: my_symbols%Num <- 1:len
              ^

Part Two: Decoding the secret message.

This chunk will load up the encoded secret message as a vector.

library(readr)
top_secret <- read_csv("https://www.dropbox.com/s/k72h1zewk4gtqep/PA_Secret_Code?dl=1", 
                       col_names = FALSE)$X1

By altering this top secret set of numbers, you will be able to create a message. Write your own code to complete the steps, in the order given below.

Hint: To update a vector after performing an operation, you overwrite the existing object with the updated version. This looks something like this:

x <- x + 12,

where the original value(s) in x have had 12 added to them, and the resulting values are put back in to the object named x.

Be careful not to overwrite more than you intend. If this happens, go back and re-read in the raw data and run and any subsequent code chunks to start fresh with the top_secret vector.

  • Add 14 to every number (completed for you!)
## Code completed for you
top_secret <- top_secret + 14
  1. Multiply every number by 18, then subtract 257 (watch your order of operations!)
## Code to carry out step 1
  1. Use the exp() function to exponentiate every number.
## Code to carry out step 2
  1. Square every number.
## Code to carry out step 3

Checkpoint: Headquarters has informed you that at this stage of decoding, there should be 352 numbers in the secret message that are below 17. The code below verifies that this is true for your top_secret object!

Hint: This is what is called a “relational” comparison, where you compare an object to a number and R will give you a vector of TRUEs and FALSEs based on whether the comparison is / is not met. You can then use these TRUEs and FALSEs as numbers, since TRUE = 1 and FALSE = 0 in R land.

# Write code to verify that there are 352 numbers with values **below** 17

Next, carry out the following steps:

  1. Turn your vector of numbers into a matrix with 5 columns. (I would recommend naming this something different than top_secret and informative such as secret_mat.)
## Write code to carry out step 4.
  1. Separately from your top secret numbers, create a vector of all the even numbers between 1 and 382. Name it evens. That is, evens should contain 2, 4, 6, 8 …, 382.
## Write code to carry out step 5.
  1. Subtract the “evens” vector from the first column of your secret message matrix.
## Write code to carry out step 6.
  1. Subtract 100 from all numbers in the 18-24th rows of the 3rd column of your secret message matrix.
## Complete the code to carry out step 7.
  1. Multiply all numbers in the 4th and 5th column of your secret message matrix by 2.
## Code to carry out step 8.
  1. Turn your secret message matrix back into a vector.
## Write code to carry out step 9.

Checkpoint: Headquarters has informed you that at this stage of decoding, all numbers in indices 500 and beyond are below 100. The code below verifies that this is true for your top_secret object!

# Write code to verify that indices 500 and beyond have values **below** 100
  1. Take the square root of all numbers in indices 38 to 465.

  2. Use the round() function to round all numbers to the nearest whole number.

  3. Replace all instances of the number 39 with 20.

  • This step requires another relational comparison, but this time it is equality. Equality in R is checked with a double equal sign rather than a single equal sign!

Checkpoint: Headquarters has informed you that your final message should have 344 even numbers.

Hint: Checking for divisibility is an interesting operation that isn’t done much in R. Modulus is the operation you are interested in, where you are checking for whether the numbers are divisible by 2, with no remainder. See what you can find about modulus in R!

# Code to verify how many even numbers are in your top_secret vector
# Should be 344!

Part Three: The secret message!

Use your final vector of numbers as indices for my_symbols to discover the final message, by running the following code. Note: if your vector of numbers is not named top_secret at this point, change the code below appropriately to use your correct vector!

stringr::str_c(my_symbols$Symbol[top_secret], collapse = "")
Error: object 'my_symbols' not found

Google the first line of this message, if you do not recognize it, to see what poem it is.

You will enter your answer in the PA1 Canvas assignment.