Practical 2

Manipulating vector and dataframes

Authors

Team member 1

Team member 2

Published

January 27, 2025

Download the raw document here

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

Some advice 🤝
  • Work in groups, maximum 2!
  • Google is your friend! If an error is confusing, copy it into Google and see what other people are saying. If you don’t know how to do something, search for it. You can use AI to help you understand code as well, but be critical of the outputs and always double check that it works.
  • Just because there is no error message, it doesn’t mean everything went smoothly. Use the console to check each step and make sure you have accomplished what you wanted to accomplish.
  • See more tips on error handling here.
  • Document your process! Use the empty code chunks set-up for you. If you need more remember the keyboard shortcut is CTRL+ALT+I or CMD+ALT+I.

Part One: Setup

Each of the following R chunks will cause an error 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: 
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 the variable name “Symbol”
my_symbols <- dataframe(my_symbols)
Error in dataframe(my_symbols): could not find function "dataframe"
names(my_symbols) = Symbol
Error: object 'Symbol' not found
  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 (which should NOT have errors) will load up the encoded secret message as a vector:

top_secret <- read.csv(
  "https://raw.githubusercontent.com/loreabad6/app-dev-gis/main/practicals/Practical2/Secret_Code_AppDev", 
  header = TRUE)$x

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

  1. Add 14 to every number (already done for you 😉)
top_secret <- top_secret + 14
  1. Multiply every number by 18, then subtract 257.
# write your solution to prompt 1
  1. Exponentiate every number. (That is, do e^[number].)
# write your solution to prompt 2
  1. Square every number.
# write your solution to prompt 3
Checkpoint

Headquarters has informed you that at this stage of decoding, there should be 1053 numbers in the secret message that are below 17.

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. This is called a Boolean variable type.

# Write code to verify that there are 1053 numbers with values **below** 17
  1. Turn your vector of numbers into a matrix with 5 columns.
# write your solution to prompt 4
  1. Separately from your top_secret numbers, create a new vector called “evens” of all the even numbers between 1 and 428. That is, “evens” should contain 2, 4, 6, 8 …, 428.
# write your solution to prompt 5
  1. Subtract the “evens” vector from the first column of your secret message matrix.
# write your solution to prompt 6
  1. Subtract 100 from all numbers 18-24th rows of the 3rd column.
# write your solution to prompt 7
  1. Multiply all numbers in the 4th and 5th column by 2.
# write your solution to prompt 8
  1. Turn your matrix back into a vector.
# write your solution to prompt 9
Checkpoint

Headquarters has informed you that at this stage of decoding, all numbers in indices 1000 and beyond are below 70.

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! It may be helpful to think of below 100 as not equal to or greater than 70.

# Write code to verify that indices 1000 and beyond have values **below** 70
# should be 0!
  1. Take the square root of all numbers in indices 38 to 465.
# write your solution to prompt 10
  1. Round all numbers to the nearest whole number.
# write your solution to prompt 11
  1. Replace all instances of the number 39 with 20.
# write your solution to prompt 12
Checkpoint

Headquarters has informed you that your final message should have 820 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 820!

Part 3: The secret message!

Use your final vector of numbers as indices for my_symbols to discover the final message!

Hint: to make your solution more legible, checkout the function cat(), you can access the help file by typing ?cat on your console.

# write code to decode the message

Google the message, if you do not recognize it, and find its title and author.

Solution 🎉

Write the title and author here!

Upload the .qmd doc and the rendered html to Blackboard (don’t forget to add all your teammates names!). The first team receives an extra point each in class participation 🏃