Question 1:
n_dims <- runif(1, 3, 10)
#vector with values from 1-n_dims^2
vec <- c(1:(n_dims^2))
#random sampling of vector above
samp <- sample(vec)
#a square matrix with all the random samples that is n_dims length
m <- matrix(data=samp, nrow=n_dims, ncol=n_dims)
## Warning in matrix(data = samp, nrow = n_dims, ncol = n_dims): data length [21]
## is not a sub-multiple or multiple of the number of rows [4]
print(m)
## [,1] [,2] [,3] [,4]
## [1,] 16 14 6 18
## [2,] 8 15 7 9
## [3,] 11 13 2 19
## [4,] 1 12 20 10
#transposing the matrix
m_trans <- t(m)
print(m_trans)
## [,1] [,2] [,3] [,4]
## [1,] 16 8 11 1
## [2,] 14 15 13 12
## [3,] 6 7 2 20
## [4,] 18 9 19 10
The transposed matrix has the columns and rows switched from before.
#sum and mean first row
sum(m_trans[1,])
## [1] 36
mean(m_trans[1,])
## [1] 9
#sum and mean last row
sum(m_trans[n_dims,])
## [1] 56
mean(m_trans[n_dims,])
## [1] 14
#eigen function
eigen <- eigen(m)
print(eigen)
## eigen() decomposition
## $values
## [1] 44.210392+0.000000i -11.626941+0.000000i 5.208275+2.120941i
## [4] 5.208275-2.120941i
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.6065833+0i 0.29106441+0i -0.09044923+0.526694219i
## [2,] -0.4248661+0i -0.05858759+0i 0.61842263+0.000000000i
## [3,] -0.4939817+0i 0.71049742+0i -0.23309510+0.008993075i
## [4,] -0.4555520+0i -0.63799876+0i -0.41112945-0.329429737i
## [,4]
## [1,] -0.09044923-0.526694219i
## [2,] 0.61842263+0.000000000i
## [3,] -0.23309510-0.008993075i
## [4,] -0.41112945+0.329429737i
typeof(eigen$values)
## [1] "complex"
typeof(eigen$vectors)
## [1] "complex"
To my knowledge, the values that are returned when the eigen function is used tell you the factor used to scale the matrix or the relationship between the coefficients and the vectors. The type for both eigen values and eigen vectors is “double”
Question 2:
#
my_matrix <- matrix(data=runif(16), nrow=4, ncol=4)
my_logical <- c(runif(100)>50)
my_letters <- sample(letters, length(letters))
#putting all of these in 1 list
my_list <- list(my_matrix, my_logical, my_letters)
# making a list of the second object in my matrix, logical, letters
new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
#returns type of object for every object in new_list
sapply(new_list, typeof)
## [1] "double" "logical" "character"
#concatenated list of all values in new_list
b <- c(my_list)
typeof(b)
## [1] "list"
Question 3
# 26 random numbers
my_unis <- runif(26, 0, 10)
#sampling of capital letters
my_letters <- sample(LETTERS, length(LETTERS))
#creating the data frame
df <- data.frame(my_unis, my_letters)
#taking random rows and making them NAs
df[sample(nrow(df), 4), "my_unis"] <- NA
#which rows have NAs
which(is.na(df["my_unis"]))
## [1] 4 13 16 22
#ordering the data set alphabetically by 2nd var
df[order(df$my_letters),]
## my_unis my_letters
## 6 5.3642535 A
## 11 2.6352356 B
## 10 8.6864475 C
## 7 5.6193683 D
## 8 0.4398226 E
## 5 7.4432219 F
## 15 9.1810034 G
## 20 9.0173529 H
## 23 2.8435114 I
## 14 5.8097134 J
## 17 0.6736333 K
## 18 4.6584788 L
## 24 9.6186973 M
## 26 5.9677202 N
## 21 4.4837785 O
## 2 7.6013302 P
## 1 8.0279428 Q
## 9 0.4278306 R
## 12 6.8720169 S
## 19 0.5762978 T
## 3 4.4345051 U
## 16 NA V
## 4 NA W
## 22 NA X
## 13 NA Y
## 25 4.3900279 Z
#calc column mean for 1st var without the NAs
mean(df$my_unis, na.rm = T)
## [1] 5.216918