Have you completed the Stat 133 Welcome Survey linked on Ed?
Consider the file system below where the root is /. The shapes following the names aren’t part of the file system; they’re pointers used in the questions below.
Fill in the blank _______ with the full command the will generate the output that follows. Let $ be the character that indicates where you can type at the command line. The working directory at the start of each question is /, the root.
Every directory and file name that you’ve seen so far in this class has refrained from using special characters. Which characters should you avoid using when naming directories and files?1
Intro to R
Have you installed R on your computer? Do you have access to the command line via a shell? This will likely be Terminal on a Mac (which is built-in) or git bash on Windows. See the Tools section of the course website for guidance.
You’re encouraged to work on these at your R console to try things out. The next three questions continue with the same R objects. What output will each code block produce?
What will the following code produce?
a <-3+5b <-4/2a + b
What will the following code produce?
a <- b - (a / a)
What will the following code produce?
a
What will the following code produce?
log(a)
For the following questions, consult the functions help file by using ? (e.g. ?round).
What modified version of this function would return the value 3.14 instead of 3?
round(3.141592)
[1] 3
What will the following code produce?
ceiling(3.14) +floor(3.14)
R on the File System
Assume you start a new R session with your working directory at ~. Which R functions would you call to do each of the following?
Read the data in raw-data-1.txt and save it to the R object named raw_data_1.
Without changing your working directory, read the data in raw-data-2.txt and save it to the R object named raw_data_2.
You may have noticed that when creating R objects, I avoid certain types of names. What characters are permissable when naming new objects?2
Atomic Vectors
If you recorded data on peoples’ answers to the following questions and read each one into R as a vector, what data type could be used to store them?
How much do you spend on average per week, in dollars and cents, on caffeinated beverages?
Did you vote in the last US Election?
How many US states have you visited?
While each of the following vectors could be created by simply using c(), instead write a command that can generate each one more efficiently using the functions from class. Read more about them by pulling up their help files with ?.
What single command can be run on each of these vectors
x <-c("A", "B", "C")x <-seq(1, 5, 1)x <-c(TRUE, FALSE)
and produce this output?
[1] "A" "B" "B" "C" "C" "C"
[1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
[1] TRUE FALSE FALSE
Predict the output of the following code.
1:3+1:2+1:1
Predict the output of the following code.
1:1+1:2+1:3
Which vector results from running the following code? Does it throw a message, warning, or error and why?
c(5, 5, 5) *c(2, 1, NULL)
Which vector results from running the following code? Does it throw a message, warning, or error and why?
c(5, 5, 5) *c(2, 1, NA)
Predict the output and type of the following three vectors.
c(1, FALSE)c("a", 1)c(TRUE, 1L)
Use square bracket subsetting to create the following vector from z <- c(apple = 2, banana = 4, cherry = 8).
cherry banana apple
8 4 2
Write down three subsetting methods (index, name, and logical) to generate the following vector from z. If you’re not able to do it in one subsetting operation, say why not.
cherry cherry
8 8
What happens when you subset a vector with a negative number?
Extending Vectors
Provide the code that will create a matrix object of type double with 3 rows and 4 columns. The entries should be the integers between 1 and 12 arrayed rowwise. Name the object mat_twelve.
Subset the matrix to isolate the element with the value 5.
Provide the code that will break the mat_twelve into two separate matrices: mat_left with the 6 leftmost entries, and mat_right with the remaining six.
What does mat_left + mat_right evaluate to?
You can load the “like” matrix into R using the following code.
Provide the code to extract the vector of the ranks that Basil gave other monks from 0 (unranked) to 3 (most liked).
Provide the code to extract the vector of the ranks that other monks gave Basil from 0 (unranked) to 3 (most liked).
Matrices make it easy and fast to apply the same function across all rows or columns. Use rowSums() or colSum() and the sort function to return the three most like monks and the three least liked / ranked monks.
Footnotes
You can research the answer to this question online.↩︎
You can research the answer to this question online.↩︎