Question 1

# assign to the variable a single random integer between 3 and 10 using sample function
n_dims <- sample(3:10, 1)
print(n_dims)
## [1] 7
# create a vector of consecutive integers 1:n_dims^2
my_vec <- 1:n_dims^2
print(my_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
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# use the sample matrix to reshuffle these values
my_vec <- sample(my_vec)
print(my_vec)
##  [1] 38 12 39 16  8 13 44 35 40  5 29 32 21 37 33 34 10 22 30 46  6 27  3 17 19
## [26] 31 49 28 11 45 48 23 18 26  2  9 24 15 47  4 42 14 43  7 25 36 41 20  1
# create a square matrix with these elements and print matrix
my_matrix <- matrix(data=(my_vec^2), nrow = n_dims, ncol = n_dims)
print(my_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1444 1225 1089  729  121   81 1849
## [2,]  144 1600 1156    9 2025  576   49
## [3,] 1521   25  100  289 2304  225  625
## [4,]  256  841  484  361  529 2209 1296
## [5,]   64 1024  900  961  324   16 1681
## [6,]  169  441 2116 2401  676 1764  400
## [7,] 1936 1369   36  784    4  196    1
# find a function in r to transpose the matrix and print again (it worked! it transposed the matrix!)
transpose_matrix <- t(my_matrix)
print(transpose_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1444  144 1521  256   64  169 1936
## [2,] 1225 1600   25  841 1024  441 1369
## [3,] 1089 1156  100  484  900 2116   36
## [4,]  729    9  289  361  961 2401  784
## [5,]  121 2025 2304  529  324  676    4
## [6,]   81  576  225 2209   16 1764  196
## [7,] 1849   49  625 1296 1681  400    1
# calculate the sum and the mean of the elements in the first row and then the last row
sum_first_row <- sum(transpose_matrix[1, ])
print(sum_first_row)
## [1] 5534
mean_first_row <- mean(transpose_matrix[1,])
print(mean_first_row)
## [1] 790.5714
sum_last_row <- sum(transpose_matrix[n_dims, ])
print(sum_last_row)
## [1] 5901
mean_last_row <- mean(transpose_matrix[n_dims, ])
print(mean_last_row)
## [1] 843
# use eigen() function on matrix
eigen_matrix <- eigen(transpose_matrix)
print(eigen_matrix)
## eigen() decomposition
## $values
## [1]  5689.7682+   0.000i  2353.1760+   0.000i -2232.5591+   0.000i
## [4] -1690.3199+   0.000i   133.1827+1390.709i   133.1827-1390.709i
## [7]  1207.5695+   0.000i
## 
## $vectors
##               [,1]           [,2]           [,3]           [,4]
## [1,] -0.3643700+0i -0.30854346+0i -0.43714628+0i -0.43235666+0i
## [2,] -0.4458902+0i -0.58771478+0i  0.06313325+0i -0.06335247+0i
## [3,] -0.3773577+0i  0.01687076+0i  0.48609265+0i  0.05383069+0i
## [4,] -0.3398735+0i  0.33158140+0i  0.31981039+0i -0.48199889+0i
## [5,] -0.4109542+0i -0.31356644+0i -0.47504905+0i  0.05747409+0i
## [6,] -0.3066668+0i  0.55870513+0i -0.22387937+0i  0.28548367+0i
## [7,] -0.3841559+0i -0.19668778+0i  0.43653064+0i  0.69930631+0i
##                          [,5]                     [,6]           [,7]
## [1,] -0.32989375-0.001332951i -0.32989375+0.001332951i  0.61648605+0i
## [2,] -0.32225589+0.152324270i -0.32225589-0.152324270i -0.32429078+0i
## [3,]  0.26582122+0.243036529i  0.26582122-0.243036529i -0.06158607+0i
## [4,]  0.03162805-0.186105604i  0.03162805+0.186105604i  0.04345103+0i
## [5,]  0.62318174+0.000000000i  0.62318174+0.000000000i -0.70383909+0i
## [6,] -0.08239544+0.145811440i -0.08239544-0.145811440i  0.11700280+0i
## [7,]  0.02185918-0.426462423i  0.02185918+0.426462423i  0.00452631+0i
# $values and $vectors- what kinds of numbers are these?
##### outputs are complex numbers for square matrices

#figure out their data type
typeof(eigen_matrix)
## [1] "list"

Question 2

# my_matrix: 4x4 matrix with random uniform values between 0 and 1
my_matrix = matrix(runif(16), nrow = 4, ncol = 4)
print(my_matrix)
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.19281515 0.4996601 0.0693210 0.8812991
## [2,] 0.89651401 0.2269533 0.4381043 0.8905899
## [3,] 0.01815808 0.1112220 0.7068814 0.0741309
## [4,] 0.20777991 0.8662358 0.5538604 0.9461306
# my_logical: 100-element vector of TRUE/FALSE values and then assign some inequality (0.5)
my_logical = runif(100) > 0.5 
print(my_logical)
##   [1] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
##  [13]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [25]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE
##  [37]  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [49] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
##  [61] FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [73]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
##  [85] FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE
##  [97]  TRUE  TRUE FALSE  TRUE
# my_letters: 26-element vector with all lowercase letters in random order
my_letters = sample(letters, 26)
print(my_letters)
##  [1] "a" "v" "b" "i" "k" "y" "q" "z" "u" "d" "f" "e" "s" "x" "t" "c" "l" "g" "h"
## [20] "m" "j" "n" "o" "r" "p" "w"
# create list
my_list <- list(my_matrix, my_logical, my_letters)

# print the list
print(my_list)
## [[1]]
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.19281515 0.4996601 0.0693210 0.8812991
## [2,] 0.89651401 0.2269533 0.4381043 0.8905899
## [3,] 0.01815808 0.1112220 0.7068814 0.0741309
## [4,] 0.20777991 0.8662358 0.5538604 0.9461306
## 
## [[2]]
##   [1] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
##  [13]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [25]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE
##  [37]  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [49] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
##  [61] FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [73]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
##  [85] FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE
##  [97]  TRUE  TRUE FALSE  TRUE
## 
## [[3]]
##  [1] "a" "v" "b" "i" "k" "y" "q" "z" "u" "d" "f" "e" "s" "x" "t" "c" "l" "g" "h"
## [20] "m" "j" "n" "o" "r" "p" "w"
# Using the original list (my_list)
my_matrix_list <- my_matrix[2,2]
my_logical_list <- my_logical[2]
my_letters_list <- my_letters[2]

# create new list
new_list <- list(my_matrix_list,my_logical_list,my_letters_list)
print(new_list)
## [[1]]
## [1] 0.2269533
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] "v"
# Check the types of each component in the new list
typeof(new_list[[1]]) 
## [1] "double"
typeof(new_list[[2]]) 
## [1] "logical"
typeof(new_list[[3]]) 
## [1] "character"
# Combine the elements into a single atomic vector
combined_vector <- c(my_matrix_list, my_logical_list, my_letters_list)

# Check the data type of the combined vector
typeof(combined_vector)
## [1] "character"

Question 3

# defining my_unis
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]  9  4  2  5  0 10  1  6  3  8  7  5  0  1 10  3  2  8  9  6  4  7  6  4  0
## [26]  8
# defining my_letters
my_letters <- sample(LETTERS, 26)
print(my_letters)
##  [1] "M" "C" "L" "Y" "U" "R" "G" "J" "N" "I" "W" "Q" "K" "V" "F" "X" "B" "A" "E"
## [20] "D" "Z" "P" "H" "O" "T" "S"
# create data frame
my_df <- data.frame(my_unis,my_letters)
print(my_df)
##    my_unis my_letters
## 1        9          M
## 2        4          C
## 3        2          L
## 4        5          Y
## 5        0          U
## 6       10          R
## 7        1          G
## 8        6          J
## 9        3          N
## 10       8          I
## 11       7          W
## 12       5          Q
## 13       0          K
## 14       1          V
## 15      10          F
## 16       3          X
## 17       2          B
## 18       8          A
## 19       9          E
## 20       6          D
## 21       4          Z
## 22       7          P
## 23       6          H
## 24       4          O
## 25       0          T
## 26       8          S
# 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
my_df$my_unis[sample(1:nrow(my_df), 4)] <- NA
print(my_df)
##    my_unis my_letters
## 1        9          M
## 2        4          C
## 3        2          L
## 4        5          Y
## 5        0          U
## 6       10          R
## 7        1          G
## 8        6          J
## 9        3          N
## 10       8          I
## 11       7          W
## 12       5          Q
## 13       0          K
## 14       1          V
## 15      NA          F
## 16       3          X
## 17      NA          B
## 18      NA          A
## 19       9          E
## 20      NA          D
## 21       4          Z
## 22       7          P
## 23       6          H
## 24       4          O
## 25       0          T
## 26       8          S
# for the first variable, write a single line of R code to identify which rows have the missing values
which(!complete.cases(my_df$my_unis))
## [1] 15 17 18 20
# reorder the entire data frame to arrange the second variable in alphabetical order
my_df <- my_df[order(my_df$my_letters), ]
print(my_df)
##    my_unis my_letters
## 18      NA          A
## 17      NA          B
## 2        4          C
## 20      NA          D
## 19       9          E
## 15      NA          F
## 7        1          G
## 23       6          H
## 10       8          I
## 8        6          J
## 13       0          K
## 3        2          L
## 1        9          M
## 9        3          N
## 24       4          O
## 22       7          P
## 12       5          Q
## 6       10          R
## 26       8          S
## 25       0          T
## 5        0          U
## 14       1          V
## 11       7          W
## 16       3          X
## 4        5          Y
## 21       4          Z
# calculate the column mean for the first variable
mean_my_unis <- mean(my_df$my_unis, na.rm =TRUE)
print(mean_my_unis)
## [1] 4.636364