math_matrix <- matrix(1:5, nrow = 5, ncol = 5)
math_vec <- 1:5Homework 6
Key
Click link above for example solutions to this assignment.
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
LETTERScontains 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?
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 contain2? - Add the numbers 1 through 10 to the first column of this empty matrix and get its data type.
- Add 10 random deviates from the normal distribution3 to the second column and get its 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:
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_vec? Run these three calculations in your console; no need to include them in your qmd, just answer the questions about them in text.
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?