Question 1

# Assign to the variable n_dims a single random integer between 3 and 10.
n_dims <- sample(3:10,1)
print(n_dims)
## [1] 9
# Create a vector of consecutive integers from 1 to n_dims^2.
my_vec <- 1:n_dims^2

# Use the sample function to randomly reshuffle these values.
my_vec <- sample(my_vec)
print(my_vec)
##  [1] 35  4 44 16 56 10 79 81 57 19 32 23 74 12 69 31 78 37 18 73  3 22 75 80 59
## [26] 58 17  2 64 24 28 51 49 25 61 34 76 55 60 48 63 46 53 27 77 52 11 62 36 29
## [51] 45 66 72 68 67 47 21 13 39 20 26 54 41 14  6 65 50 42 33 30 70 38  8 71  9
## [76] 15 43  5  1 40  7
# Create a square matrix with these elements
my_square <- matrix(data=(my_vec^2),nrow= n_dims, ncol=n_dims)
# and print matrix
print(my_square)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,] 1225  361  324    4 5776 2704 4489  196   64
##  [2,]   16 1024 5329 4096 3025  121 2209   36 5041
##  [3,] 1936  529    9  576 3600 3844  441 4225   81
##  [4,]  256 5476  484  784 2304 1296  169 2500  225
##  [5,] 3136  144 5625 2601 3969  841 1521 1764 1849
##  [6,]  100 4761 6400 2401 2116 2025  400 1089   25
##  [7,] 6241  961 3481  625 2809 4356  676  900    1
##  [8,] 6561 6084 3364 3721  729 5184 2916 4900 1600
##  [9,] 3249 1369  289 1156 5929 4624 1681 1444   49
# Find a function in R to transpose the matrix
my_matrix <- t(my_square)
# print it out again and not how it has changed
print(my_matrix)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
##  [1,] 1225   16 1936  256 3136  100 6241 6561 3249
##  [2,]  361 1024  529 5476  144 4761  961 6084 1369
##  [3,]  324 5329    9  484 5625 6400 3481 3364  289
##  [4,]    4 4096  576  784 2601 2401  625 3721 1156
##  [5,] 5776 3025 3600 2304 3969 2116 2809  729 5929
##  [6,] 2704  121 3844 1296  841 2025 4356 5184 4624
##  [7,] 4489 2209  441  169 1521  400  676 2916 1681
##  [8,]  196   36 4225 2500 1764 1089  900 4900 1444
##  [9,]   64 5041   81  225 1849   25    1 1600   49
# It work, it transposed the matrix!

# Calculate the sum and the means of the elements in the first row and then the last row
my_sum_f <- sum(my_matrix[1,])
print(my_sum_f)
## [1] 22720
my_mean_f <- mean(my_matrix[1,])
print(my_mean_f)
## [1] 2524.444
my_sum_l <- sum(my_matrix[length(n_dims),])
print(my_sum_l)
## [1] 22720
my_mean_l <- mean(my_matrix[length(n_dims),])
print(my_mean_l)
## [1] 2524.444
# Read about the eigen() function and use it on your matrix 
eigen_matrix <- eigen(my_matrix)
print(eigen_matrix)
## eigen() decomposition
## $values
## [1] 19844.455+   0.000i -5963.134+   0.000i  1284.944+3735.881i
## [4]  1284.944-3735.881i -2984.477+2033.212i -2984.477-2033.212i
## [7]  3196.602+1276.192i  3196.602-1276.192i -2214.458+   0.000i
## 
## $vectors
##                [,1]           [,2]                    [,3]
##  [1,] -0.3374539+0i  0.23339550+0i -0.18866408-0.10605857i
##  [2,] -0.3085932+0i  0.13986584+0i -0.33633414+0.22516307i
##  [3,] -0.4415847+0i  0.72039933+0i  0.27736248+0.07344453i
##  [4,] -0.2646210+0i  0.25299946+0i -0.00593569+0.07282964i
##  [5,] -0.4803840+0i -0.44764157+0i  0.60078402+0.00000000i
##  [6,] -0.3697507+0i -0.27310919+0i -0.04143706+0.09986092i
##  [7,] -0.2299261+0i -0.04744670+0i -0.22875024+0.18323319i
##  [8,] -0.2865558+0i -0.24783623+0i -0.11830932-0.41984158i
##  [9,] -0.1530006+0i  0.06583754+0i  0.05502700+0.22327380i
##                          [,4]                     [,5]                     [,6]
##  [1,] -0.18866408+0.10605857i -0.19728299+0.178485653i -0.19728299-0.178485653i
##  [2,] -0.33633414-0.22516307i  0.31852146+0.201274684i  0.31852146-0.201274684i
##  [3,]  0.27736248-0.07344453i  0.52982304+0.000000000i  0.52982304+0.000000000i
##  [4,] -0.00593569-0.07282964i  0.18536061-0.007757309i  0.18536061+0.007757309i
##  [5,]  0.60078402+0.00000000i -0.27005483-0.070715102i -0.27005483+0.070715102i
##  [6,] -0.04143706-0.09986092i -0.32382136+0.060922033i -0.32382136-0.060922033i
##  [7,] -0.22875024-0.18323319i  0.33925681+0.022427321i  0.33925681-0.022427321i
##  [8,] -0.11830932+0.41984158i -0.26406280-0.008536903i -0.26406280+0.008536903i
##  [9,]  0.05502700-0.22327380i -0.03577675-0.314549688i -0.03577675+0.314549688i
##                          [,7]                    [,8]          [,9]
##  [1,] -0.38521839-0.14868749i -0.38521839+0.14868749i  0.1122273+0i
##  [2,]  0.32812995+0.22629281i  0.32812995-0.22629281i -0.2955412+0i
##  [3,] -0.22957920+0.06566572i -0.22957920-0.06566572i -0.4723249+0i
##  [4,]  0.33645480+0.03824555i  0.33645480-0.03824555i -0.3421512+0i
##  [5,] -0.01051847-0.33247301i -0.01051847+0.33247301i  0.2893754+0i
##  [6,] -0.26772060+0.08888306i -0.26772060-0.08888306i  0.2785243+0i
##  [7,] -0.22439590-0.05056777i -0.22439590+0.05056777i -0.4948972+0i
##  [8,] -0.05724408+0.06462965i -0.05724408-0.06462965i  0.2949452+0i
##  [9,]  0.49834728+0.00000000i  0.49834728+0.00000000i  0.2582099+0i
# Look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?
typeof(eigen_matrix)
## [1] "list"
# They are decimal(imaginary) numbers. It's a list!

# If the code is set up properly, you should be able to re-run it and create a matrix of different sizes because n_dims will change. 

Question 2

# Create a list with the following named elements:

# my_matrix, which is a 4 x 4 matrix filled with random uniform values
my_matrix <- matrix(data=runif(4),nrow=4, ncol=4)
print(my_matrix)
##            [,1]       [,2]       [,3]       [,4]
## [1,] 0.68338604 0.68338604 0.68338604 0.68338604
## [2,] 0.17099994 0.17099994 0.17099994 0.17099994
## [3,] 0.06096987 0.06096987 0.06096987 0.06096987
## [4,] 0.86764981 0.86764981 0.86764981 0.86764981
# 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_logical <- runif(100) < 0.5
print(my_logical)
##   [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE
##  [13] FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE
##  [25] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE
##  [37]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE
##  [49] FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE
##  [61] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
##  [73]  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
##  [85]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
##  [97] FALSE FALSE FALSE  TRUE
# my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters, 26)
print(my_letters)
##  [1] "o" "v" "t" "n" "x" "u" "f" "h" "z" "c" "m" "j" "s" "g" "p" "l" "d" "i" "r"
## [20] "q" "y" "w" "e" "k" "b" "a"
# 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
group_list <- list(my_matrix[2,2], my_logical[2],my_letters[2])
print(group_list)
## [[1]]
## [1] 0.1709999
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] "v"
# use the typeof() function to confirm the underlying data types of each component in this list.
typeof(my_matrix)
## [1] "double"
typeof(my_logical)
## [1] "logical"
typeof(my_letters)
## [1] "character"
# combine the underlying elements from the new list into a single atomic with the c() function
group_con <- c(my_matrix[2,2], my_logical[2],my_letters[2])
print(group_con)
## [1] "0.170999940251932" "TRUE"              "v"
# What is the data type of this vector?
typeof(group_con)
## [1] "character"
# It's character!

Question 3

# Create a data frame with the two variables (=columns) and 26 cases (=rows) below:
my_unis_10<- sample(0:10)
my_unis_20<- sample(0:10)
my_unis_26<- sample(0:10, 4)
my_unis <- c(my_unis_10,my_unis_20,my_unis_26)
print(my_unis)
##  [1]  7  9  1  4  2  3 10  5  6  0  8  1  8  9  5  2  4  7  6 10  3  0  5  0 10
## [26]  3
# Call the second variable my_letters and fill it with capital letters in random order.
my_letters <- sample(LETTERS, 26)
print(my_letters)
##  [1] "X" "U" "Q" "P" "K" "A" "G" "J" "W" "M" "E" "Z" "N" "L" "Y" "I" "H" "D" "R"
## [20] "F" "V" "O" "S" "C" "B" "T"
df <- data.frame(my_unis,my_letters)
print(df)
##    my_unis my_letters
## 1        7          X
## 2        9          U
## 3        1          Q
## 4        4          P
## 5        2          K
## 6        3          A
## 7       10          G
## 8        5          J
## 9        6          W
## 10       0          M
## 11       8          E
## 12       1          Z
## 13       8          N
## 14       9          L
## 15       5          Y
## 16       2          I
## 17       4          H
## 18       7          D
## 19       6          R
## 20      10          F
## 21       3          V
## 22       0          O
## 23       5          S
## 24       0          C
## 25      10          B
## 26       3          T
# For the first variable, use a single line of code to select 4 random rows and replace the numerical values in those rows with NA
df$my_unis[sample(0:nrow(df), 4)] <- NA
print(df)
##    my_unis my_letters
## 1        7          X
## 2        9          U
## 3        1          Q
## 4        4          P
## 5        2          K
## 6        3          A
## 7       10          G
## 8       NA          J
## 9        6          W
## 10       0          M
## 11       8          E
## 12       1          Z
## 13       8          N
## 14       9          L
## 15       5          Y
## 16      NA          I
## 17       4          H
## 18       7          D
## 19       6          R
## 20      10          F
## 21       3          V
## 22       0          O
## 23       5          S
## 24       0          C
## 25      NA          B
## 26      NA          T
# For the first variable, write a single line to identify which rows have the missing values. 
which(!complete.cases(df$my_unis))
## [1]  8 16 25 26
# re-order the entire data frame to arrange the second variable in alphabetical order
df_letters <- df[order(df$my_letters),]
print(df_letters)
##    my_unis my_letters
## 6        3          A
## 25      NA          B
## 24       0          C
## 18       7          D
## 11       8          E
## 20      10          F
## 7       10          G
## 17       4          H
## 16      NA          I
## 8       NA          J
## 5        2          K
## 14       9          L
## 10       0          M
## 13       8          N
## 22       0          O
## 4        4          P
## 3        1          Q
## 19       6          R
## 23       5          S
## 26      NA          T
## 2        9          U
## 21       3          V
## 9        6          W
## 1        7          X
## 15       5          Y
## 12       1          Z
# calculate the column mean for the first variable 
unis_means <- mean(df$my_unis, na.rm = TRUE)
print(unis_means)
## [1] 4.909091