Homework 5
n_dims <- sample(3:10,1,replace=TRUE)
vec1 <- seq(1:n_dims^2)
sample(x=vec1)
## [1] 48 7 45 10 37 13 9 44 18 21 16 47 5 23 33 25 28 26 3 34 22 17 12 40 32
## [26] 39 1 6 35 2 15 14 11 41 36 29 46 4 30 42 38 31 49 20 27 43 24 19 8
mat1 <- matrix(data=vec1,nrow=n_dims)
print(mat1)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1 8 15 22 29 36 43
## [2,] 2 9 16 23 30 37 44
## [3,] 3 10 17 24 31 38 45
## [4,] 4 11 18 25 32 39 46
## [5,] 5 12 19 26 33 40 47
## [6,] 6 13 20 27 34 41 48
## [7,] 7 14 21 28 35 42 49
tmat1 <- t(mat1)
print(tmat1)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,] 1 2 3 4 5 6 7
## [2,] 8 9 10 11 12 13 14
## [3,] 15 16 17 18 19 20 21
## [4,] 22 23 24 25 26 27 28
## [5,] 29 30 31 32 33 34 35
## [6,] 36 37 38 39 40 41 42
## [7,] 43 44 45 46 47 48 49
#the same as doing byrow vs bycolumn, this has "flipped" the matrix
sum(tmat1[1,])
## [1] 28
mean(tmat1[1,])
## [1] 4
sum(tmat1[n_dims,])
## [1] 322
mean(tmat1[n_dims,])
## [1] 46
eigen1 <- eigen(tmat1)
print(eigen1)
## eigen() decomposition
## $values
## [1] 182.51710372348776445505791+0.0000000000000000000000i
## [2] -7.51710372348759392480133+0.0000000000000000000000i
## [3] -0.00000000000000590603849+0.0000000000000000000000i
## [4] 0.00000000000000074986135+0.0000000000000000000000i
## [5] -0.00000000000000005245824+0.0000000000000002931226i
## [6] -0.00000000000000005245824-0.0000000000000002931226i
## [7] -0.00000000000000021039007+0.0000000000000000000000i
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] -0.06480021+0i 0.5941931+0i -0.73192505+0i -0.3964361348+0i
## [2,] -0.15419968+0i 0.4057643+0i 0.48795004+0i -0.0532556739+0i
## [3,] -0.24359915+0i 0.2173356+0i 0.34156503+0i 0.2032002452+0i
## [4,] -0.33299862+0i 0.0289069+0i 0.19518001+0i 0.4748048614+0i
## [5,] -0.42239809+0i -0.1595218+0i 0.04879500+0i -0.0008880844+0i
## [6,] -0.51179756+0i -0.3479505+0i -0.09759001+0i 0.4094557819+0i
## [7,] -0.60119703+0i -0.5363793+0i -0.24397502+0i -0.6368809954+0i
## [,5] [,6] [,7]
## [1,] 0.258780338+0.10365766i 0.258780338-0.10365766i -0.17202979+0i
## [2,] -0.005689689+0.04720817i -0.005689689-0.04720817i -0.05055448+0i
## [3,] 0.237436834-0.10161417i 0.237436834+0.10161417i 0.09428234+0i
## [4,] -0.461449218-0.18366538i -0.461449218+0.18366538i -0.21583527+0i
## [5,] -0.491524651-0.03494772i -0.491524651+0.03494772i 0.87273686+0i
## [6,] -0.106583963+0.16936144i -0.106583963-0.16936144i -0.19014614+0i
## [7,] 0.569030349+0.00000000i 0.569030349+0.00000000i -0.33845352+0i
typeof(eigen1$values)
## [1] "complex"
typeof(eigen1$vectors)
## [1] "complex"
#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] 3 15 19 25
myframe <- myframe[order(myframe$my_letters),]
mean(myframe$my_unis,na.rm=TRUE)
## [1] 4.898213
print(myframe)
## my_unis my_letters
## 1 7.585331 A
## 26 2.249701 B
## 21 3.610957 C
## 9 9.409372 D
## 25 NA E
## 16 2.128807 F
## 19 NA G
## 13 6.331537 H
## 20 5.309740 I
## 8 1.075933 J
## 3 NA K
## 5 4.168296 L
## 15 NA M
## 7 1.340068 N
## 12 2.256209 O
## 22 4.341354 P
## 4 2.277361 Q
## 10 8.022226 R
## 6 6.144708 S
## 2 7.135527 T
## 23 8.687643 U
## 14 2.499110 V
## 18 8.115957 W
## 24 3.056855 X
## 11 4.811542 Y
## 17 7.202450 Z