Question One

# I would like to set seed first to avoid changing results or conflicts
set.seed(123)

# assigning a single random inter between 3 and 10 to n_dims
n_dims <- sample(3:10, size = 1)

print(n_dims)
## [1] 9
#create a vector of consecutive integers from 1:n_dims^2
vec <- c(1:n_dims ^ 2)
head(vec)
## [1] 1 2 3 4 5 6
# randomly sample to reshuffle the values in the new vector
shuffled_vec <- sample(vec)

# creating a square matrix with the shuffled elements
shuf_matrix <- matrix(shuffled_vec, nrow = n_dims, ncol=n_dims)
head(shuf_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]   79   69   17   68   38   33   16   37   30
## [2,]   51   57   75   66   73   47   20   19   61
## [3,]   14    9   39   41   34   44    6    2    1
## [4,]   67   26   53   10   29   21   11    4   76
## [5,]   42    7   12   23    5   58   40   55   65
## [6,]   50   77   15   27    8   60   22   52   24
# transposinng the matrix
trns_matrix <- t(shuf_matrix)

head(trns_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
## [1,]   79   51   14   67   42   50   43   81   25
## [2,]   69   57    9   26    7   77   70   36   74
## [3,]   17   75   39   53   12   15   32   78   45
## [4,]   68   66   41   10   23   27   54   49   71
## [5,]   38   73   34   29    5    8   59   13   18
## [6,]   33   47   44   21   58   60   46   72   28
nrow(trns_matrix)
## [1] 9
# calculating the sum and means of elements in the first and last rows
sum(trns_matrix[1,] )
## [1] 452
sum(trns_matrix[9,])
## [1] 457
mean(trns_matrix[1,])
## [1] 50.22222
mean(trns_matrix[9,])
## [1] 50.77778
# using the eigen function on my matrix
eigen_output <- eigen(trns_matrix)

head(eigen_output) #looks like we generated a bunch of characteristic roots
## $values
## [1] 366.849540+ 0.000000i -65.760156+ 0.000000i  52.937905+ 9.156369i
## [4]  52.937905- 9.156369i -10.070124+51.982021i -10.070124-51.982021i
## [7] -20.974240+ 0.000000i  -6.170663+ 0.000000i   5.319955+ 0.000000i
## 
## $vectors
##                [,1]           [,2]                    [,3]
##  [1,] -0.4003557+0i -0.36467251+0i -0.76423243+0.00000000i
##  [2,] -0.3925288+0i  0.20435520+0i -0.08156143-0.07501265i
##  [3,] -0.3256978+0i -0.38498423+0i  0.16136070-0.02821235i
##  [4,] -0.3722517+0i  0.53589482+0i -0.15984157+0.07690690i
##  [5,] -0.2533823+0i -0.16921595+0i -0.07220939+0.02833808i
##  [6,] -0.3458081+0i  0.02571944+0i  0.11695642-0.24288909i
##  [7,] -0.2223479+0i  0.23676330+0i  0.41662389+0.07429501i
##  [8,] -0.2264092+0i  0.24509906+0i  0.07916646-0.05702481i
##  [9,] -0.3956015+0i -0.49439949+0i  0.21540516+0.17795365i
##                          [,4]                      [,5]
##  [1,] -0.76423243+0.00000000i  0.001366297-0.007970864i
##  [2,] -0.08156143+0.07501265i  0.065103067-0.528549816i
##  [3,]  0.16136070+0.02821235i -0.324505989-0.015871722i
##  [4,] -0.15984157-0.07690690i -0.134854437-0.014311775i
##  [5,] -0.07220939-0.02833808i -0.583284549+0.000000000i
##  [6,]  0.11695642+0.24288909i  0.130232522+0.221638489i
##  [7,]  0.41662389-0.07429501i  0.267355744+0.038508831i
##  [8,]  0.07916646+0.05702481i  0.224083810+0.136221495i
##  [9,]  0.21540516-0.17795365i -0.044401622+0.205855081i
##                            [,6]             [,7]           [,8]           [,9]
##  [1,]  0.001366297+0.007970864i -0.0003187299+0i -0.02780909+0i  0.04579170+0i
##  [2,]  0.065103067+0.528549816i -0.5030079251+0i  0.54641266+0i -0.29322221+0i
##  [3,] -0.324505989+0.015871722i  0.4669183595+0i -0.59977548+0i  0.60725383+0i
##  [4,] -0.134854437+0.014311775i  0.4991421550+0i -0.46009679+0i  0.48913052+0i
##  [5,] -0.583284549+0.000000000i -0.2383286204+0i  0.14400181+0i  0.06515207+0i
##  [6,]  0.130232522-0.221638489i  0.2259489336+0i -0.10272184+0i  0.08164658+0i
##  [7,]  0.267355744-0.038508831i  0.2529626854+0i -0.11816203+0i -0.26169116+0i
##  [8,]  0.224083810-0.136221495i -0.3285513726+0i  0.26026039+0i -0.39337110+0i
##  [9,] -0.044401622-0.205855081i  0.0061403449+0i -0.12749175+0i  0.26416650+0i
# digging in to find out  the actual types of numbers generated
typeof(eigen_output$values)
## [1] "complex"
typeof(eigen_output$vectors) #They are all complex numbers of latent roots
## [1] "complex"

Question 2

# a 4 x 4 matrix filled with random uniform values
my_matrix <- matrix(data=runif(16), nrow=4, ncol=4)
head(my_matrix)
##            [,1]      [,2]      [,3]      [,4]
## [1,] 0.30122890 0.1422943 0.4045103 0.2197676
## [2,] 0.06072057 0.5492847 0.6478935 0.3694889
## [3,] 0.94772694 0.9540912 0.3198206 0.9842192
## [4,] 0.72059627 0.5854834 0.3077200 0.1542023
# a 100-element vector of TRUE or FALSE values
my_logical <- sample(c(TRUE, FALSE), size = 100, replace = TRUE)
head(my_logical)
## [1]  TRUE  TRUE  TRUE FALSE  TRUE FALSE
# a 26-element vector of all the lower-case letters in random order
my_letters <- sample(letters, size = 26, replace = FALSE)
print(my_letters)
##  [1] "m" "b" "k" "z" "n" "f" "h" "l" "d" "w" "q" "v" "e" "g" "a" "t" "x" "i" "j"
## [20] "y" "o" "u" "s" "c" "r" "p"
# 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(c(my_matrix[2,2], my_logical[2], my_letters[2]))
print(my_list)
## [[1]]
## [1] "0.549284656066447" "TRUE"              "b"
typeof(my_matrix[2,2])
## [1] "double"
typeof(my_logical[2])
## [1] "logical"
typeof(my_letters[2])
## [1] "character"
typeof(my_list) # its a list
## [1] "list"

Question 3

# creating a dataframe with two variables
my_unis <- runif(26, min = 0, max =10)
my_letters <- sample(LETTERS, size = 26, replace = FALSE)

my_df <- data.frame(my_unis, my_letters)

head(my_df)
##    my_unis my_letters
## 1 8.681068          Z
## 2 9.257080          T
## 3 8.819776          X
## 4 6.741868          I
## 5 9.501670          W
## 6 5.164449          E
#selecting four random rows and replacing them with NAs in my df
my_df$my_unis <- replace(my_df$my_unis, list=sample(my_df$my_unis, size = 4), values=NA)

head(my_df)
##    my_unis my_letters
## 1 8.681068          Z
## 2 9.257080          T
## 3 8.819776          X
## 4 6.741868          I
## 5 9.501670          W
## 6 5.164449          E
# identifying the portions of the first variable with missing spots
which(is.na(my_df$my_unis))
## [1] 7 8 9
# rearrage the second column in alphabetical order
my_df[order(my_df$my_letters),]
##       my_unis my_letters
## 10 0.20024301          A
## 21 6.48818124          B
## 25 2.24985329          C
## 19 4.66472377          D
## 6  5.16444894          E
## 9          NA          F
## 18 9.71875636          G
## 23 1.37106081          H
## 4  6.74186843          I
## 11 5.02813046          J
## 20 0.74384513          K
## 22 7.58593170          L
## 16 7.70334074          M
## 7          NA          N
## 14 0.72057124          O
## 12 8.71043414          P
## 13 0.06300784          Q
## 24 3.96584595          R
## 15 1.64211225          S
## 2  9.25707956          T
## 17 7.35184306          U
## 26 0.57958561          V
## 5  9.50166979          W
## 3  8.81977559          X
## 8          NA          Y
## 1  8.68106807          Z
#calculating the mean of the first variable
d <- mean(my_df$my_unis, na.rm = TRUE)
head(d)
## [1] 5.084929