<- matrix(1:5, nrow = 5, ncol = 5)
math_matrix <- 1:5 math_vec
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
LETTERS
contains the 26 capital letters in order. UseLETTERS
and[ ]
to create a vector calledvec_char
of the first 10 capital letters.letters
contains the 26 lowercase letters in order. Usefactor
,letters
, and[ ]
to create a factor variable calledvec_fac
using the last 10 lower case letters.- Use
rev()
to reverse the order ofvec_fac
. - If you used
c()
to combinevec_fac
with 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_mixed
with 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 it’s data type.
- Add 10 random deviates from the normal distribution3 to the second column and get it’s data type.
- Assign
vec_char
andvec_fac
to the third and fourth columns ofmatrix_mixed
using 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 lettersa
tom
in the first column andn
toz
in 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
4?
Question 3: Lists
- Create a list called
first_list
that containsletters
,math_matrix
, the number17
, andvec_fac
(in that order) and assign them their vector names. - Index
first_list
to pull out just the letters"l" "m" "n" "o" "p"
. - Create another list called
second_list
and putmath_vec
andvec_char
as named elements in it. - Add
second_list
as the fifth element offirst_list
. - Index into
first_list
and pull out the capital A fromvec_char
. - Run the following code:
<- lm(mpg ~ wt, data = mtcars)
lm_output 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_vec
in the second column? Why? - Use
$
to addvec_fac
fromfirst_list
todf_1
and 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 | 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
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 ---
Use
typeof()
to find this.↩︎Using
rnorm()
.↩︎Run all three calculations for this problem in your console, no need to include them in your qmd↩︎