Part 1

#set seed
set.seed(90)
#part 1
n_dims <- runif(1, 3, 10)
vec1 <- 1:n_dims^2 # create vector from random numbers
vec1s <- sample(vec1) # random sample of vector
mat1 <- matrix(vec1s, 7) # create matrix from sampled vector
## Warning in matrix(vec1s, 7): data length [45] is not a sub-multiple or multiple
## of the number of rows [7]
print(mat1)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   20   41   35    4   25   11   23
## [2,]   34   28    3    7   21   43   13
## [3,]   17   45   31   38   44   24   30
## [4,]   15   27   42   26   12    1   20
## [5,]   40    8   37   19   16    6   34
## [6,]    5   29   32   14   33    9   17
## [7,]   18   36    2   10   22   39   15
mat1t <- t(mat1) # transpose matrix
print(mat1t)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   20   34   17   15   40    5   18
## [2,]   41   28   45   27    8   29   36
## [3,]   35    3   31   42   37   32    2
## [4,]    4    7   38   26   19   14   10
## [5,]   25   21   44   12   16   33   22
## [6,]   11   43   24    1    6    9   39
## [7,]   23   13   30   20   34   17   15

When transposed, the matrix switches the rows and columns

#calculate sum and mean of first row
var1 <- mat1t[1,]
sum1 <- sum(var1)
print(sum1)
## [1] 149
mean1 <- mean(var1)
print(mean1)
## [1] 21.28571
#calculate sum and mean of last roq
var2 <- mat1t[7,]
sum2 <- sum(var2)
print(sum2)
## [1] 152
mean2 <- mean(var2)
print(mean2)
## [1] 21.71429
# eigen() function of matrix
eigen1 <- eigen(mat1t)
print(eigen1)
## eigen() decomposition
## $values
## [1] 161.438884+ 0.00000i -25.134027+11.64317i -25.134027-11.64317i
## [4]  21.077718+ 0.00000i   3.482258+16.09093i   3.482258-16.09093i
## [7]   5.786936+ 0.00000i
## 
## $vectors
##               [,1]                   [,2]                   [,3]          [,4]
## [1,] -0.3646131+0i -0.11076434+0.3501631i -0.11076434-0.3501631i -0.3520568+0i
## [2,] -0.4909703+0i  0.03204858-0.3026953i  0.03204858+0.3026953i  0.0452445+0i
## [3,] -0.3965351+0i  0.63039826+0.0000000i  0.63039826+0.0000000i  0.2518496+0i
## [4,] -0.2646389+0i -0.29130608-0.0181022i -0.29130608+0.0181022i  0.7612622+0i
## [5,] -0.4043957+0i -0.11079687-0.1873424i -0.11079687+0.1873424i -0.3245626+0i
## [6,] -0.3346810+0i -0.47831768+0.1137619i -0.47831768-0.1137619i -0.3104458+0i
## [7,] -0.3509763+0i  0.01715918+0.0218969i  0.01715918-0.0218969i -0.1712985+0i
##                         [,5]                    [,6]          [,7]
## [1,] -0.08020608-0.07739352i -0.08020608+0.07739352i  0.2263824+0i
## [2,]  0.41948210+0.22877172i  0.41948210-0.22877172i  0.4697726+0i
## [3,] -0.22363433-0.15594025i -0.22363433+0.15594025i  0.2388469+0i
## [4,] -0.10721586+0.33500581i -0.10721586-0.33500581i  0.1874859+0i
## [5,] -0.06034809-0.31410157i -0.06034809+0.31410157i -0.3150853+0i
## [6,]  0.55264636+0.00000000i  0.55264636+0.00000000i -0.3204240+0i
## [7,] -0.36841781+0.13330538i -0.36841781-0.13330538i -0.6587227+0i

The numbers produced by the eigen() function are complex numbers

typeof(eigen1)
## [1] "list"

Part 2

# part 2
# set new seed
set.seed(50)
vec2 <- runif(16)
my_matrix <- matrix(vec2, 4, 4) # create matrix from vector
print(my_matrix)
##           [,1]       [,2]       [,3]       [,4]
## [1,] 0.7087271 0.51316189 0.04202962 0.64088615
## [2,] 0.4376599 0.04470388 0.10754123 0.07755953
## [3,] 0.2000049 0.69991279 0.39055818 0.27728954
## [4,] 0.7670660 0.64634362 0.26976581 0.67610045
vec3 <- runif(100, 1, 10)
my_logical <- vec3 > 5 #create boolean vector
print(my_logical)
##   [1]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE
##  [13] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
##  [25] FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [37]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE
##  [49] FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [61]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE
##  [73]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
##  [85]  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE
##  [97] FALSE FALSE FALSE FALSE
letters1 <- letters # set letters
my_letters <- sample(letters1) #randomize letters

list1 <- list(my_matrix[2,2], my_logical[2], my_letters[2])
print(list1)
## [[1]]
## [1] 0.04470388
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "t"
typeof(list1[[1]])
## [1] "double"
typeof(list1[[2]])
## [1] "logical"
typeof(list1[[3]])
## [1] "character"
vec4 <- c(list1[[1]], list1[[2]], list1[[3]])
typeof(vec4)
## [1] "character"

The new vector is a character vector

Part 3

# set seed
set.seed(20)
my_unis <- runif(26, 0, 10)
my_letters2 <- sample(LETTERS)
df <- data.frame(my_unis, my_letters2)
print(df)
##       my_unis my_letters2
## 1  8.77521389           Y
## 2  7.68533209           F
## 3  2.78963138           H
## 4  5.29163693           T
## 5  9.62907031           V
## 6  9.80354585           E
## 7  0.91332588           I
## 8  0.70749478           B
## 9  3.27593952           U
## 10 3.70074542           S
## 11 7.15527557           C
## 12 7.57796354           X
## 13 0.01927939           M
## 14 7.42802090           P
## 15 1.92418547           W
## 16 4.52099627           R
## 17 3.22147280           K
## 18 1.09071407           Q
## 19 2.89262362           Z
## 20 8.19456486           O
## 21 4.91967700           J
## 22 0.30256489           N
## 23 4.40231532           A
## 24 0.77285102           D
## 25 2.64960458           G
## 26 0.69590674           L
df[sample(26,4),1] <- NA
which(is.na(df[,1]))
## [1]  2  6 15 25
sort(df[,2])
##  [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
## [20] "T" "U" "V" "W" "X" "Y" "Z"
mean(df[,1], na.rm=TRUE)
## [1] 4.012604