Question 1
n_dims <- sample(x=3:10,size=1)
# print(n_dims)
# (n_dims)^2
vector_one <- 1:(n_dims)^2
# print(vector_one)
vector_one <- sample(x=vector_one)
# print(vector_one)
sq_matrix <- matrix(vector_one, nrow= n_dims)
print(sq_matrix)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 60 23 45 40 51 27 62 61
## [2,] 2 35 13 9 3 10 4 34
## [3,] 31 26 7 46 33 19 56 50
## [4,] 11 38 14 54 55 53 18 49
## [5,] 63 59 37 5 30 47 57 6
## [6,] 8 58 21 36 16 22 64 12
## [7,] 39 17 20 25 48 44 28 29
## [8,] 43 24 32 52 15 41 1 42
?t # fxn to transpose matrix in r
## starting httpd help server ... done
sq_matrix <- t(sq_matrix)
print(sq_matrix) # the rows are now columns and vice versa
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 60 2 31 11 63 8 39 43
## [2,] 23 35 26 38 59 58 17 24
## [3,] 45 13 7 14 37 21 20 32
## [4,] 40 9 46 54 5 36 25 52
## [5,] 51 3 33 55 30 16 48 15
## [6,] 27 10 19 53 47 22 44 41
## [7,] 62 4 56 18 57 64 28 1
## [8,] 61 34 50 49 6 12 29 42
# ?sum
sum(sq_matrix[1,])
## [1] 257
sum(sq_matrix[dim(sq_matrix)[1],])
## [1] 283
# ?mean
mean(sq_matrix[1,])
## [1] 32.125
mean(sq_matrix[dim(sq_matrix)[1],])
## [1] 35.375
?eigen
eigen(sq_matrix)
## eigen() decomposition
## $values
## [1] 257.468373+ 0.00000i 60.319451+ 0.00000i -37.197251+27.79405i
## [4] -37.197251-27.79405i 24.513976+27.26983i 24.513976-27.26983i
## [7] -18.541533+ 0.00000i 4.120258+ 0.00000i
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] 0.3456913+0i -0.4385079+0i 0.17836462-0.22152247i 0.17836462+0.22152247i
## [2,] 0.3824558+0i 0.5775803+0i -0.23597849-0.20975572i -0.23597849+0.20975572i
## [3,] 0.2615802+0i -0.0358404+0i -0.04171497-0.19116763i -0.04171497+0.19116763i
## [4,] 0.3606776+0i 0.4415483+0i -0.04830697+0.01330152i -0.04830697-0.01330152i
## [5,] 0.3384201+0i -0.1312283+0i 0.16362057+0.27131597i 0.16362057-0.27131597i
## [6,] 0.3630197+0i 0.2484301+0i 0.37022157-0.12266971i 0.37022157+0.12266971i
## [7,] 0.3791729+0i -0.3156537+0i -0.61307092+0.00000000i -0.61307092+0.00000000i
## [8,] 0.3816136+0i 0.3150980+0i 0.04085700+0.38550447i 0.04085700-0.38550447i
## [,5] [,6] [,7]
## [1,] 0.11276038+0.15048483i 0.11276038-0.15048483i -0.551680462+0i
## [2,] -0.63742900+0.00000000i -0.63742900+0.00000000i -0.102303367+0i
## [3,] -0.02247548+0.09694104i -0.02247548-0.09694104i 0.685558401+0i
## [4,] 0.38161939-0.18476941i 0.38161939+0.18476941i -0.114180293+0i
## [5,] -0.02717972-0.19143665i -0.02717972+0.19143665i 0.297572229+0i
## [6,] -0.10096183-0.18883196i -0.10096183+0.18883196i -0.290922201+0i
## [7,] -0.41796495-0.02865948i -0.41796495+0.02865948i -0.005056788+0i
## [8,] 0.19742610+0.26630287i 0.19742610-0.26630287i 0.170131417+0i
## [,8]
## [1,] 0.192115351+0i
## [2,] -0.290841232+0i
## [3,] 0.007937457+0i
## [4,] 0.437168113+0i
## [5,] 0.262280823+0i
## [6,] -0.253450886+0i
## [7,] -0.744344639+0i
## [8,] -0.015702977+0i
typeof(eigen(sq_matrix)$values) # doubles
## [1] "complex"
typeof(eigen(sq_matrix)$vectors) # doubles
## [1] "complex"
Question 2
my_matrix <- matrix(runif(16),nrow=4, ncol=4)
# print(my_matrix)
random_numbers <- runif(100)
my_logical <- random_numbers>0.5
# print(my_logical)
my_letters <- letters
# print(my_letters)
combined_list <- list(my_matrix[2,2],
my_logical[2],
my_letters[2])
# print(combined_list)
typeof(combined_list)
## [1] "list"
typeof(combined_list[[1]]) # use double brackets to call/index items in a list
## [1] "double"
typeof(combined_list[[2]])
## [1] "logical"
typeof(combined_list[[3]])
## [1] "character"
unrolled_list <- unlist(combined_list)
print(unrolled_list)
## [1] "0.0640123565681279" "FALSE" "b"
single_vector <- c(unrolled_list)
print(single_vector)
## [1] "0.0640123565681279" "FALSE" "b"
typeof(single_vector) # character
## [1] "character"
Question 3
my_unis <- runif(26, min=1, max=10)
# print(my_unis)
my_letters <- sample(LETTERS)
# print(my_letters)
d_frame <- data.frame(my_unis,my_letters)
d_frame[sample(length(d_frame$my_unis),4),1] <- NA
# print(d_frame)
which(!complete.cases(d_frame$my_unis))
## [1] 1 5 18 21
d_frame <- d_frame[order(d_frame$my_letters),]
# print(d_frame)
mean(d_frame[length(d_frame$my_unis),1])
## [1] 4.746781