Library
Question 1:
n_dims<- sample(3:10, 1)
n_dims
## [1] 5
v1 <- 1:(n_dims)^2
v2 <- sample(v1)
m <- matrix(data = v2, nrow = n_dims)
print(m)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 15 11 4 25 7
## [2,] 20 24 21 17 6
## [3,] 2 16 5 18 23
## [4,] 9 1 19 3 22
## [5,] 12 10 8 14 13
m_transpose <- t(m)
# The values in the rows and columns have flipped. Everything that was in previously in [,1] is now in [1,] and vice versa for all other columns and rows.
sum_first_row <- sum(m_transpose[1,])
print(sum_first_row)
## [1] 58
mean_first_row <- mean(m_transpose[1,])
print(mean_first_row)
## [1] 11.6
sum_last_row <- sum(m_transpose[nrow(m_transpose),])
print(sum_last_row)
## [1] 71
mean_last_row <- mean(m_transpose[nrow(m_transpose),])
print(mean_last_row)
## [1] 14.2
em <- eigen(m_transpose)
# This returns a list of values and vectors. Eigenvalues informs us about the variation along the axis, the larger the eigenvalue the more variance. Eigenvectors are vectors associated with linear equations.
emval <- em$values
emvec <- em$vectors
typeof(emval)
## [1] "complex"
## [1] "complex"
#These are both complex numbers.
Question 2:
my_matrix <- matrix(runif(16), nrow = 4)
T_F <- runif(100)
my_logical <- T_F > 0.5
my_letters <- sample(letters)
my_list <- list(my_matrix, my_logical,my_letters)
new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
print(new_list)
## [[1]]
## [1] 0.6220783
##
## [[2]]
## [1] FALSE
##
## [[3]]
## [1] "l"
## [1] "double"
## [1] "logical"
## [1] "character"
comb_my_list <- c(new_list[[1]], new_list[[2]], new_list[[3]])
typeof(comb_my_list)
## [1] "character"
Question 3:
my_unis <- runif(26, min = 0, max = 10)
my_letters <- sample(LETTERS)
df <- data.frame(my_unis,my_letters)
df$my_unis[sample(nrow(df), 4)] <- NA
df
## my_unis my_letters
## 1 NA Y
## 2 1.7364788 H
## 3 7.5361076 V
## 4 6.3965237 F
## 5 6.5492664 U
## 6 9.6076963 A
## 7 2.1999056 X
## 8 9.0779965 T
## 9 9.3902570 Q
## 10 3.6458499 B
## 11 2.0516814 N
## 12 9.5759625 R
## 13 0.6077451 K
## 14 8.6806193 P
## 15 3.3878845 M
## 16 8.1713553 L
## 17 8.2012265 I
## 18 3.2433868 J
## 19 4.2350754 O
## 20 7.3088778 G
## 21 NA D
## 22 3.4010065 Z
## 23 NA C
## 24 3.3838072 S
## 25 7.3000881 W
## 26 NA E
## [1] 1 21 23 26
df1 <- arrange(df, my_letters)
df1
## my_unis my_letters
## 1 9.6076963 A
## 2 3.6458499 B
## 3 NA C
## 4 NA D
## 5 NA E
## 6 6.3965237 F
## 7 7.3088778 G
## 8 1.7364788 H
## 9 8.2012265 I
## 10 3.2433868 J
## 11 0.6077451 K
## 12 8.1713553 L
## 13 3.3878845 M
## 14 2.0516814 N
## 15 4.2350754 O
## 16 8.6806193 P
## 17 9.3902570 Q
## 18 9.5759625 R
## 19 3.3838072 S
## 20 9.0779965 T
## 21 6.5492664 U
## 22 7.5361076 V
## 23 7.3000881 W
## 24 2.1999056 X
## 25 NA Y
## 26 3.4010065 Z
mean(df1$my_unis, na.rm = TRUE)
## [1] 5.713127