Q1

# 1. Assign a single random integer between 3 and 10
n_dims <- sample(3:10, 1)
# Create a vector of consecutive integers
vec <- 1:n_dims^2
# Use the sample function to randomly reshuffle these values
shuffled_vec <- sample(vec)
# create a square matrix with these elements
sq_matrix <- matrix(shuffled_vec, nrow=n_dims)
# print out the matrix
print(sq_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    8   26   13    2   25   42   32
## [2,]   33   38   22   27   29   28   39
## [3,]   19   23   49   46   48   11    9
## [4,]   45    6   34   18   36   15   17
## [5,]   35    1   47    4   14   43   40
## [6,]   24   37   20    3   41   16   44
## [7,]   10   31   12    7   30    5   21
# transpose the matrix
t_matrix <- t(sq_matrix)
# print out the transposed matrix
print(t_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    8   33   19   45   35   24   10
## [2,]   26   38   23    6    1   37   31
## [3,]   13   22   49   34   47   20   12
## [4,]    2   27   46   18    4    3    7
## [5,]   25   29   48   36   14   41   30
## [6,]   42   28   11   15   43   16    5
## [7,]   32   39    9   17   40   44   21
# calculate the sum and the mean of the elements in the first row and the last row
fst_sum <- sum(t_matrix[1,])
fst_mean <- mean(t_matrix[1,])
lst_sum <- sum(t_matrix[n_dims,])
lst_mean <- mean(t_matrix[n_dims,])
# calculate eigen value
ev <- eigen(t_matrix)
# Eigen values and vectors are complex numbers
# find out the type of eigen values and vectors
typeof(ev$values)
## [1] "complex"
typeof(ev$vectors)
## [1] "complex"

Q2

# 2. Create a list with the following named elements
# 4 x 4 matrix filled with random uniform values
my_matrix <- matrix(runif(4^2), nrow=4)
# 100-element vector of TRUE or FALSE values
my_logical <- runif(100)<0.5
# 26-element vector of all the lower-case letters in random order
my_letters <- sample(letters)
# 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.
my_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
# check type of each element in my_list
typeof(my_list[[1]]) # double
## [1] "double"
typeof(my_list[[2]]) # logical
## [1] "logical"
typeof(my_list[[3]]) # char
## [1] "character"
# convert my_list to vector
my_vec <- unlist(my_list)
# the data type of this vector is char
typeof(my_vec)
## [1] "character"

Q3

# 3. Create a data frame with two variables (= columns) and 26 cases (= rows).
my_unis <- runif(26, min=0, max=10)
my_letters <- sample(LETTERS)
df <- data.frame(my_unis, my_letters)
# select 4 random rows and replace the numerical values in those rows with NA
df[sample(nrow(df), 4), 1] = NA
# identify which rows have the missing values
which(is.na(df[1]))
## [1]  4  6 15 26
# sort the second variable in alphabetical order
df[order(df$my_letters),]
##      my_unis my_letters
## 12 9.7856394          A
## 3  1.5645464          B
## 19 7.0916455          C
## 7  3.3321897          D
## 9  3.7402583          E
## 24 7.4484351          F
## 1  4.6906603          G
## 21 1.6417315          H
## 14 7.2602532          I
## 16 6.5896728          J
## 2  2.0560036          K
## 11 8.3864688          L
## 8  6.5591866          M
## 25 2.4363170          N
## 10 3.9161759          O
## 20 5.4668650          P
## 22 4.3376379          Q
## 18 6.9709839          R
## 5  0.9215468          S
## 15        NA          T
## 17 3.6439653          U
## 13 9.0713193          V
## 23 6.4637681          W
## 26        NA          X
## 4         NA          Y
## 6         NA          Z
# calculate the column mean for the first variable
fst_var_mean <- mean(df$my_unis[!is.na(df$my_unis)])