Practice Activity 2: Version Control

Today you will be creating and manipulating vectors, lists, and data frames to uncover a top secret message using the tidyverse while practicing version control.

This task is complex. It requires many different types of abilities. Everyone will be good at some of these abilities but nobody will be good at all of them. In order to solve this puzzle, you will need to use the skills of each member of your group.

Some advice:

Groupwork Protocols

During the Practice Activity, you and your partner will alternate between two roles—Computer and Coder.

When you are the Computer, you will type into the Quarto document in RStudio. However, you do not type your own ideas. Instead, you type what the Coder tells you to type. You are permitted to ask the Coder clarifying questions, and, if both of you have a question, you are permitted to ask the professor. You are expected to run the code provided by the Coder and, if necessary, to work with the Coder to debug the code. Once the code runs, you are expected to collaborate with the Coder to write code comments that describe the actions taken by your code.

When you are the Coder, you are responsible for reading the instructions / prompts and directing the Computer what to type in the Quarto document. You are responsible for managing the resources your group has available to you (e.g., cheatsheet, textbook). If necessary, you should work with the Computer to debug the code you specified. Once the code runs, you are expected to collaborate with the Computer to write code comments that describe the actions taken by your code.

Here are more details of the Pair Programming Protocols

Group Norms

Remember, your group is expected to adhere to the following norms:

  1. Be curious. Don’t correct.
  2. Be open minded.
  3. Ask questions rather than contribute.
  4. Respect each other.
  5. Allow each teammate to contribute to the activity through their role.
  6. Do not divide the work.
  7. No cross talk with other groups.
  8. Communicate with each other!

Part 0: Accessing the Practice Activity

To access the activity and get it ready to share:

  1. The Computer (partner with next upcoming birthday) should create a GitHub repo called “pa-2-code-breaking”.
  2. To the description add “Peer Activity 2 with NAME1 and NAME 2” and check to add a README file and Create the Repository.
  3. Under Settings, go to Collaborators, the Add people and search for your partner by username or email.
  4. The partner should then log into GitHub and accept the invitation. Now you both should be able to push/pull to the same repository.
  5. The Computer should then create a Version Control R Project on their computer and then download the pa-2-vc-intro-tidy-activity.qmd file into the folder on your computer.
  6. Test your connection by staging, committing, and pushing the new file (.Rproj and .qmd) to the GitHub Repository.

Now you are ready to start! You will switch roles here and the new Computer will need to create a local repository on their own computer of the shared repository before starting.

For each code chunk, add a #| label: and a #comment describing what the code chunk is doing. Be sure to check that all spacing is appropriate as well!

Part 1: Setup

In your activity, each of the following R chunks will cause an error and / or do the desired task incorrectly. The correct code provided below, please copy the correct code into each code chunk and add a label and comment.

  1. Add a setup code chunk to your activity that loads the tidyverse package and hides the code output.
#| label: setup
#| include: false
library(tidyverse)
  1. Create vectors containing the upper case letters, lower case letters, and some punctuation marks. add a label that indicate this code chunk creates the vectors of letters and punctuation.
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(".", ",", "!", "?", "'", '"', "(", ")", " ", "-", ";", ":")

Using the above, we can complete the following 5 steps within a few lines of code using the tidyverse.

  1. Make one long vector containing all the symbols.
  2. Turn the my_symbols vector into a data frame, with one column named “symbol”.
  3. Find the total number of symbols we have in our data frame.
  4. Create a new variable in your data frame that assigns a number to each symbol.

If you want to look at my_symbols click on it under the Environment Tab in the top right panel.

# add comment here

my_symbols <- tibble(symbols = c(lower_case, 
                                 upper_case, 
                                 punctuation)) |> 
              mutate(num = row_number()) 

Part 2: Decoding the secret message.

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

#extracts the code from a dropbox file and pulls out generic first vector X1
top_secret <- read_csv("https://www.dropbox.com/s/k72h1zewk4gtqep/PA_Secret_Code?dl=1", 
                       col_names = FALSE, show_col_types = 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, but replace all of this mess with the tidyverse code below.

  1. Add 14 to every number.
  2. Multiply every number by 18, then subtract 257.
  3. Use the exp() function to exponentiate every number.
  4. Square every number.
#add comment here
#there are several ways to do this, the way below doesn't overwrite any column so you "don't lose work"

code_break <- top_secret |> 
  as_tibble() |> 
  mutate(step6 = top_secret + 14,
         step7 = step6*18 - 257,
         step8 = exp(step7),
         step9 = step8^2)

head(code_break) #see the first few lines here

Checkpoint: Headquarters has informed you that at this stage of decoding, there should be 352 numbers in the secret message that are below 17. Write the code to verify 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.

#add comment here

code_break |> 
  summarize(total = sum(step9 < 17))

Next, carry out the following steps:

  1. Turn your vector of numbers into a matrix with 5 columns.
#add comment here
code_matrix <- code_break |> 
  pull(step9) |> 
  matrix(ncol = 5) |> 
  as_tibble(column_name = "V") |> 
  mutate(id = row_number())
  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.
#add comment here
evens <- seq(from = 2, to = 382, by = 2)
  1. Subtract the “evens” vector from the first column of your secret message matrix.
  2. Subtract 100 from all numbers 18-24th rows of the 3rd column.
  3. Multiply all numbers in the 4th and 5th column by 2.
  4. Turn your matrix back into a vector.
#add comment here
code_vector <- code_matrix |> 
  mutate(V1 = V1 - evens) |> 
  mutate(V3 = if_else(id %in% 18:24, V3 - 100, V3)) |> 
  mutate(V4 = V4*2, 
         V5 = V5*2) |> 
  select(-id) |> 
  pivot_longer(V1:V5, names_to = "column", values_to = "code") |> 
  arrange(column) |> 
  mutate(id = row_number())

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

Hint: Use a relational comparison similar to what you used in the last checkpoint, but here you will need to subset values from your vector!

#code to verify that indices 500 and beyond have values **below** 100
code_vector |> 
  summarize(beyond = sum(if_else(id %in% 500:max(id) >= 100, TRUE, FALSE))) 
  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. Hint: Step 18 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
#add comment here
code_vector_update <- code_vector |> 
  mutate(code = if_else(id %in% 38:465, sqrt(code), code)) |> 
  mutate(code = round(code, digits = 0)) |> 
  mutate(code = if_else(code == 39, 20, code))

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

code_vector_update |> 
  summarize(even = sum(code %% 2 == 0))

Part 3: The secret message!

Use your final vector of numbers as indices for my_symbols to discover the final message, by running the following code:

# add comment here
code_vector_update |> 
  left_join(my_symbols, by = c("code" = "num")) |> 
  pull(symbols) |> 
  stringr::str_c(collapse = "")

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

Part 4: Wrap up

The last step is to make each code chunk hidden, even though it runs by modifying the YAML. Once knit, Export the project and submit with your “quiz” for the week. Note that each person should be submit a copy.