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] 5
# 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
# use the sample matrix to reshuffle these values
my_vec <- sample(my_vec)
print(my_vec)
##  [1] 23 15  1 25  7  4  9 11 17 13 16 20 21  3  2 10  6 19 22 14  5 24  8 18 12
# 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]
## [1,]  529   16  256  100   25
## [2,]  225   81  400   36  576
## [3,]    1  121  441  361   64
## [4,]  625  289    9  484  324
## [5,]   49  169    4  196  144
# 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]
## [1,]  529  225    1  625   49
## [2,]   16   81  121  289  169
## [3,]  256  400  441    9    4
## [4,]  100   36  361  484  196
## [5,]   25  576   64  324  144
# 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] 1429
mean_first_row <- mean(transpose_matrix[1,])
print(mean_first_row)
## [1] 285.8
sum_last_row <- sum(transpose_matrix[n_dims, ])
print(sum_last_row)
## [1] 1133
mean_last_row <- mean(transpose_matrix[n_dims, ])
print(mean_last_row)
## [1] 226.6
# use eigen() function on matrix
eigen_matrix <- eigen(transpose_matrix)
print(eigen_matrix)
## eigen() decomposition
## $values
## [1] 1101.5298+  0.0000i  305.7253+  0.0000i  208.9590+221.5618i
## [4]  208.9590-221.5618i -146.1732+  0.0000i
## 
## $vectors
##               [,1]           [,2]                   [,3]                   [,4]
## [1,] -0.6460889+0i  0.32031537+0i  0.3298604-0.39627177i  0.3298604+0.39627177i
## [2,] -0.2516756+0i -0.31824871+0i  0.1797873+0.09479594i  0.1797873-0.09479594i
## [3,] -0.4114063+0i  0.35477116+0i -0.4859793-0.20135107i -0.4859793+0.20135107i
## [4,] -0.4726992+0i  0.06356416+0i -0.1364286+0.28604788i -0.1364286-0.28604788i
## [5,] -0.3557089+0i -0.81621862+0i  0.5618690+0.00000000i  0.5618690+0.00000000i
##                [,5]
## [1,]  0.32454963+0i
## [2,] -0.26455757+0i
## [3,]  0.03788067+0i
## [4,] -0.32192971+0i
## [5,]  0.84829426+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.00703445 0.1611352 0.09530089 0.1173762
## [2,] 0.94384864 0.1963959 0.78860588 0.2636484
## [3,] 0.01110774 0.1643156 0.98927385 0.5304749
## [4,] 0.31704407 0.9548033 0.23544270 0.3332748
# 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]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
##  [13]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE
##  [25] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE
##  [37] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE
##  [61] FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
##  [73] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE
##  [85] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE
##  [97] FALSE FALSE FALSE  TRUE
# my_letters: 26-element vector with all lowercase letters in random order
my_letters = sample(letters, 26)
print(my_letters)
##  [1] "v" "k" "n" "h" "x" "l" "f" "j" "e" "d" "r" "b" "p" "i" "c" "y" "q" "m" "o"
## [20] "s" "w" "a" "g" "z" "u" "t"
# create list
my_list <- list(my_matrix, my_logical, my_letters)

# print the list
print(my_list)
## [[1]]
##            [,1]      [,2]       [,3]      [,4]
## [1,] 0.00703445 0.1611352 0.09530089 0.1173762
## [2,] 0.94384864 0.1963959 0.78860588 0.2636484
## [3,] 0.01110774 0.1643156 0.98927385 0.5304749
## [4,] 0.31704407 0.9548033 0.23544270 0.3332748
## 
## [[2]]
##   [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
##  [13]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE
##  [25] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE
##  [37] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE
##  [61] FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
##  [73] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE
##  [85] FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE
##  [97] FALSE FALSE FALSE  TRUE
## 
## [[3]]
##  [1] "v" "k" "n" "h" "x" "l" "f" "j" "e" "d" "r" "b" "p" "i" "c" "y" "q" "m" "o"
## [20] "s" "w" "a" "g" "z" "u" "t"
# 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.1963959
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "k"
# 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]  0  3  1  8  7  9  5  4  2  6 10  7  8  9  2  3  5  1  0 10  4  6  0  7  8
## [26]  4
# defining my_letters
my_letters <- sample(LETTERS, 26)
print(my_letters)
##  [1] "K" "T" "B" "X" "O" "J" "C" "M" "D" "L" "Q" "W" "R" "U" "Z" "I" "S" "H" "V"
## [20] "P" "N" "E" "Y" "A" "G" "F"
# create data frame
my_df <- data.frame(my_unis,my_letters)
print(my_df)
##    my_unis my_letters
## 1        0          K
## 2        3          T
## 3        1          B
## 4        8          X
## 5        7          O
## 6        9          J
## 7        5          C
## 8        4          M
## 9        2          D
## 10       6          L
## 11      10          Q
## 12       7          W
## 13       8          R
## 14       9          U
## 15       2          Z
## 16       3          I
## 17       5          S
## 18       1          H
## 19       0          V
## 20      10          P
## 21       4          N
## 22       6          E
## 23       0          Y
## 24       7          A
## 25       8          G
## 26       4          F
# 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       NA          K
## 2        3          T
## 3        1          B
## 4        8          X
## 5        7          O
## 6       NA          J
## 7        5          C
## 8        4          M
## 9        2          D
## 10       6          L
## 11      10          Q
## 12       7          W
## 13       8          R
## 14       9          U
## 15       2          Z
## 16       3          I
## 17       5          S
## 18       1          H
## 19       0          V
## 20      10          P
## 21      NA          N
## 22       6          E
## 23       0          Y
## 24      NA          A
## 25       8          G
## 26       4          F
# 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]  1  6 21 24
# 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
## 24      NA          A
## 3        1          B
## 7        5          C
## 9        2          D
## 22       6          E
## 26       4          F
## 25       8          G
## 18       1          H
## 16       3          I
## 6       NA          J
## 1       NA          K
## 10       6          L
## 8        4          M
## 21      NA          N
## 5        7          O
## 20      10          P
## 11      10          Q
## 13       8          R
## 17       5          S
## 2        3          T
## 14       9          U
## 19       0          V
## 12       7          W
## 4        8          X
## 23       0          Y
## 15       2          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.954545