Question 1
# I've written my code so that it's repeatable given n_dims is equal to any random integer
n_dims <- round(runif(1,min=3,max=10))
n_vect <- c(1:n_dims**2)
shuffle <- sample(n_vect,length(n_vect))
sqmatrix <- matrix(shuffle,ncol=sqrt(length(shuffle)))
sqmatrix
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 79 1 74 50 48 36 81 20 42 69
## [2,] 84 25 34 22 9 49 17 71 88 14
## [3,] 94 33 82 58 29 32 30 19 98 66
## [4,] 15 35 68 85 26 16 73 39 76 28
## [5,] 54 72 56 65 70 3 10 43 27 5
## [6,] 12 7 99 92 55 11 64 46 83 4
## [7,] 91 13 24 45 62 63 44 8 31 59
## [8,] 6 52 95 18 89 100 41 80 93 53
## [9,] 47 87 40 2 23 51 57 75 21 90
## [10,] 38 97 67 78 96 77 61 60 86 37
tpose <- t(sqmatrix)
tpose # elements are flipped over the top-left to bottom-right diagonal
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 79 84 94 15 54 12 91 6 47 38
## [2,] 1 25 33 35 72 7 13 52 87 97
## [3,] 74 34 82 68 56 99 24 95 40 67
## [4,] 50 22 58 85 65 92 45 18 2 78
## [5,] 48 9 29 26 70 55 62 89 23 96
## [6,] 36 49 32 16 3 11 63 100 51 77
## [7,] 81 17 30 73 10 64 44 41 57 61
## [8,] 20 71 19 39 43 46 8 80 75 60
## [9,] 42 88 98 76 27 83 31 93 21 86
## [10,] 69 14 66 28 5 4 59 53 90 37
firstRowSumMean <- c(sum(tpose[1,]),mean(tpose[1,]))
firstRowSumMean
## [1] 520 52
lastrowSumMean <- c(sum(tpose[length(tpose[1,]),]),mean(tpose[length(tpose[1,]),]))
lastrowSumMean
## [1] 425.0 42.5
eig <- eigen(tpose)
eig$values # these are either positive or negative doubles or complex numbers, depending on what n_dims is. I don't remember enough of my linear algebra class off the top of my head to speak as to why some matrices return complex eigen values and others don't. Curious.
## [1] 504.875821+ 0.00000i 92.169856+ 0.00000i 5.201799+66.94677i
## [4] 5.201799-66.94677i -62.842328+ 0.00000i -6.354920+48.91074i
## [7] -6.354920-48.91074i 39.952377+ 0.00000i -34.526922+ 0.00000i
## [10] -3.322562+ 0.00000i
eig$vectors # These seems to be doubles or complex numbers within the range -1<0<1
## [,1] [,2] [,3]
## [1,] 0.3328411+0i -0.35346523+0i -0.21081809-0.09412933i
## [2,] 0.2667014+0i 0.20644791+0i -0.00675700-0.12353684i
## [3,] 0.3945426+0i 0.14515400+0i 0.06562561-0.20346923i
## [4,] 0.3120029+0i -0.54587314+0i 0.26806302-0.39384024i
## [5,] 0.3039996+0i -0.03525229+0i 0.49195743+0.00000000i
## [6,] 0.2675851+0i 0.22465812+0i 0.04770990+0.44161028i
## [7,] 0.2980034+0i -0.41854661+0i 0.01207605+0.17931777i
## [8,] 0.2809178+0i 0.47352889+0i -0.02082026-0.05619233i
## [9,] 0.3923367+0i 0.22904073+0i -0.08371048-0.07006488i
## [10,] 0.2817996+0i -0.09904497+0i -0.31421949+0.26991456i
## [,4] [,5] [,6]
## [1,] -0.21081809+0.09412933i -0.42997153+0i 0.03265747+0.09849196i
## [2,] -0.00675700+0.12353684i 0.07782018+0i -0.02244870-0.12979355i
## [3,] 0.06562561+0.20346923i 0.32601459+0i -0.05182249+0.25519638i
## [4,] 0.26806302+0.39384024i 0.20387041+0i -0.44704507+0.09767655i
## [5,] 0.49195743+0.00000000i -0.03127888+0i -0.31356339-0.22084635i
## [6,] 0.04770990-0.44161028i -0.47336034+0i 0.20040837-0.34462006i
## [7,] 0.01207605-0.17931777i 0.50338768+0i -0.00855842-0.08920249i
## [8,] -0.02082026+0.05619233i 0.27137057+0i -0.20898760+0.12934462i
## [9,] -0.08371048+0.07006488i -0.30101011+0i 0.02112990+0.04344853i
## [10,] -0.31421949-0.26991456i -0.13610008+0i 0.56381709+0.00000000i
## [,7] [,8] [,9] [,10]
## [1,] 0.03265747-0.09849196i 0.353999279+0i -0.55265359+0i 0.23661552+0i
## [2,] -0.02244870+0.12979355i 0.047546735+0i -0.12046978+0i 0.05443706+0i
## [3,] -0.05182249-0.25519638i 0.301180916+0i 0.23283660+0i -0.11958024+0i
## [4,] -0.44704507-0.09767655i -0.715393461+0i 0.14471778+0i 0.55752594+0i
## [5,] -0.31356339+0.22084635i 0.009151866+0i 0.17711204+0i 0.24316419+0i
## [6,] 0.20040837+0.34462006i 0.032621906+0i 0.01379109+0i -0.60080667+0i
## [7,] -0.00855842+0.08920249i -0.486303080+0i 0.51982551+0i -0.09011072+0i
## [8,] -0.20898760-0.12934462i 0.036421632+0i 0.09476513+0i 0.26919671+0i
## [9,] 0.02112990-0.04344853i 0.024015450+0i 0.21526386+0i -0.26931203+0i
## [10,] 0.56381709+0.00000000i 0.174310533+0i -0.49780371+0i -0.20664424+0i
typeof(eig$values) # Indeed these are both doubles (or complex numbers, depending on n_dims)
## [1] "complex"
typeof(eig$vectors)
## [1] "complex"
Question 2
my_matrix <- matrix(runif(16),ncol=4)
my_logical <- runif(100)>=0.5
my_letters <- sample(letters,26)
my_list <- list(my_matrix,my_logical,my_letters)
my_list
## [[1]]
## [,1] [,2] [,3] [,4]
## [1,] 0.8240028 0.2598287 0.6948941 0.2616435
## [2,] 0.4792932 0.3748043 0.8658329 0.3879103
## [3,] 0.2198988 0.8954891 0.3988437 0.5798934
## [4,] 0.1283635 0.5098603 0.5018301 0.2752768
##
## [[2]]
## [1] TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE
## [13] FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE TRUE
## [25] FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE
## [37] TRUE TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE
## [49] FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE
## [61] FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE
## [73] TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE
## [85] FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE
## [97] FALSE FALSE FALSE FALSE
##
## [[3]]
## [1] "i" "n" "h" "y" "p" "o" "f" "a" "e" "w" "l" "t" "m" "v" "c" "g" "k" "s" "z"
## [20] "b" "r" "u" "d" "j" "x" "q"
new_list <- list(my_list[[1]][2,2],my_list[[2]][2],my_list[[3]][2])
new_list
## [[1]]
## [1] 0.3748043
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "n"
types <- c(typeof(new_list[[1]]),typeof(new_list[[2]]),typeof(new_list[[3]]))
types # Checks out
## [1] "double" "logical" "character"
atomic <- c(new_list[[1]],new_list[[2]],new_list[[3]])
atomic
## [1] "0.374804298160598" "TRUE" "n"
typeof(atomic) # all elements were coerced into character strings
## [1] "character"
Question 3
my_unis <- runif(26,0,10)
my_letters <- sample(LETTERS,26)
df <- data.frame(my_unis,my_letters)
df[sample(26,4),1] <- NA
df
## my_unis my_letters
## 1 7.2431326 X
## 2 2.2252127 G
## 3 3.4435705 O
## 4 9.1243402 P
## 5 NA U
## 6 NA T
## 7 2.4828002 D
## 8 3.3445484 F
## 9 0.9529286 J
## 10 NA Z
## 11 NA A
## 12 5.4708735 C
## 13 4.0778076 M
## 14 0.1700216 I
## 15 1.7489257 B
## 16 7.9016572 S
## 17 8.8043788 V
## 18 2.0261596 Q
## 19 3.0012703 E
## 20 6.0119501 L
## 21 0.3385701 N
## 22 8.5199808 Y
## 23 5.4151657 W
## 24 9.3782360 H
## 25 2.4651892 K
## 26 4.9867148 R
which(is.na(df[,1]))
## [1] 5 6 10 11
df[,2] <- sort(df[,2])
df
## my_unis my_letters
## 1 7.2431326 A
## 2 2.2252127 B
## 3 3.4435705 C
## 4 9.1243402 D
## 5 NA E
## 6 NA F
## 7 2.4828002 G
## 8 3.3445484 H
## 9 0.9529286 I
## 10 NA J
## 11 NA K
## 12 5.4708735 L
## 13 4.0778076 M
## 14 0.1700216 N
## 15 1.7489257 O
## 16 7.9016572 P
## 17 8.8043788 Q
## 18 2.0261596 R
## 19 3.0012703 S
## 20 6.0119501 T
## 21 0.3385701 U
## 22 8.5199808 V
## 23 5.4151657 W
## 24 9.3782360 X
## 25 2.4651892 Y
## 26 4.9867148 Z
mean(df[,1],na.rm=T)
## [1] 4.506065