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] 42 37 12 23 27 49 44 38 39 45 47  6  5 21 34 30 17 13 11 41  7 24 35 48 26
## [26] 40  1 22 19 33 25 10 46  9  3 18 36  2 31 16  8 20 15 32 14  4 29 43 28
# 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,] 1764 1444 1156  576  361  324  225
## [2,] 1369 1521  900 1225 1089 1296 1024
## [3,]  144 2025  289 2304  625    4  196
## [4,]  529 2209  169  676  100  961   16
## [5,]  729   36  121 1600 2116  256  841
## [6,] 2401   25 1681    1   81   64 1849
## [7,] 1936  441   49  484    9  400  784
# 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,] 1764 1369  144  529  729 2401 1936
## [2,] 1444 1521 2025 2209   36   25  441
## [3,] 1156  900  289  169  121 1681   49
## [4,]  576 1225 2304  676 1600    1  484
## [5,]  361 1089  625  100 2116   81    9
## [6,]  324 1296    4  961  256   64  400
## [7,]  225 1024  196   16  841 1849  784
# 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] 8872
mean_first_row <- mean(transpose_matrix[1,])
print(mean_first_row)
## [1] 1267.429
sum_last_row <- sum(transpose_matrix[n_dims, ])
print(sum_last_row)
## [1] 4935
mean_last_row <- mean(transpose_matrix[n_dims, ])
print(mean_last_row)
## [1] 705
# use eigen() function on matrix
eigen_matrix <- eigen(transpose_matrix)
print(eigen_matrix)
## eigen() decomposition
## $values
## [1] 5994.6220+   0.000i -473.5709+1412.036i -473.5709-1412.036i
## [4]  582.5589+1255.940i  582.5589-1255.940i 1345.9267+   0.000i
## [7] -344.5248+   0.000i
## 
## $vectors
##              [,1]                     [,2]                     [,3]
## [1,] 0.5328776+0i -0.160200678+0.22643244i -0.160200678-0.22643244i
## [2,] 0.5282673+0i  0.511548232+0.00000000i  0.511548232+0.00000000i
## [3,] 0.2815041+0i -0.293514111-0.20468786i -0.293514111+0.20468786i
## [4,] 0.4035281+0i  0.008453165+0.37603443i  0.008453165-0.37603443i
## [5,] 0.2592958+0i -0.102806096-0.04191433i -0.102806096+0.04191433i
## [6,] 0.2392229+0i  0.162046334-0.34349011i  0.162046334+0.34349011i
## [7,] 0.2653935+0i -0.484459249-0.02429728i -0.484459249+0.02429728i
##                        [,4]                   [,5]           [,6]
## [1,]  0.6790722+0.00000000i  0.6790722+0.00000000i -0.08309610+0i
## [2,] -0.2582552-0.33310117i -0.2582552+0.33310117i  0.44551166+0i
## [3,]  0.0154577-0.14268990i  0.0154577+0.14268990i  0.29069203+0i
## [4,] -0.1752524+0.03324724i -0.1752524-0.03324724i -0.22375550+0i
## [5,] -0.1156310+0.18811843i -0.1156310-0.18811843i -0.80605670+0i
## [6,] -0.1964457+0.13529193i -0.1964457-0.13529193i  0.08358981+0i
## [7,]  0.1021236+0.43898472i  0.1021236-0.43898472i -0.05771797+0i
##                [,7]
## [1,] -0.08382407+0i
## [2,] -0.57307892+0i
## [3,] -0.11171768+0i
## [4,]  0.66905373+0i
## [5,]  0.25704489+0i
## [6,]  0.32602659+0i
## [7,] -0.17909477+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.74324903 0.8712035 0.629364799 0.86696385
## [2,] 0.08410895 0.7308486 0.283997210 0.08975908
## [3,] 0.73162592 0.7156768 0.005349946 0.14198044
## [4,] 0.53803465 0.6242206 0.584260358 0.71076866
# 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 FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [13]  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE
##  [25]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [37] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE
##  [61]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE
##  [73] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [85]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE
# my_letters: 26-element vector with all lowercase letters in random order
my_letters = sample(letters, 26)
print(my_letters)
##  [1] "j" "o" "a" "i" "l" "u" "x" "f" "z" "n" "s" "m" "q" "k" "h" "t" "d" "g" "b"
## [20] "y" "w" "c" "r" "p" "v" "e"
# create list
my_list <- list(my_matrix, my_logical, my_letters)

# print the list
print(my_list)
## [[1]]
##            [,1]      [,2]        [,3]       [,4]
## [1,] 0.74324903 0.8712035 0.629364799 0.86696385
## [2,] 0.08410895 0.7308486 0.283997210 0.08975908
## [3,] 0.73162592 0.7156768 0.005349946 0.14198044
## [4,] 0.53803465 0.6242206 0.584260358 0.71076866
## 
## [[2]]
##   [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [13]  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE
##  [25]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [37] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE
##  [61]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE
##  [73] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [85]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE
## 
## [[3]]
##  [1] "j" "o" "a" "i" "l" "u" "x" "f" "z" "n" "s" "m" "q" "k" "h" "t" "d" "g" "b"
## [20] "y" "w" "c" "r" "p" "v" "e"
# 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.7308486
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "o"
# 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]  3  0  7  9  8 10  5  2  1  4  6  0  4 10  2  3  8  7  1  9  5  6  4  3  8
## [26]  9
# defining my_letters
my_letters <- sample(LETTERS, 26)
print(my_letters)
##  [1] "S" "W" "H" "L" "O" "A" "J" "G" "F" "N" "T" "R" "P" "I" "Q" "B" "Z" "E" "M"
## [20] "U" "Y" "X" "K" "D" "C" "V"
# create data frame
my_df <- data.frame(my_unis,my_letters)
print(my_df)
##    my_unis my_letters
## 1        3          S
## 2        0          W
## 3        7          H
## 4        9          L
## 5        8          O
## 6       10          A
## 7        5          J
## 8        2          G
## 9        1          F
## 10       4          N
## 11       6          T
## 12       0          R
## 13       4          P
## 14      10          I
## 15       2          Q
## 16       3          B
## 17       8          Z
## 18       7          E
## 19       1          M
## 20       9          U
## 21       5          Y
## 22       6          X
## 23       4          K
## 24       3          D
## 25       8          C
## 26       9          V
# 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        3          S
## 2        0          W
## 3        7          H
## 4        9          L
## 5        8          O
## 6       10          A
## 7        5          J
## 8        2          G
## 9       NA          F
## 10       4          N
## 11      NA          T
## 12       0          R
## 13       4          P
## 14      10          I
## 15       2          Q
## 16       3          B
## 17       8          Z
## 18       7          E
## 19       1          M
## 20       9          U
## 21       5          Y
## 22       6          X
## 23      NA          K
## 24       3          D
## 25       8          C
## 26      NA          V
# 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]  9 11 23 26
# 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
## 6       10          A
## 16       3          B
## 25       8          C
## 24       3          D
## 18       7          E
## 9       NA          F
## 8        2          G
## 3        7          H
## 14      10          I
## 7        5          J
## 23      NA          K
## 4        9          L
## 19       1          M
## 10       4          N
## 5        8          O
## 13       4          P
## 15       2          Q
## 12       0          R
## 1        3          S
## 11      NA          T
## 20       9          U
## 26      NA          V
## 2        0          W
## 22       6          X
## 21       5          Y
## 17       8          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] 5.181818