Question 1
set.seed(12)
# Create a vector of consecutive integers from 1-n_dims^2.
n_dims <- sample(3:10, 1)
n_dims
## [1] 4
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
# Use the sample function to randomly reshuffle these values, create a square matrix, and print it.
my_vec_remix <- sample(my_vec)
my_vec_remix
## [1] 10 7 11 5 13 2 8 14 12 9 1 4 15 16 6 3
my_square <- matrix(my_vec_remix, nrow = 4)
print(my_square)
## [,1] [,2] [,3] [,4]
## [1,] 10 13 12 15
## [2,] 7 2 9 16
## [3,] 11 8 1 6
## [4,] 5 14 4 3
# Transpose the matrix
transpose <- t(my_square)
print(transpose)
## [,1] [,2] [,3] [,4]
## [1,] 10 7 11 5
## [2,] 13 2 8 14
## [3,] 12 9 1 4
## [4,] 15 16 6 3
# Calculate the sum and means of the elements in the first and last row of transposed matrix
sum(transpose[1,])
## [1] 33
mean(transpose[1,])
## [1] 8.25
sum(transpose[3,])
## [1] 26
mean(transpose[3,])
## [1] 6.5
# Special decomposition of a matrix using the "eigen" function (typed "?eigen" into the console)
decomp_matrix <- eigen(transpose)
decomp_matrix
## eigen() decomposition
## $values
## [1] 33.805563 -13.183933 -5.678906 1.057276
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] -0.4620340 -0.08639279 -0.63951602 -0.5964205
## [2,] -0.5433435 -0.68925887 0.33069174 0.5334069
## [3,] -0.3891537 0.33021474 0.69383497 -0.1215456
## [4,] -0.5829768 0.63907490 0.01597698 0.5873554
typeof(decomp_matrix)
## [1] "list"
Question 2
# Create a 4x4 matrix with random uniform values
my_matrix <- matrix(data = runif(16), nrow = 4)
my_matrix
## [,1] [,2] [,3] [,4]
## [1,] 0.09785304 0.5047680 0.2408832 0.6332646
## [2,] 0.70983047 0.1885869 0.8932649 0.9410875
## [3,] 0.21782304 0.4394293 0.8827564 0.6939114
## [4,] 0.26794359 0.6698193 0.8140633 0.8437022
# Produce a vector of 100 boolean values
my_logical <- runif(100, 0, 1)
my_logical <- my_logical < 0.5
my_logical
## [1] TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE
## [13] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE
## [25] FALSE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE FALSE FALSE TRUE
## [37] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
## [49] TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE
## [61] TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
## [73] FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE
## [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
## [97] TRUE FALSE FALSE FALSE
# Create a 26 element vector of letters
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"
# Create a new list combining the matrix, logical vector, and letters
my_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
my_list
## [[1]]
## [1] 0.1885869
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "b"
typeof(my_list[1])
## [1] "list"
typeof(my_list[2])
## [1] "list"
typeof(my_list[3])
## [1] "list"
my_list <- c(my_list)
my_list
## [[1]]
## [1] 0.1885869
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "b"
typeof(my_list[1]) # Still a list
## [1] "list"
my_list_2 <- c(my_matrix[2,2], my_logical[2], my_letters[2])
typeof(my_list_2[1])
## [1] "character"
typeof(my_list_2[2])
## [1] "character"
typeof(my_list_2[3])
## [1] "character"
Question 3
# Create a data frame with 26 random uniform values from 0 to 10 and another with 26 randomly ordered capital letters
my_unis <- runif(26, min = 0, max = 10)
my_letters <- sample(LETTERS)
dat <- data.frame(my_unis, my_letters)
dat
## my_unis my_letters
## 1 8.7390817 E
## 2 5.9467854 Y
## 3 2.9965236 P
## 4 8.4011628 L
## 5 5.9883009 B
## 6 7.1031208 H
## 7 3.3381367 A
## 8 5.8215147 K
## 9 4.2758748 M
## 10 8.6829426 O
## 11 4.5885828 Q
## 12 4.2597642 C
## 13 2.6309321 T
## 14 7.9018964 G
## 15 1.0185476 N
## 16 0.8347121 S
## 17 3.5050761 X
## 18 6.9962838 R
## 19 6.9733668 U
## 20 0.6649924 V
## 21 4.2937384 F
## 22 2.5003389 Z
## 23 5.0169870 I
## 24 5.2981234 J
## 25 1.0132115 W
## 26 3.6711480 D
dat[sample(1:nrow(dat),size=4),1] = NA
dat
## my_unis my_letters
## 1 NA E
## 2 5.9467854 Y
## 3 2.9965236 P
## 4 8.4011628 L
## 5 5.9883009 B
## 6 7.1031208 H
## 7 3.3381367 A
## 8 5.8215147 K
## 9 4.2758748 M
## 10 8.6829426 O
## 11 4.5885828 Q
## 12 4.2597642 C
## 13 2.6309321 T
## 14 7.9018964 G
## 15 1.0185476 N
## 16 0.8347121 S
## 17 3.5050761 X
## 18 6.9962838 R
## 19 6.9733668 U
## 20 0.6649924 V
## 21 NA F
## 22 2.5003389 Z
## 23 NA I
## 24 5.2981234 J
## 25 NA W
## 26 3.6711480 D
# Find the values with missing values
which(is.na(dat))
## [1] 1 21 23 25
# Reorder the data according to alphabetical order using the second variable
dat <- dat[order(dat$my_letters),]
dat
## my_unis my_letters
## 7 3.3381367 A
## 5 5.9883009 B
## 12 4.2597642 C
## 26 3.6711480 D
## 1 NA E
## 21 NA F
## 14 7.9018964 G
## 6 7.1031208 H
## 23 NA I
## 24 5.2981234 J
## 8 5.8215147 K
## 4 8.4011628 L
## 9 4.2758748 M
## 15 1.0185476 N
## 10 8.6829426 O
## 3 2.9965236 P
## 11 4.5885828 Q
## 18 6.9962838 R
## 16 0.8347121 S
## 13 2.6309321 T
## 19 6.9733668 U
## 20 0.6649924 V
## 25 NA W
## 17 3.5050761 X
## 2 5.9467854 Y
## 22 2.5003389 Z
dat[complete.cases(dat$my_unis), 1]
## [1] 3.3381367 5.9883009 4.2597642 3.6711480 7.9018964 7.1031208 5.2981234
## [8] 5.8215147 8.4011628 4.2758748 1.0185476 8.6829426 2.9965236 4.5885828
## [15] 6.9962838 0.8347121 2.6309321 6.9733668 0.6649924 3.5050761 5.9467854
## [22] 2.5003389
mean(dat$my_unis, na.rm = TRUE)
## [1] 4.699915