Practical 1
Vector, matrices, dataframes & functions
Download the raw document here.
Here is a bonus if you already finished: raw document here.
Today you will be creating and manipulating vectors, and data frames to uncover a top secret message.
- 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
orCMD+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.
- Create vectors containing the upper case letters, lower case letters, and some punctuation marks.
<- 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")
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(".", ",", "!", "?", "'", """, "(", ")", " ", ":", ";", "—") punctuation
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"
^
- Make one long vector containing all the symbols.
<- cbind(lower_case, upper_case, punctuation) my_symbols
Error: object 'lower_case' not found
- Turn the
my_symbols
vector into a data frame, with the variable name “Symbol”
<- dataframe(my_symbols) my_symbols
Error in dataframe(my_symbols): could not find function "dataframe"
names(my_symbols) = Symbol
Error: object 'Symbol' not found
- Find the total number of symbols we have in our data frame.
<- length(my_symbols) len
Error: object 'my_symbols' not found
- Create a new variable in your dataframe that assigns a number to each symbol.
<- 1:len my_symbols%Num
Error in parse(text = input): <text>:1:11: unexpected input
1: my_symbols%Num <- 1:len
^
Part Two: Creating functions
Write your own function which includes the steps below.
- Add 14 to every number (already done for you 😉)
- Multiply every number by 18, then subtract 257.
- Exponentiate every number. (That is, do e^[number].)
- Square every number.
= function(x) {
arithmetics # step 0
= x + 14
x # step 1
= ... # write your solution to prompt 1
x # step 2
= ... # write your solution to prompt 2
x # step 3
= ... # write your solution to prompt 3
x # print output
x }
Test your function, do you get any errors when applying it?
= seq(0, 1, length.out = 5)
test arithmetics(test)
Error in arithmetics(test): '...' used in an incorrect context
Part Three: Decoding the secret message
This chunk (which should NOT have errors) will load up the encoded secret message as a vector:
<- read.csv(
top_secret "https://raw.githubusercontent.com/loreabad6/app-dev-gis/main/practicals/Practical1/Secret_Code_AppDev",
header = TRUE)$x
By altering this top secret set of numbers, you will be able to create a message. Use your function above to alter the top_secret
object.
# apply your function to the top_secret object and overwrite top_secret
= ... top_secret
Error: '...' used in an incorrect context
Headquarters has informed you that at this stage of decoding, there should be 552 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 TRUE
s and FALSE
s based on whether the comparison is / is not met. You can then use these TRUE
s and FALSE
s 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 552 numbers with values **below** 17
- Turn your vector of numbers into a matrix with 5 columns, fill the values by column and not by row.
# write your solution to prompt 4
- Separately from your
top_secret
numbers, create a new vector called “evens” of all the even numbers between 1 and 552. That is, “evens” should contain 2, 4, 6, 8 …, 552.
# write your solution to prompt 5
- Subtract the “evens” vector from the first column of your secret message matrix.
# write your solution to prompt 6
- Subtract 100 from all numbers 18-24th rows of the 3rd column.
# write your solution to prompt 7
- Multiply all numbers in the 4th and 5th column by 2.
# write your solution to prompt 8
- Turn your matrix back into a vector.
# write your solution to prompt 9
Headquarters has informed you that at this stage of decoding, all numbers in indices 500 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 as not equal to or smaller than 70.
# Write code to verify that indices 500 and beyond have values **below** 70
- Take the square root of all numbers in indices 38 to 465.
# write your solution to prompt 10
- Round all numbers to the nearest whole number.
# write your solution to prompt 11
- Replace all instances of the number 39 with 20.
# write your solution to prompt 12
Headquarters has informed you that your final message should have 507 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 507!
Part Four: The secret message!
Use your final vector of numbers as indices for my_symbols
to discover the final message! The code to do so is already there for you:
cat(my_symbols$Symbol[top_secret], sep = "")
Error: object 'my_symbols' not found
Google the message, if you do not recognize it, and find its title and author.
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 🏃