---
title: "My Document"
execute:
echo: true
---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).
1 You can make this a global option for your whole document by putting it directly in the YAML of your qmd:
Exercises
Question 1: Vectors
LETTERS2 contains the 26 capital letters in order. UseLETTERSand[ ]to create a vector calledvec_charof the first 10 capital letters.letterscontains the 26 lowercase letters in order. Usefactor,letters, and[ ]to create a factor variable calledvec_facusing the last 10 lower case letters.- Use
rev()to reverse the order ofvec_fac. - If you used
c()to combinevec_facwith a vector of integers, what class of vector would you get? Why? - 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?
2 LETTERS and letters are built-in vectors in R that you can use without loading any external packages.
Question 2: Matrices
- Use
matrix()to create a matrix calledmatrix_mixedwith 10 rows and four columns filled with NA. What data type does this matrix contain3? - Add the numbers 1 through 10 to the first column of this empty matrix and get it’s data type.
- Add 10 random deviates from the normal distribution4 to the second column and get it’s data type.
- Assign
vec_charandvec_facto the third and fourth columns ofmatrix_mixedusing one assignment operator. What is the data type of the matrix now? - Explain this progression of data types from part i to part iv.
- Look at
matrix_mixed. What happened to the letters in column 4? - Run this code in your console:
matrix(letters, ncol = 2). It consists of lettersatomin the first column andntozin the second column. How can you change this code to make it go in alphabetical order left to right, top to bottom instead? - Consider the code below:
3 Use typeof() to find this.
4 Using rnorm().
math_matrix <- matrix(1:5, nrow = 5, ncol = 5)
math_vec <- 1:5What 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_vec5?
5 Run all three calculations for this problem in your console, no need to include them in your qmd
Question 3: Lists
- Create a list called
first_listthat containsletters,math_matrix, the number17, andvec_fac(in that order) and assign them their vector names. - Index
first_listto pull out just the letters"l" "m" "n" "o" "p". - Create another list called
second_listand putmath_vecandvec_charas named elements in it. - Add
second_listas the fifth element offirst_list. - Index into
first_listand pull out the capital A fromvec_char. - 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
- Use
data.frame()to combinevec_char(first column) andmath_vec(second column) intodf_1. - Look at
df_1. What happened withmath_vecin the second column? Why? - Use
$to addvec_facfromfirst_listtodf_1and call itfac_letters. - Use
names(),colnames(), andrownames()ondf_1.How does this compare to the behavior of these functions on lists and matrices? - Similarly, how do the results of
length()anddim()differ between data frames, lists, matrices, and vectors?
Due Dates
| # | Homework Due | Peer Review Due |
|---|---|---|
| 1 | 7 October | 12 October |
| 2 | 14 October | 19 October |
| 3 | 21 October | 26 October |
| 4 | 28 October | 2 November |
| 5 | 11 November | 16 November |
| 6 | 18 November1 | 23 November |
| 7 | 25 November | 30 November |
| 8 | 2 December | 7 December |
| 9 | 9 December | 14 December |
| 1 We will cover content for Homework 6 two weeks prior to its due date | ||