Homework 6

Key

Click link above for answers to homework 6.

Instructions

Answer each of the following questions. Be sure to display all your code in the rendered version (use echo: true throughout1).

Exercises

Question 1: Vectors

  1. LETTERS contains the 26 capital letters in order. Use LETTERS and [ ] to create a vector called vec_char of the first 10 capital letters.
  2. letters contains the 26 lowercase letters in order. Use factor, letters, and [ ] to create a factor variable called vec_fac using the last 10 lower case letters.
  3. Use rev() to reverse the order of vec_fac.
  4. If you used c() to combine vec_fac with a vector of integers, what class of vector would you get? Why?
  5. Consider the vector c(TRUE, FALSE, TRUE, TRUE). In words, what happens to its values when you try to convert it to numeric? To character? To numeric and then character?

Question 2: Matrices

  1. Use matrix() to create a matrix called matrix_mixed with 10 rows and four columns filled with NA. What data type does this matrix contain2?
  2. Add the numbers 1 through 10 to the first column of this empty matrix and get it’s data type.
  3. Add 10 random deviates from the normal distribution3 to the second column and get it’s data type.
  4. Assign vec_char and vec_fac to the third and fourth columns of matrix_mixed using one assignment operator. What is the data type of the matrix now?
  5. Explain this progression of data types from part i to part iv.
  6. Look at matrix_mixed. What happened to the letters in column 4?
  7. Run this code in your console: matrix(letters, ncol = 2). It consists of letters a to m in the first column and n to z in the second column. How can you change this code to make it go in alphabetical order left to right, top to bottom instead?
  8. Consider the code below:
math_matrix <- matrix(1:5, nrow = 5, ncol = 5)
math_vec <- 1:5

What happens when you add math_matrix and math_vec to one another? What’s the difference between the results of math_matrix %*% math_vec and math_matrix * math_vec4?

Question 3: Lists

  1. Create a list called first_list that contains letters, math_matrix, the number 17, and vec_fac (in that order) and assign them their vector names.
  2. Index first_list to pull out just the letters "l" "m" "n" "o" "p".
  3. Create another list called second_list and put math_vec and vec_char as named elements in it.
  4. Add second_list as the fifth element of first_list.
  5. Index into first_list and pull out the capital A from vec_char.
  6. Run the following code:
lm_output <- lm(mpg ~ wt, data = mtcars)
lm_output

Call:
lm(formula = mpg ~ wt, data = mtcars)

Coefficients:
(Intercept)           wt  
     37.285       -5.344  

How many elements does lm_output have and what are the dimensions of the model element?

Question 4: Data Frames

  1. Use data.frame() to combine vec_char (first column) and math_vec (second column) into df_1.
  2. Look at df_1. What happened with math_vec in the second column? Why?
  3. Use $ to add vec_fac from first_list to df_1 and call it fac_letters.
  4. Use names(), colnames(), and rownames() on df_1. How does this compare to the behavior of these functions on lists and matrices?
  5. Similarly, how do the results of length() and dim() differ between data frames, lists, matrices, and vectors?

Due Dates

# Homework Due Peer Review Due
1 2 April 7 April
2 9 April 14 April
3 16 April 21 April
4 23 April 28 April
5 30 April 5 May
6 7 May 12 May
7 14 May 19 May
8 21 May 26 May
9 28 May 2 June

Footnotes

  1. You can make this a global option for your whole document by putting it directly in the YAML of your qmd:

    ---
    title: "My Document"
    execute:
      echo: true
    ---
    ↩︎
  2. Use typeof() to find this.↩︎

  3. Using rnorm().↩︎

  4. Run all three calculations for this problem in your console, no need to include them in your qmd↩︎