1.

Assign to the variable n_dims a single random integer between 3 and 10.

n_dims <- sample(3:10,1)

vec <- c(1:(n_dims)^2)

sample(vec)
##  [1] 25 15 19 23  4  1 12 16 10 20 22 24  7 11 13  2 17  6  8  9  3 21 14  5 18
matr <- matrix(data = vec, nrow = n_dims)

print(matr)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    6   11   16   21
## [2,]    2    7   12   17   22
## [3,]    3    8   13   18   23
## [4,]    4    9   14   19   24
## [5,]    5   10   15   20   25
matr <- t(matr)

print(matr)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
## [3,]   11   12   13   14   15
## [4,]   16   17   18   19   20
## [5,]   21   22   23   24   25
sum(matr[1,])
## [1] 15
mean(matr[1,])
## [1] 3
sum(matr[n_dims,])
## [1] 115
mean(matr[n_dims,])
## [1] 23
eigen(matr)
## eigen() decomposition
## $values
## [1]  6.864208e+01+0.000000e+00i -3.642081e+00+0.000000e+00i
## [3] -2.153465e-15+2.015276e-15i -2.153465e-15-2.015276e-15i
## [5]  7.219523e-16+0.000000e+00i
## 
## $vectors
##               [,1]           [,2]                  [,3]                  [,4]
## [1,] -0.1079750+0i  0.67495283+0i  0.0381046+0.2345472i  0.0381046-0.2345472i
## [2,] -0.2527750+0i  0.36038970+0i  0.2645195-0.2955559i  0.2645195+0.2955559i
## [3,] -0.3975750+0i  0.04582657+0i -0.1224010-0.0257606i -0.1224010+0.0257606i
## [4,] -0.5423751+0i -0.26873656+0i -0.7011750+0.0000000i -0.7011750+0.0000000i
## [5,] -0.6871751+0i -0.58329969+0i  0.5209519+0.0867693i  0.5209519-0.0867693i
##                [,5]
## [1,] -0.11703805+0i
## [2,] -0.22725312+0i
## [3,]  0.82596595+0i
## [4,] -0.50202033+0i
## [5,]  0.02034555+0i

The $values are eigenvalues of the matrix which are the characteristic roots of the matrix, and they are scalar. The $vectors are the eigenvectors of the matrix, and are vectors so they are not scalar and have direction.

typeof(eigen(matr)$values)
## [1] "complex"
typeof(eigen(matr)$vectors)
## [1] "complex"
# these are complex numbers

2.

Create a list with the following named elements:

Then, complete the following steps:

mylist <- list(my_matrix=matrix(data = runif(16),nrow = 4), my_logical=(runif(100))<0.5, my_letters=sample(letters))
print(mylist)
## $my_matrix
##             [,1]       [,2]      [,3]       [,4]
## [1,] 0.725260447 0.57651754 0.7820777 0.56801887
## [2,] 0.009603657 0.01462046 0.6298037 0.02435276
## [3,] 0.133783261 0.80240908 0.8023232 0.04697961
## [4,] 0.634346471 0.54542652 0.4204182 0.65836008
## 
## $my_logical
##   [1]  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [13]  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [25]  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE
##  [37]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE
##  [49]  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE
##  [61]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [73]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
##  [85]  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE
##  [97] FALSE FALSE FALSE FALSE
## 
## $my_letters
##  [1] "a" "j" "x" "v" "r" "c" "g" "b" "w" "n" "h" "z" "k" "l" "m" "p" "f" "s" "y"
## [20] "q" "o" "i" "e" "d" "u" "t"
newlist <- list(mylist$my_matrix[2,2], mylist$my_logical[2], mylist$my_letters[2])
print(newlist) 
## [[1]]
## [1] 0.01462046
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "j"
typeof(newlist[[1]])
## [1] "double"
typeof(newlist[[2]])
## [1] "logical"
typeof(newlist[[3]])
## [1] "character"
myvec <- c(newlist[[1]],newlist[[2]],newlist[[3]])
typeof(myvec)
## [1] "character"

3.

Create a data frame with the two variables (= columns) and 26 cases (= rows) below:

myframe <- data.frame(my_unis=runif(26,min = 0,max = 10),my_letters=sample(LETTERS))
print(myframe)
##       my_unis my_letters
## 1  8.35157179          M
## 2  9.96715304          K
## 3  8.69563717          E
## 4  7.24752890          G
## 5  0.28279357          Y
## 6  3.59986997          P
## 7  0.29850483          L
## 8  0.87818203          U
## 9  7.93922206          D
## 10 9.63060810          T
## 11 8.90974517          X
## 12 0.05405679          Q
## 13 8.06707244          W
## 14 9.29516392          C
## 15 4.57337987          A
## 16 6.96246726          Z
## 17 8.65555684          N
## 18 9.15184251          J
## 19 7.28717337          R
## 20 7.07962466          S
## 21 2.11171730          F
## 22 9.36811574          V
## 23 2.28931822          I
## 24 2.57017151          H
## 25 8.85182761          O
## 26 5.08360072          B
myframe[sample(1:26,4),1]<-NA

which(is.na(myframe$my_unis))
## [1]  7 10 15 26
arrange(myframe,my_letters)
##       my_unis my_letters
## 1          NA          A
## 2          NA          B
## 3  9.29516392          C
## 4  7.93922206          D
## 5  8.69563717          E
## 6  2.11171730          F
## 7  7.24752890          G
## 8  2.57017151          H
## 9  2.28931822          I
## 10 9.15184251          J
## 11 9.96715304          K
## 12         NA          L
## 13 8.35157179          M
## 14 8.65555684          N
## 15 8.85182761          O
## 16 3.59986997          P
## 17 0.05405679          Q
## 18 7.28717337          R
## 19 7.07962466          S
## 20         NA          T
## 21 0.87818203          U
## 22 9.36811574          V
## 23 8.06707244          W
## 24 8.90974517          X
## 25 0.28279357          Y
## 26 6.96246726          Z
mean(myframe$my_unis,na.rm = TRUE)
## [1] 6.255264