Question 1
set.seed(120)
# Part 1, creating a value that is an int between 3 and 10
n_dims <- sample(3:10, 1)
# Part 2, creating a vector of ints of length 1 to n_dims^2
my_vec <- 1:(n_dims^2)
my_vec
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# Part 3, using sample to shuffle the vector
shuffled_vec <- sample(my_vec)
shuffled_vec
## [1] 3 38 47 23 36 34 15 44 7 28 13 11 1 35 45 20 2 9 17 40 33 12 6 14 10
## [26] 25 48 24 41 29 46 31 19 16 39 4 5 18 43 21 8 32 42 37 49 27 22 26 30
# Part 4 and 5, creating a matrix from this vector and then printing it
my_matrix <- matrix(shuffled_vec, nrow = n_dims, ncol = n_dims)
my_matrix
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 3 44 45 12 41 4 42
## [2,] 38 7 20 6 29 5 37
## [3,] 47 28 2 14 46 18 49
## [4,] 23 13 9 10 31 43 27
## [5,] 36 11 17 25 19 21 22
## [6,] 34 1 40 48 16 8 26
## [7,] 15 35 33 24 39 32 30
# Part 6, Transpose Matrix I.E. swap col and row positions of elements
trans_mat <- t(my_matrix)
trans_mat
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 3 38 47 23 36 34 15
## [2,] 44 7 28 13 11 1 35
## [3,] 45 20 2 9 17 40 33
## [4,] 12 6 14 10 25 48 24
## [5,] 41 29 46 31 19 16 39
## [6,] 4 5 18 43 21 8 32
## [7,] 42 37 49 27 22 26 30
# Part 7, calculate sum and mean of elements of first and last row
sum(trans_mat[1,], trans_mat[n_dims,])
## [1] 429
## [1] 28
## [1] 33.28571
mean_trans_mat <- sum(trans_mat[1,], trans_mat[n_dims,])/(2*n_dims)
mean_trans_mat
## [1] 30.64286
sum(my_matrix[1,], my_matrix[n_dims,])
## [1] 399
## [1] 27.28571
## [1] 29.71429
mean_mat <- sum(my_matrix[1,], my_matrix[n_dims,])/(2*n_dims)
mean_mat
## [1] 28.5
# Part 8, using eigen() function on matrix
eig_values_mat <- eigen(my_matrix)
eig_values_mat
## eigen() decomposition
## $values
## [1] 176.878999 -50.125667 -35.360763 23.312087 -17.813318 -14.285899 -3.605438
##
## $vectors
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.4052835 -0.6133900686 -0.05925702 0.48012018 -0.03965263 0.254412770
## [2,] 0.3149572 0.3688797633 0.25494782 0.44425266 0.44699880 0.725490608
## [3,] 0.4325965 0.5084530633 0.18149756 0.24814781 -0.48928000 -0.253049072
## [4,] 0.3321426 0.1523372565 -0.53476084 -0.53766909 0.63880230 0.290066205
## [5,] 0.3246817 0.2089938689 0.07595916 -0.09106761 -0.12149918 -0.453555393
## [6,] 0.3782953 -0.0005901362 0.67266402 -0.40710143 -0.36636718 0.007947276
## [7,] 0.4365355 -0.4028280044 -0.39285241 -0.21779330 0.04657770 -0.234498678
## [,7]
## [1,] 0.08346057
## [2,] 0.32723879
## [3,] -0.00908617
## [4,] -0.07486926
## [5,] -0.80568917
## [6,] 0.18396841
## [7,] 0.44416407
eig_values_trans <- eigen(trans_mat)
eig_values_trans
## eigen() decomposition
## $values
## [1] 176.878999 -50.125667 -35.360763 23.312087 -17.813318 -14.285899 -3.605438
##
## $vectors
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.3968164 0.5554423 -0.2715347 -0.07610943 0.3288329 -0.3962422
## [2,] -0.3152027 -0.1806437 0.4369449 -0.47843670 -0.4672601 0.2697789
## [3,] -0.3542649 -0.7028753 -0.4759485 -0.01168568 0.4671761 -0.2481937
## [4,] -0.2902236 -0.2403037 -0.4720553 0.58640780 0.2928932 -0.4458029
## [5,] -0.4692271 0.1665808 0.3923040 -0.28173674 -0.4072259 0.4000323
## [6,] -0.2805113 0.2080597 0.2751299 0.52268348 0.1021146 -0.1332066
## [7,] -0.4843449 0.1898268 0.2374574 -0.26214766 -0.4395870 0.5762823
## [,7]
## [1,] -0.445709906
## [2,] 0.314285662
## [3,] 0.018702156
## [4,] -0.348366210
## [5,] -0.334602469
## [6,] -0.002676285
## [7,] 0.684757213
# Values in this are the Eigen Values of X
# Vectors are the Eigen Vectors of X for each col
# Part 9, what types of data are these values?
typeof(eig_values_mat$values)
## [1] "double"
typeof(eig_values_mat$vectors)
## [1] "double"
Question 2
# Part 1, creating a 4x4 matrix filled with rand unif values
my_matrix <- matrix(runif(16), nrow = 4, ncol = 4)
my_matrix
## [,1] [,2] [,3] [,4]
## [1,] 0.1161918 0.9927956 0.21879988 0.2225530
## [2,] 0.1828351 0.5509923 0.62395236 0.3849193
## [3,] 0.6762991 0.3366540 0.02173818 0.6528628
## [4,] 0.9963986 0.1432392 0.60869513 0.3316239
# Part 2, Creating a vector of boolean values using a list of rand values
rand_vec <- runif(100, 0, 1)
my_logic <- rand_vec < 0.5
my_logic
## [1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE
## [13] FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE
## [25] TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE
## [37] FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE
## [49] FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [61] FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
## [73] FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE
## [85] TRUE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE
## [97] TRUE FALSE TRUE TRUE
# Part 3, creating a list of letters called my_letters
my_letters <- list(letters)
my_letters
## [[1]]
## [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"
## [1] "list"
my_letters <- letters
my_letters
## [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"
## [1] "character"
# Part 4, new list with elements [2,2] of the matrix, 2nd element of my_logic, and 2nd element of my_letters
# Part 5, showing type of data values of this list
combined_list <- list(my_matrix[2,2], my_logic[2], my_letters[2])
combined_list
## [[1]]
## [1] 0.5509923
##
## [[2]]
## [1] FALSE
##
## [[3]]
## [1] "b"
## [1] "list"
## [1] "list"
## [1] "list"
unrolled_list <- unlist(combined_list)
typeof(unrolled_list)
## [1] "character"
## [1] "character"
## [1] "character"
## [1] "character"
## [1] "0.550992346834391" "FALSE" "b"
# Part 6, creating list using c()
comb_list2 <- c(my_matrix[2,2], my_logic[2], my_letters[2])
comb_list2
## [1] "0.550992346834391" "FALSE" "b"
## [1] "character"
## [1] "character"
## [1] "character"
Question 3
# Part 1, creating data frame with my_unis and my_letters, where my_unis is 26 elements uni dist between 0 and 10 and my_letters is a random list of all capital letters
my_unis <- runif(26, min = 0, max = 10)
my_unis
## [1] 6.0772023 9.0866522 7.6605942 7.8014232 8.2107228 2.0729577 9.9203391
## [8] 8.9051881 5.4351155 4.8934270 1.9132470 8.2471649 5.0342658 7.6155247
## [15] 0.3688531 7.4294379 4.0040800 5.5535435 2.0166351 5.7239729 1.3388730
## [22] 4.4684308 9.3706495 3.2529472 5.2520033 0.1096382
my_letters <- sample(LETTERS)
my_letters
## [1] "R" "M" "Y" "S" "O" "U" "E" "X" "B" "K" "A" "T" "Q" "F" "P" "Z" "C" "N" "D"
## [20] "L" "J" "W" "G" "I" "H" "V"
d_frame <- data.frame(col = my_unis, rows = my_letters)
d_frame
## col rows
## 1 6.0772023 R
## 2 9.0866522 M
## 3 7.6605942 Y
## 4 7.8014232 S
## 5 8.2107228 O
## 6 2.0729577 U
## 7 9.9203391 E
## 8 8.9051881 X
## 9 5.4351155 B
## 10 4.8934270 K
## 11 1.9132470 A
## 12 8.2471649 T
## 13 5.0342658 Q
## 14 7.6155247 F
## 15 0.3688531 P
## 16 7.4294379 Z
## 17 4.0040800 C
## 18 5.5535435 N
## 19 2.0166351 D
## 20 5.7239729 L
## 21 1.3388730 J
## 22 4.4684308 W
## 23 9.3706495 G
## 24 3.2529472 I
## 25 5.2520033 H
## 26 0.1096382 V
# Part 2, turning 4 random rows in my_unis into NA
d_frame[sample(1:26, 4, replace = TRUE),1] <- NA # Specify to not repeat here
d_frame$col
## [1] 6.0772023 9.0866522 7.6605942 7.8014232 8.2107228 NA NA
## [8] 8.9051881 5.4351155 4.8934270 1.9132470 NA NA 7.6155247
## [15] 0.3688531 7.4294379 4.0040800 5.5535435 2.0166351 5.7239729 1.3388730
## [22] 4.4684308 9.3706495 3.2529472 5.2520033 0.1096382
# Part 2.3, finding the positions of NA values
which(!complete.cases(d_frame$col)) # find NA slots
## [1] 6 7 12 13
# Part 3, sorting data frame alphabetically
d_frame <- d_frame[order(d_frame$rows),]
d_frame
## col rows
## 11 1.9132470 A
## 9 5.4351155 B
## 17 4.0040800 C
## 19 2.0166351 D
## 7 NA E
## 14 7.6155247 F
## 23 9.3706495 G
## 25 5.2520033 H
## 24 3.2529472 I
## 21 1.3388730 J
## 10 4.8934270 K
## 20 5.7239729 L
## 2 9.0866522 M
## 18 5.5535435 N
## 5 8.2107228 O
## 15 0.3688531 P
## 13 NA Q
## 1 6.0772023 R
## 4 7.8014232 S
## 12 NA T
## 6 NA U
## 26 0.1096382 V
## 22 4.4684308 W
## 8 8.9051881 X
## 3 7.6605942 Y
## 16 7.4294379 Z
# Part 4, calc column mean for first variable, my_unis
d_frame[complete.cases(d_frame$col),1]
## [1] 1.9132470 5.4351155 4.0040800 2.0166351 7.6155247 9.3706495 5.2520033
## [8] 3.2529472 1.3388730 4.8934270 5.7239729 9.0866522 5.5535435 8.2107228
## [15] 0.3688531 6.0772023 7.8014232 0.1096382 4.4684308 8.9051881 7.6605942
## [22] 7.4294379
mean(d_frame[complete.cases(d_frame$col),1])
## [1] 5.294916