Working with matrices, lists, and data frames
Question 1
n_dims <- sample(3:10,1)
print(n_dims)
## [1] 5
vector <- (1 : n_dims^2)
print(vector)
## [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
sample(vector)
## [1] 25 4 7 2 3 18 23 5 10 17 12 22 6 13 8 16 14 21 1 19 9 20 11 24 15
matrix_sq <- matrix(data = vector, nrow = n_dims, byrow = TRUE)
print(matrix_sq)
## [,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
t(matrix_sq)
## [,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
# when you use the function "t" to transpose a matrix, the columns and rows switch
sum_firstrow <- sum(matrix_sq[1,])
sum_lastrow <- sum(matrix_sq[5,])
mean_firstrow <- mean(matrix_sq[1,])
mean_lastrow <- mean(matrix_sq[5,])
eigen(matrix_sq)
## 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
eigen_matrix <- eigen(matrix_sq)
print(eigen_matrix)
## 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
# $values are imaginary numbers in scientific notation
# $vectors are negative and positive decimals added to or subtracted from imaginary decimal numbers
typeof(eigen_matrix$values)
## [1] "complex"
typeof(eigen_matrix$vectors)
## [1] "complex"
# the numbers in both the $values and $vectors in the eigen_matrix are complex figures
Question 2
my_values <- sample(1:16)
my_matrix <- matrix(data = my_values, nrow = 4, ncol = 4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 3 7 15 9
## [2,] 5 14 1 11
## [3,] 16 2 13 10
## [4,] 12 4 8 6
z <- runif(100)
print(z)
## [1] 0.744626133 0.195652751 0.666846739 0.247873843 0.820582684 0.906708506
## [7] 0.047962036 0.040619333 0.964068710 0.201644832 0.554535004 0.472106363
## [13] 0.638229952 0.625162339 0.966087036 0.397858005 0.193420413 0.472851969
## [19] 0.344425970 0.913262796 0.198835632 0.377213403 0.146217789 0.959746141
## [25] 0.417998700 0.008371570 0.904872093 0.621065659 0.596810484 0.305971111
## [31] 0.382728049 0.007247837 0.032900944 0.703651245 0.501106358 0.379944417
## [37] 0.400260307 0.431856498 0.298073045 0.949237700 0.684094351 0.934086729
## [43] 0.762196951 0.706670749 0.324962866 0.498449883 0.199559744 0.920664343
## [49] 0.417243907 0.166663267 0.731020679 0.294948984 0.297302145 0.936267407
## [55] 0.342155898 0.790210976 0.872473416 0.729855010 0.988508686 0.449956820
## [61] 0.126302962 0.416527704 0.191048202 0.147784519 0.342084569 0.837184939
## [67] 0.559118362 0.586003651 0.955898930 0.183968774 0.802513895 0.877300724
## [73] 0.414610987 0.455294853 0.170976786 0.486540607 0.265139540 0.753639485
## [79] 0.575273736 0.626808941 0.363446965 0.236751431 0.809231101 0.510971203
## [85] 0.563753560 0.618912896 0.403955626 0.696932117 0.500206217 0.034555731
## [91] 0.508003091 0.422840456 0.912978338 0.380379890 0.978938347 0.440430484
## [97] 0.213323523 0.045585094 0.699346899 0.626158561
my_logical <- (z < 0.5)
print(my_logical)
## [1] FALSE TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE TRUE
## [13] FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE
## [25] TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE TRUE
## [37] TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE
## [49] TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE
## [61] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [73] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
## [85] FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
## [97] TRUE TRUE FALSE FALSE
rm(letters)
## Warning in rm(letters): object 'letters' not found
my_letters <- letters
print(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"
my_letters <- sample(my_letters)
print(my_letters)
## [1] "e" "q" "w" "s" "z" "b" "y" "m" "f" "p" "n" "l" "r" "o" "v" "i" "t" "d" "h"
## [20] "c" "k" "u" "x" "a" "j" "g"
list_of_elements <- list(my_matrix[2,2],my_logical[2],my_letters[2])
print(list_of_elements)
## [[1]]
## [1] 14
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "q"
typeof(list_of_elements[[1]])
## [1] "integer"
typeof(list_of_elements[[2]])
## [1] "logical"
typeof(list_of_elements[[3]])
## [1] "character"
single_atomic_vector <- c(list_of_elements[[1]], list_of_elements[[2]], list_of_elements[[3]])
print(single_atomic_vector)
## [1] "14" "TRUE" "q"
typeof(single_atomic_vector)
## [1] "character"
# the "single_atomic_vector" is a character
Question 3
my_unis <- sample(0:10,26, replace = TRUE)
print(my_unis)
## [1] 1 6 4 7 10 10 2 10 7 8 6 7 8 8 4 4 9 5 3 0 9 1 9 0 7
## [26] 4
my_letters <- sample(LETTERS)
print(my_letters)
## [1] "D" "U" "O" "Q" "N" "Z" "V" "P" "L" "R" "W" "B" "S" "H" "M" "K" "E" "T" "G"
## [20] "I" "X" "Y" "J" "A" "F" "C"
dFrame <- data.frame(my_unis,my_letters, stringsAsFactors = FALSE)
print(dFrame)
## my_unis my_letters
## 1 1 D
## 2 6 U
## 3 4 O
## 4 7 Q
## 5 10 N
## 6 10 Z
## 7 2 V
## 8 10 P
## 9 7 L
## 10 8 R
## 11 6 W
## 12 7 B
## 13 8 S
## 14 8 H
## 15 4 M
## 16 4 K
## 17 9 E
## 18 5 T
## 19 3 G
## 20 0 I
## 21 9 X
## 22 1 Y
## 23 9 J
## 24 0 A
## 25 7 F
## 26 4 C
dFrame$my_unis[sample(1:nrow(dFrame),4)] <- NA
which(!complete.cases(dFrame))
## [1] 3 4 6 7
dFrame[order(dFrame$my_letters),]
## my_unis my_letters
## 24 0 A
## 12 7 B
## 26 4 C
## 1 1 D
## 17 9 E
## 25 7 F
## 19 3 G
## 14 8 H
## 20 0 I
## 23 9 J
## 16 4 K
## 9 7 L
## 15 4 M
## 5 10 N
## 3 NA O
## 8 10 P
## 4 NA Q
## 10 8 R
## 13 8 S
## 18 5 T
## 2 6 U
## 7 NA V
## 11 6 W
## 21 9 X
## 22 1 Y
## 6 NA Z
mean(dFrame$my_unis, na.rm=TRUE)
## [1] 5.727273