Homework 6 Key

Code
knitr::opts_chunk$set(comment = ">")
library(tidyverse)
library(ggrepel)
library(ggthemes)

Answer 1.i

vec_char <- LETTERS[1:10]

Answer 1.ii

vec_fac <- factor(letters[17:26])

Answer 1.iii

rev(vec_fac)
>  [1] z y x w v u t s r q
> Levels: q r s t u v w x y z

Answer 1.iv

An integer, because vectors can only be of one type and factors have an underlying numeric representation (as integers!).

Answer 1.v

You get 1s and 0s when you covert it to numeric. You get "TRUE"s and "FALSE"s when you convert it to character. You get "0"s, and “1”s if you convert it to numeric and then character.

Answer 2.i

matrix_mixed <- matrix(NA, nrow = 10, ncol = 4)
typeof(matrix_mixed)
> [1] "logical"

Answer 2.ii

matrix_mixed[, 1] <- 1:10
typeof(matrix_mixed)
> [1] "integer"

Answer 2.iii

matrix_mixed[, 2] <- rnorm(10)
typeof(matrix_mixed)
> [1] "double"

Answer 2.iv

matrix_mixed[, c(3,4)] <- c(vec_char, vec_fac)
typeof(matrix_mixed)
> [1] "character"

Answer 2.v

NAs are technically a logical indicator of missingness so if an entire matrix is NA, it’s data type will be logical. NAs can be paired with any data type so when the first column was added it automatically became the data type of those values. integers are compatible with doubles so they were coerced to double when the second column was added. All of the columns were coerced to a character string when the character and factor vectors were added since characters are the lowest-common-denominator of data types. This progression is due to the fact that matrices can only be one data type so they will morph into whatever data type can coerce the others.

Answer 2.vi

factors are technically represented by integers that correspond to their level number. Therefore, when vec_fac was coerced into a character string, it was the numbers of the levels that became character strings, not the level names themselves.

Answer 2.vii

Add the argument byrow = TRUE.

Answer 2.viii

Firstly, the vector math_vec is added to each column of math_matrix. For the second part, the first calculation does matrix multiplication, while the second does element-wise multiplication.

math_matrix %*% math_vec
>      [,1]
> [1,]   15
> [2,]   30
> [3,]   45
> [4,]   60
> [5,]   75
math_matrix * math_vec
>      [,1] [,2] [,3] [,4] [,5]
> [1,]    1    1    1    1    1
> [2,]    4    4    4    4    4
> [3,]    9    9    9    9    9
> [4,]   16   16   16   16   16
> [5,]   25   25   25   25   25

Answer 3.i

first_list <- list("letters" = letters, "math_matrix" = math_matrix, "17" = 17, "vec_fac" = vec_fac)

Answer 3.ii

first_list$letters[12:16]
> [1] "l" "m" "n" "o" "p"
first_list[["letters"]][12:16]
> [1] "l" "m" "n" "o" "p"
pluck(first_list, 1)[12:16]
> [1] "l" "m" "n" "o" "p"

Answer 3.iii

second_list <- list("math_vec" = math_vec, "vec_char" = vec_char) 

Answer 3.iv

first_list$second_list <- second_list

Answer 3.v

first_list$second_list$vec_char[1]
> [1] "A"
first_list[["second_list"]][["vec_char"]][1]
> [1] "A"
pluck(first_list, "second_list", "vec_char")[1]
> [1] "A"

Answer 3.vi

lm_output <- lm(mpg ~ wt, data = mtcars)
lm_output
> 
> Call:
> lm(formula = mpg ~ wt, data = mtcars)
> 
> Coefficients:
> (Intercept)           wt  
>      37.285       -5.344
lm_output$model |> dim()
> [1] 32  2

Answer 4.i

df_1 <- data.frame(vec_char = vec_char, math_vec = math_vec)

Answer 4.ii

df_1
>    vec_char math_vec
> 1         A        1
> 2         B        2
> 3         C        3
> 4         D        4
> 5         E        5
> 6         F        1
> 7         G        2
> 8         H        3
> 9         I        4
> 10        J        5

math_vec was repeated twice due to R’s recycling rules for vectors.

Answer 4.iii

df_1$fac_letters <- first_list$vec_fac

Answer 4.iv

names(df_1)
> [1] "vec_char"    "math_vec"    "fac_letters"
colnames(df_1)
> [1] "vec_char"    "math_vec"    "fac_letters"
rownames(df_1)
>  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"
names(first_list)
> [1] "letters"     "math_matrix" "17"          "vec_fac"     "second_list"
colnames(first_list)
> NULL
rownames(first_list)
> NULL
names(matrix_mixed)
> NULL
colnames(matrix_mixed)
> NULL
rownames(matrix_mixed)
> NULL

All of these functions work for a data.frame()! names() and colnames() tell us the column names, while rownames() gives us the numbers between 1 and 10. names() works for lists and returns a vector of the names of each element. colnames() and rownames() don’t work for lists. None of these functions work for the matrix we created1.

Answer 4.v

length() gives number of columns in a data frame, number of elements in a list, number of total cells in a matrix, and length of a vector. dim() gives number of rows/columns for data frames and matrices, but doesn’t work for lists and vectors.

Footnotes

  1. You can, however, assign row names and column names to a matrix using rownames() and columnnames() respectively. Alternatively, if you create a matrix using cbind and named vectors, the colnames will be taken from the names of the vectors used.↩︎