Question 1:

n_dims <- sample(3:10,size=1) # random integer between 3-10
print(n_dims)
## [1] 3
my_seq <- 1:n_dims^2 # sequence from 1 to n_dims^2
print(my_seq)
## [1] 1 2 3 4 5 6 7 8 9
seq_shuffle <- sample(x=my_seq) # randomly reshuffle these values
print(seq_shuffle)
## [1] 9 5 6 4 1 8 2 7 3
seq_matrix <- matrix(data=seq_shuffle,nrow=n_dims) # creating a square matrix with these values
print(seq_matrix)
##      [,1] [,2] [,3]
## [1,]    9    4    2
## [2,]    5    1    7
## [3,]    6    8    3

Transposing a matrix swaps the rows with the columns

transpose_matrix <- t(seq_matrix) # transposing this matrix
print(transpose_matrix)
##      [,1] [,2] [,3]
## [1,]    9    5    6
## [2,]    4    1    8
## [3,]    2    7    3
sum(transpose_matrix[1,]) # sum of first row
## [1] 20
mean(transpose_matrix[1,]) # mean of first row
## [1] 6.666667
sum(transpose_matrix[n_dims,]) # sum of last row
## [1] 12
mean(transpose_matrix[n_dims,]) # mean of last row
## [1] 4
eigen(transpose_matrix) # using eigen()
## eigen() decomposition
## $values
## [1] 14.931570 -5.558319  3.626749
## 
## $vectors
##           [,1]         [,2]       [,3]
## [1,] 0.7927761  0.005446303  0.8191681
## [2,] 0.4583392 -0.774669397 -0.2789202
## [3,] 0.4017851  0.632342994 -0.5011657
eigen(transpose_matrix)$values # looking at $values
## [1] 14.931570 -5.558319  3.626749
eigen(transpose_matrix)$vectors # looking at $vectors 
##           [,1]         [,2]       [,3]
## [1,] 0.7927761  0.005446303  0.8191681
## [2,] 0.4583392 -0.774669397 -0.2789202
## [3,] 0.4017851  0.632342994 -0.5011657
typeof(eigen(transpose_matrix)$values) # type of $values
## [1] "double"
typeof(eigen(transpose_matrix)$vectors) # type of $vectors
## [1] "double"

Question 2:

my_matrix <- matrix(data=runif(16),nrow=4) # creating my_matrix
print(my_matrix)
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.2273021 0.8700357 0.9457997 0.23094772
## [2,] 0.2896987 0.7866773 0.5433780 0.28132805
## [3,] 0.9841563 0.9886634 0.2929972 0.07325231
## [4,] 0.3819385 0.9696800 0.7914918 0.27124610
my_logical <- runif(100)<0.5 # creating my_logical
print(my_logical)
##   [1]  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
##  [13]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
##  [25]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
##  [37]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [49]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE
##  [61] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
##  [73]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
##  [85] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
##  [97] FALSE  TRUE  TRUE FALSE
my_letters <- sample(letters) # creating my_letters
print(my_letters)
##  [1] "s" "d" "w" "f" "q" "j" "n" "v" "p" "o" "x" "m" "k" "z" "b" "u" "e" "l" "h"
## [20] "a" "t" "c" "r" "y" "g" "i"
my_list <- list(my_matrix,my_logical,my_letters) # creating my_list
print(my_list)
## [[1]]
##           [,1]      [,2]      [,3]       [,4]
## [1,] 0.2273021 0.8700357 0.9457997 0.23094772
## [2,] 0.2896987 0.7866773 0.5433780 0.28132805
## [3,] 0.9841563 0.9886634 0.2929972 0.07325231
## [4,] 0.3819385 0.9696800 0.7914918 0.27124610
## 
## [[2]]
##   [1]  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
##  [13]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
##  [25]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
##  [37]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [49]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE
##  [61] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
##  [73]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
##  [85] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
##  [97] FALSE  TRUE  TRUE FALSE
## 
## [[3]]
##  [1] "s" "d" "w" "f" "q" "j" "n" "v" "p" "o" "x" "m" "k" "z" "b" "u" "e" "l" "h"
## [20] "a" "t" "c" "r" "y" "g" "i"
new_list <- list(my_matrix[2,2],my_logical[2],my_letters[2]) # creating new list
print(new_list)
## [[1]]
## [1] 0.7866773
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "d"
typeof(new_list[[1]])
## [1] "double"
typeof(new_list[[2]])
## [1] "logical"
typeof(new_list[[3]])
## [1] "character"
atomic_vec <- c(new_list[[1]], new_list[[2]], new_list[[3]])
print(atomic_vec)
## [1] "0.786677304422483" "FALSE"             "d"
typeof(atomic_vec)
## [1] "character"

Question 3:

my_unis <- sample(0:10,size=26,replace=TRUE) # create my_unis
print(my_unis)
##  [1]  4  6  4  5  8  5 10  4  1  1  5  6  5  1  6  0  9  7  4  8 10 10 10  3  0
## [26]  2
my_letters <- sample(LETTERS) # create my_letters
print(my_letters)
##  [1] "S" "L" "F" "Z" "W" "C" "D" "J" "M" "G" "N" "U" "V" "O" "P" "K" "B" "X" "H"
## [20] "Q" "I" "R" "E" "Y" "T" "A"
my_df <- data.frame(my_unis,my_letters) # create dataframe
print(my_df)
##    my_unis my_letters
## 1        4          S
## 2        6          L
## 3        4          F
## 4        5          Z
## 5        8          W
## 6        5          C
## 7       10          D
## 8        4          J
## 9        1          M
## 10       1          G
## 11       5          N
## 12       6          U
## 13       5          V
## 14       1          O
## 15       6          P
## 16       0          K
## 17       9          B
## 18       7          X
## 19       4          H
## 20       8          Q
## 21      10          I
## 22      10          R
## 23      10          E
## 24       3          Y
## 25       0          T
## 26       2          A
my_df[sample(1:nrow(my_df),size=4),1] = NA # replace 4 random rows of my_unis with NA
print(my_df)
##    my_unis my_letters
## 1        4          S
## 2        6          L
## 3        4          F
## 4        5          Z
## 5        8          W
## 6        5          C
## 7       NA          D
## 8       NA          J
## 9        1          M
## 10       1          G
## 11       5          N
## 12       6          U
## 13       5          V
## 14       1          O
## 15       6          P
## 16       0          K
## 17       9          B
## 18      NA          X
## 19       4          H
## 20      NA          Q
## 21      10          I
## 22      10          R
## 23      10          E
## 24       3          Y
## 25       0          T
## 26       2          A
which(is.na(my_df$my_unis)) # which rows in my_unis have missing values
## [1]  7  8 18 20
new_df <- arrange(my_df,my_letters) # rearrange the dataframe alphabetically
print(new_df)
##    my_unis my_letters
## 1        2          A
## 2        9          B
## 3        5          C
## 4       NA          D
## 5       10          E
## 6        4          F
## 7        1          G
## 8        4          H
## 9       10          I
## 10      NA          J
## 11       0          K
## 12       6          L
## 13       1          M
## 14       5          N
## 15       1          O
## 16       6          P
## 17      NA          Q
## 18      10          R
## 19       4          S
## 20       0          T
## 21       6          U
## 22       5          V
## 23       8          W
## 24      NA          X
## 25       3          Y
## 26       5          Z
mean(new_df$my_unis,na.rm=TRUE) # calculate the mean for the first variable after removing NAs
## [1] 4.772727