Question 1

n_dims <- sample(seq(3:10), size=1) # random number of dimensions
  
vector <- seq(1:n_dims^2)

mat <- matrix(sample(vector), nrow=n_dims)
print(mat)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   44   47   19   13   28    3   39
## [2,]   26   15   12   32   11   27   38
## [3,]   45   21   42   43   22   35   33
## [4,]   16    2    7   30    9   20    6
## [5,]    8   29   25   36   18   17   46
## [6,]   10    4    1   37   24   49   48
## [7,]   41   14   34   31    5   40   23
print(t(mat)) # transpose the element and look at it
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   44   26   45   16    8   10   41
## [2,]   47   15   21    2   29    4   14
## [3,]   19   12   42    7   25    1   34
## [4,]   13   32   43   30   36   37   31
## [5,]   28   11   22    9   18   24    5
## [6,]    3   27   35   20   17   49   40
## [7,]   39   38   33    6   46   48   23
mean(mat[1,])
## [1] 27.57143
sum(mat[1,])
## [1] 193
mean(mat[2,])
## [1] 23
sum(mat[2,])
## [1] 161
mat_eigen <- eigen(mat) # returns a list with the elements "values" and "vectors"

typeof(mat_eigen$values) # $values is type double
## [1] "complex"
typeof(mat_eigen$vectors) # $vectors is type double, but visually I can see that these numbers are within a matrix
## [1] "complex"

Question 2

my_matrix <- matrix(data=runif(16), nrow=4)
my_logical <- (runif(100) > .5)
my_letters <- sample(letters[1:26])

my_list <- list(my_matrix, my_logical, my_letters)

new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
typeof(new_list[[1]])
## [1] "double"
typeof(new_list[[2]])
## [1] "logical"
typeof(new_list[[3]])
## [1] "character"
atomic <- c(new_list[[1]], new_list[[2]], new_list[[3]])
typeof(atomic) # type is character
## [1] "character"

Question 3

# make a data frame
df <- data.frame(my_unis=runif(26, min=0, max=10), my_letters=sample(LETTERS[1:26]))

df[sample(length(df$my_unis),size=4),1]=NA # take the random rows and replace with NA only in the first column
 
# identify which rows have my_unis = NA
which(is.na(df$my_unis))
## [1]  3  8 22 23
# reorder df to be alphabetical
df[order(df$my_letters),]
##      my_unis my_letters
## 24 2.7133143          A
## 12 1.4906009          B
## 19 6.4287917          C
## 17 5.3579046          D
## 5  2.7103179          E
## 8         NA          F
## 13 1.3123442          G
## 2  0.2124493          H
## 14 4.8112179          I
## 6  2.0075028          J
## 3         NA          K
## 26 9.2007178          L
## 4  4.0115974          M
## 23        NA          N
## 10 9.9602077          O
## 18 3.7524853          P
## 1  1.2951689          Q
## 11 4.8988104          R
## 21 8.7022328          S
## 22        NA          T
## 15 8.3144317          U
## 9  0.2489451          V
## 16 3.0015951          W
## 20 3.3904048          X
## 25 0.8943649          Y
## 7  8.1208814          Z
# What is the mean of the first variable?
mean(which(is.na(df$my_unis)==TRUE))
## [1] 14