Question 1

Assign to the variable ‘n_dims’ a single random integer between 3 and 10.

# generate random integer between 3 and 10
n_dims <- sample(3:10, size = 1)

# generate vector from 1 to n_dims^2
vec <- (1:n_dims^2)
print(vec)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# reshuffle vector
vec <- sample(vec)
length(vec) # length = 16, therefore 4 x 4 matrix
## [1] 25
# even better: populate with n_dims in order to avoid errors

# turn vector into a square matrix
m <- matrix(data = vec, nrow = n_dims)
print(m)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   10    5   16    8   20
## [2,]   11   25   15    9    2
## [3,]   19   12    1    7   18
## [4,]   21    4    3   17   22
## [5,]   24    6   23   14   13
# transpose matrix
m <- t(m)
print(m)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   10   11   19   21   24
## [2,]    5   25   12    4    6
## [3,]   16   15    1    3   23
## [4,]    8    9    7   17   14
## [5,]   20    2   18   22   13
  # order of rows and columns have changed

# calculate the sum and the mean of the elements in the first row and then the last row.
sum_r1 <- sum(m[1,])
mean_r1 <- mean(m[1,])

sum_r4 <- sum(m[4,])
mean_r4 <- mean(m[4,])

# read about the eigen() function and use it on your matrix
# Computes eigenvalues and eigenvectors of numeric (double, integer, logical) or complex matrices.
# an eigen vector is a vector associated with a set of linear equations
# an eigen value is the factor by which an eigen vector is squished
# these functions can only be used with square matrices

eigen_m <- eigen(m)
print(eigen_m)
## eigen() decomposition
## $values
## [1]  65.421051  20.383392 -17.070470 -10.011788   7.277815
## 
## $vectors
##            [,1]        [,2]        [,3]        [,4]       [,5]
## [1,] -0.5690302 -0.19964825  0.24599989  0.79161144 -0.2148128
## [2,] -0.3080851  0.87280608  0.16645709  0.09936043  0.3686910
## [3,] -0.4172747 -0.02069582 -0.85876638 -0.43143138 -0.5180994
## [4,] -0.3632771 -0.02988438 -0.09257509  0.06026773  0.6688115
## [5,] -0.5245949 -0.44387918  0.40709490 -0.41678963 -0.3196664
# look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?
# $values numbers are unreal numbers (they end with i)
# $vectors are also small unreal numbers

typeof(eigen_m$values)
## [1] "double"
# outputs complex numbers
typeof(eigen_m$vectors)
## [1] "double"
# again, outputs complex

Question 2.

Create a list with the following named elements:

# my_matrix, which is a 4 x 4 matrix filled with random uniform values
random_nums <- runif(16)
my_matrix <- matrix(data = random_nums, nrow = 4)

# my_logical which is a 100-element vector of TRUE or FALSE values. Do this efficiently by setting up a vector of random values and then applying an inequality to it.
my_vec <- runif(100)
my_logical <- my_vec > runif(1)

# my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters[1:26])

# Then, complete the following steps:
# create a new list, which has the element[2,2] from the matrix, the second element of the logical vector, and the second element of the letters vector.
new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])

# use the typeof() function to confirm the underlying data types of each component in this list
typeof(new_list)
## [1] "list"
str(new_list)
## List of 3
##  $ : num 0.323
##  $ : logi TRUE
##  $ : chr "v"
# outputs type is list. str() gives number, logical, and character

# combine the underlying elements from the new list into a single atomic vector with the c() function.
vec_list <- c(my_matrix[2,2], my_logical[2], my_letters[2])

# what is the data type of this vector?
typeof(vec_list)
## [1] "character"
str(vec_list)
##  chr [1:3] "0.32292015873827" "TRUE" "v"
# r coerces vector into characters

Question 3

Create a data frame with the two variables (= columns) and 26 cases (= rows) below:

# call the first variable my_unis and fill it with 26 random uniform values from 0 to 10
my_unis <- runif(26, 0, 10)

# call the second variable my_letters and fill it with 26 capital letters in random order.
my_letters <- sample(LETTERS[1:26])

dat <- data.frame(my_unis, my_letters)
print(dat)
##      my_unis my_letters
## 1  1.1522146          C
## 2  8.2008660          I
## 3  2.2225648          P
## 4  5.1080171          U
## 5  4.3194782          J
## 6  6.8093590          O
## 7  9.9204941          G
## 8  2.9072497          M
## 9  0.4328099          H
## 10 7.2046386          V
## 11 8.8033642          S
## 12 5.0032932          N
## 13 7.4222484          A
## 14 6.4164776          E
## 15 8.5919918          W
## 16 3.3733168          F
## 17 5.4141033          Z
## 18 4.5486523          K
## 19 9.1727826          T
## 20 3.5823671          Q
## 21 6.2165137          Y
## 22 4.3976318          B
## 23 9.2931086          L
## 24 4.6005944          R
## 25 8.1190920          D
## 26 6.3145966          X
# creates data frame with two variables

# for the first variable, use a single line of code in R to select 4 random rows and replace the numerical values in those rows with NA.
rows <- sample(length(my_unis), size = 4)
dat[rows,1] <- NA

# for the first variable, write a single line of R code to identify which rows have the missing values.
which(is.na(dat$my_unis))
## [1]  6  8 12 15
# re-order the entire data frame to arrange the second variable in alphabetical order
dat <- dat[order(dat$my_letters),]
print(dat)
##      my_unis my_letters
## 13 7.4222484          A
## 22 4.3976318          B
## 1  1.1522146          C
## 25 8.1190920          D
## 14 6.4164776          E
## 16 3.3733168          F
## 7  9.9204941          G
## 9  0.4328099          H
## 2  8.2008660          I
## 5  4.3194782          J
## 18 4.5486523          K
## 23 9.2931086          L
## 8         NA          M
## 12        NA          N
## 6         NA          O
## 3  2.2225648          P
## 20 3.5823671          Q
## 24 4.6005944          R
## 11 8.8033642          S
## 19 9.1727826          T
## 4  5.1080171          U
## 10 7.2046386          V
## 15        NA          W
## 26 6.3145966          X
## 21 6.2165137          Y
## 17 5.4141033          Z
# calculate the column mean for the first variable.
mean(dat$my_unis, na.rm = TRUE)
## [1] 5.737997