Homework 5

n_dims <- sample(3:10,1,replace=TRUE)

vec1 <- seq(1:n_dims^2)

sample(x=vec1)
##  [1] 11 24  5 18 20 17  2  8 16 23 13 19  4  3 22 21  7  1 25 10 12  6  9 14 15
mat1 <- matrix(data=vec1,nrow=n_dims)
print(mat1)
##      [,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
tmat1 <- t(mat1)
print(tmat1)
##      [,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
#the same as doing byrow vs bycolumn, this has "flipped" the matrix

sum(tmat1[1,])
## [1] 15
mean(tmat1[1,])
## [1] 3
sum(tmat1[n_dims,])
## [1] 115
mean(tmat1[n_dims,])
## [1] 23
eigen1 <- eigen(tmat1)
print(eigen1)
## eigen() decomposition
## $values
## [1]  6.864208e+01 -3.642081e+00 -2.303866e-15 -7.200060e-16  1.927814e-16
## 
## $vectors
##            [,1]        [,2]        [,3]         [,4]       [,5]
## [1,] -0.1079750 -0.67495283 -0.30969357 -0.000832563 -0.1370971
## [2,] -0.2527750 -0.36038970  0.76783803 -0.420258699  0.1260447
## [3,] -0.3975750 -0.04582657 -0.55648658  0.822939221  0.4817413
## [4,] -0.5423751  0.26873656  0.04823335 -0.381772092 -0.7932281
## [5,] -0.6871751  0.58329969  0.05010877 -0.020075867  0.3225393
typeof(eigen1$values)
## [1] "double"
typeof(eigen1$vectors)
## [1] "double"
#They are "doubles" aka numeric values with decimal precision (n_dims = 3), when re-running I found that they were complex (n_dims = 7 or 8).
my_matrix <- matrix(data=runif(16),nrow=4)

logic1 <- runif(100)
my_logical <- logic1>0.5

my_letters <- sample(letters)

mylist <- list(my_matrix,my_logical,my_letters)

newlist <- list(my_matrix[2,2],my_logical[2],my_letters[2])

typeof(newlist[[1]])
## [1] "double"
typeof(newlist[[2]])
## [1] "logical"
typeof(newlist[[3]])
## [1] "character"
newvec <- c(newlist[[1]],newlist[[2]],newlist[[3]])
typeof(newvec)
## [1] "character"
my_unis <- runif(26,min=1,max=10)
my_letters <- sample(LETTERS)

myframe <- data.frame(my_unis,my_letters)

myframe[sample(1:26,4),1] <- NA
which(!complete.cases(myframe$my_unis))
## [1]  5 18 21 25
myframe <- myframe[order(myframe$my_letters),]

mean(myframe$my_unis,na.rm=TRUE)
## [1] 5.508503
print(myframe)
##     my_unis my_letters
## 3  5.314257          A
## 25       NA          B
## 13 9.655105          C
## 5        NA          D
## 7  4.516785          E
## 11 2.937064          F
## 14 7.992971          G
## 1  9.256576          H
## 17 1.314986          I
## 20 4.129205          J
## 2  7.585263          K
## 18       NA          L
## 15 8.338804          M
## 19 2.069267          N
## 8  2.838642          O
## 21       NA          P
## 22 6.539970          Q
## 24 3.351593          R
## 12 9.235258          S
## 9  6.026829          T
## 23 4.225909          U
## 10 2.570208          V
## 16 4.861250          W
## 6  2.221503          X
## 26 9.909252          Y
## 4  6.296360          Z