Problem 1
#create vector of consecutive int 1:ndims^2
n_dims<-sample(3:10,1,replace=TRUE)
print(n_dims)
## [1] 10
n_dims2<-n_dims^2
print(n_dims2)
## [1] 100
n_dims_vec<-seq(1:n_dims2)
print(n_dims_vec)
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
## [23] 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
## [45] 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
## [67] 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
## [89] 89 90 91 92 93 94 95 96 97 98 99 100
#sampling
ran_vec<-sample(x=n_dims_vec)
#creating square matrix
m<- matrix(data=ran_vec,nrow=sqrt(n_dims2))
print(m)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 51 5 44 12 82 36 45 64 32 20
## [2,] 2 19 21 92 83 7 15 60 27 8
## [3,] 1 56 17 80 18 11 47 97 4 25
## [4,] 40 58 24 69 94 16 91 39 28 43
## [5,] 55 88 79 67 31 98 72 42 37 99
## [6,] 87 34 59 41 77 54 95 74 78 46
## [7,] 53 52 14 38 84 89 23 81 93 75
## [8,] 62 85 68 96 65 70 49 66 76 9
## [9,] 30 73 90 33 6 71 63 10 13 26
## [10,] 61 86 22 35 100 29 50 57 3 48
#transposing the matrix via t() function
m_transposed<-t(m)
print(m_transposed)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 51 2 1 40 55 87 53 62 30 61
## [2,] 5 19 56 58 88 34 52 85 73 86
## [3,] 44 21 17 24 79 59 14 68 90 22
## [4,] 12 92 80 69 67 41 38 96 33 35
## [5,] 82 83 18 94 31 77 84 65 6 100
## [6,] 36 7 11 16 98 54 89 70 71 29
## [7,] 45 15 47 91 72 95 23 49 63 50
## [8,] 64 60 97 39 42 74 81 66 10 57
## [9,] 32 27 4 28 37 78 93 76 13 3
## [10,] 20 8 25 43 99 46 75 9 26 48
#sum of elements in first row
row1_sum<-sum(m_transposed[1,])
print(row1_sum)
## [1] 442
#sum of elements in last row
row4_sum<-sum(m_transposed[4,]) # This indexing needs to be vectorized
print(row4_sum)
## [1] 563
#exploring eigen
eigen_m_transposed<-eigen(m_transposed, symmetric=FALSE,only.values=FALSE)
print(eigen_m_transposed)
## eigen() decomposition
## $values
## [1] 515.46473+ 0.00000i -78.72457+22.00125i -78.72457-22.00125i -42.97375+54.20082i
## [5] -42.97375-54.20082i 43.38933+26.24350i 43.38933-26.24350i 28.52346+15.63043i
## [9] 28.52346-15.63043i -24.89368+ 0.00000i
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] 0.2709175+0i 0.020066654+0.18141268i 0.020066654-0.18141268i 0.040004691+0.22909351i
## [2,] 0.3415655+0i -0.280790605+0.22583586i -0.280790605-0.22583586i -0.125789011+0.28833741i
## [3,] 0.2679167+0i -0.092428091+0.20704405i -0.092428091-0.20704405i 0.525804326+0.00000000i
## [4,] 0.3559121+0i 0.006691387-0.04604802i 0.006691387+0.04604802i 0.042625056-0.02248971i
## [5,] 0.3925010+0i 0.413744351-0.08098855i 0.413744351+0.08098855i -0.008444965+0.20141856i
## [6,] 0.3016782+0i -0.482001584+0.00000000i -0.482001584+0.00000000i 0.088180236+0.15683347i
## [7,] 0.3352917+0i 0.250599854+0.19417457i 0.250599854-0.19417457i 0.164571437-0.28342738i
## [8,] 0.3571770+0i 0.300206738-0.28553311i 0.300206738+0.28553311i -0.371029874-0.34146368i
## [9,] 0.2508946+0i -0.154071707-0.08530393i -0.154071707+0.08530393i -0.117314548+0.22297443i
## [10,] 0.2519389+0i -0.234464764-0.12268356i -0.234464764+0.12268356i -0.123965248-0.23372034i
## [,5] [,6] [,7]
## [1,] 0.040004691-0.22909351i -0.184552935+0.309460317i -0.184552935-0.309460317i
## [2,] -0.125789011-0.28833741i -0.057598280-0.117682497i -0.057598280+0.117682497i
## [3,] 0.525804326+0.00000000i 0.179195374+0.102035524i 0.179195374-0.102035524i
## [4,] 0.042625056+0.02248971i 0.333818409-0.241746760i 0.333818409+0.241746760i
## [5,] -0.008444965-0.20141856i -0.353136684+0.084936958i -0.353136684-0.084936958i
## [6,] 0.088180236-0.15683347i 0.005905407+0.001892896i 0.005905407-0.001892896i
## [7,] 0.164571437+0.28342738i 0.166170512-0.202753484i 0.166170512+0.202753484i
## [8,] -0.371029874+0.34146368i 0.144378021+0.232830505i 0.144378021-0.232830505i
## [9,] -0.117314548-0.22297443i 0.323578084-0.197369577i 0.323578084+0.197369577i
## [10,] -0.123965248+0.23372034i -0.470336984+0.000000000i -0.470336984+0.000000000i
## [,8] [,9] [,10]
## [1,] -0.41331985+0.087597115i -0.41331985-0.087597115i 0.45386193+0i
## [2,] 0.08185502-0.126504565i 0.08185502+0.126504565i -0.32351979+0i
## [3,] 0.03408078+0.090046657i 0.03408078-0.090046657i 0.07176513+0i
## [4,] 0.22273626+0.164084396i 0.22273626-0.164084396i 0.32983384+0i
## [5,] -0.38002947-0.065040016i -0.38002947+0.065040016i -0.01729958+0i
## [6,] 0.14308290-0.079341353i 0.14308290+0.079341353i -0.65664379+0i
## [7,] 0.33295793-0.001905399i 0.33295793+0.001905399i 0.27865800+0i
## [8,] -0.19704248+0.171189281i -0.19704248-0.171189281i 0.08900842+0i
## [9,] 0.50217212+0.000000000i 0.50217212+0.000000000i 0.11871718+0i
## [10,] -0.21685760-0.243647744i -0.21685760+0.243647744i -0.21037302+0i
print(eigen_m_transposed$values)
## [1] 515.46473+ 0.00000i -78.72457+22.00125i -78.72457-22.00125i -42.97375+54.20082i
## [5] -42.97375-54.20082i 43.38933+26.24350i 43.38933-26.24350i 28.52346+15.63043i
## [9] 28.52346-15.63043i -24.89368+ 0.00000i
print(eigen_m_transposed$vector)
## [,1] [,2] [,3] [,4]
## [1,] 0.2709175+0i 0.020066654+0.18141268i 0.020066654-0.18141268i 0.040004691+0.22909351i
## [2,] 0.3415655+0i -0.280790605+0.22583586i -0.280790605-0.22583586i -0.125789011+0.28833741i
## [3,] 0.2679167+0i -0.092428091+0.20704405i -0.092428091-0.20704405i 0.525804326+0.00000000i
## [4,] 0.3559121+0i 0.006691387-0.04604802i 0.006691387+0.04604802i 0.042625056-0.02248971i
## [5,] 0.3925010+0i 0.413744351-0.08098855i 0.413744351+0.08098855i -0.008444965+0.20141856i
## [6,] 0.3016782+0i -0.482001584+0.00000000i -0.482001584+0.00000000i 0.088180236+0.15683347i
## [7,] 0.3352917+0i 0.250599854+0.19417457i 0.250599854-0.19417457i 0.164571437-0.28342738i
## [8,] 0.3571770+0i 0.300206738-0.28553311i 0.300206738+0.28553311i -0.371029874-0.34146368i
## [9,] 0.2508946+0i -0.154071707-0.08530393i -0.154071707+0.08530393i -0.117314548+0.22297443i
## [10,] 0.2519389+0i -0.234464764-0.12268356i -0.234464764+0.12268356i -0.123965248-0.23372034i
## [,5] [,6] [,7]
## [1,] 0.040004691-0.22909351i -0.184552935+0.309460317i -0.184552935-0.309460317i
## [2,] -0.125789011-0.28833741i -0.057598280-0.117682497i -0.057598280+0.117682497i
## [3,] 0.525804326+0.00000000i 0.179195374+0.102035524i 0.179195374-0.102035524i
## [4,] 0.042625056+0.02248971i 0.333818409-0.241746760i 0.333818409+0.241746760i
## [5,] -0.008444965-0.20141856i -0.353136684+0.084936958i -0.353136684-0.084936958i
## [6,] 0.088180236-0.15683347i 0.005905407+0.001892896i 0.005905407-0.001892896i
## [7,] 0.164571437+0.28342738i 0.166170512-0.202753484i 0.166170512+0.202753484i
## [8,] -0.371029874+0.34146368i 0.144378021+0.232830505i 0.144378021-0.232830505i
## [9,] -0.117314548-0.22297443i 0.323578084-0.197369577i 0.323578084+0.197369577i
## [10,] -0.123965248+0.23372034i -0.470336984+0.000000000i -0.470336984+0.000000000i
## [,8] [,9] [,10]
## [1,] -0.41331985+0.087597115i -0.41331985-0.087597115i 0.45386193+0i
## [2,] 0.08185502-0.126504565i 0.08185502+0.126504565i -0.32351979+0i
## [3,] 0.03408078+0.090046657i 0.03408078-0.090046657i 0.07176513+0i
## [4,] 0.22273626+0.164084396i 0.22273626-0.164084396i 0.32983384+0i
## [5,] -0.38002947-0.065040016i -0.38002947+0.065040016i -0.01729958+0i
## [6,] 0.14308290-0.079341353i 0.14308290+0.079341353i -0.65664379+0i
## [7,] 0.33295793-0.001905399i 0.33295793+0.001905399i 0.27865800+0i
## [8,] -0.19704248+0.171189281i -0.19704248-0.171189281i 0.08900842+0i
## [9,] 0.50217212+0.000000000i 0.50217212+0.000000000i 0.11871718+0i
## [10,] -0.21685760-0.243647744i -0.21685760+0.243647744i -0.21037302+0i
#These are generating complex numbers, with negative and positive real parts as well as the imaginary part. I found that depending on the value of the samples sequence that began this matrix, we may not generate complex numbers. Rather, they may end up as real numbers only.
typeof(eigen_m_transposed$values)
## [1] "complex"
typeof(eigen_m_transposed$vectors)
## [1] "complex"
Problem 2
#creating random 4x4 matrix
ran_nums<-runif(16)
print(ran_nums)
## [1] 0.7865743 0.2305713 0.9746150 0.8550059 0.8548788 0.6740558 0.1405941 0.3306175 0.5470876
## [10] 0.7244528 0.6094610 0.1712554 0.3822704 0.6735868 0.1822605 0.5920377
my_matrix<-matrix(data=ran_nums,nrow=4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 0.7865743 0.8548788 0.5470876 0.3822704
## [2,] 0.2305713 0.6740558 0.7244528 0.6735868
## [3,] 0.9746150 0.1405941 0.6094610 0.1822605
## [4,] 0.8550059 0.3306175 0.1712554 0.5920377
#setting up logical vector
my_vec<-runif(100)
print(my_vec)
## [1] 0.219500172 0.245817682 0.899537302 0.993948224 0.406226936 0.734983398 0.788708123
## [8] 0.465707403 0.017685138 0.717580555 0.598634224 0.792402399 0.683159258 0.720576810
## [15] 0.630574635 0.516865939 0.978462220 0.758893257 0.715899467 0.320663697 0.893421874
## [22] 0.554016126 0.752780522 0.443822710 0.884017248 0.276147319 0.475274787 0.272479762
## [29] 0.042043897 0.640192103 0.308827885 0.783396096 0.027871577 0.044090010 0.519338559
## [36] 0.290079784 0.315258813 0.964660115 0.454407656 0.244247688 0.410937354 0.952312871
## [43] 0.554260746 0.028220206 0.619009248 0.251979183 0.624283146 0.943897791 0.832062578
## [50] 0.023218959 0.743864926 0.762918715 0.514878405 0.597770066 0.785385541 0.908698756
## [57] 0.507936536 0.582142891 0.768327001 0.591043048 0.020285135 0.460094646 0.859036702
## [64] 0.461669372 0.948105286 0.744938513 0.184229170 0.254210542 0.542320606 0.394157788
## [71] 0.485059566 0.657350800 0.236558826 0.722824400 0.376602400 0.668011262 0.437107106
## [78] 0.211238722 0.924250767 0.533343747 0.693023656 0.052253001 0.923327900 0.001912357
## [85] 0.225844482 0.627472954 0.990073623 0.316586472 0.866666591 0.264957682 0.846882118
## [92] 0.109800275 0.419494400 0.774050133 0.648540362 0.701227191 0.490587364 0.264397603
## [99] 0.552954167 0.281337406
my_logical<-(my_vec <0.5)
print(my_logical)
## [1] TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [16] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE TRUE TRUE FALSE
## [31] TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE
## [46] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [61] TRUE TRUE FALSE TRUE FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE
## [76] FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE
## [91] FALSE TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE TRUE
#setting up my letters
my_alph<-letters
print(my_alph)
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v"
## [23] "w" "x" "y" "z"
my_letters<-sample(x=my_alph)
print(my_letters)
## [1] "r" "n" "c" "z" "m" "g" "k" "a" "y" "i" "q" "j" "d" "o" "v" "f" "t" "w" "h" "e" "l" "s"
## [23] "b" "x" "p" "u"
#new list
new_list<-list(my_matrix[2,2],my_logical[2],my_letters[2])
print(new_list)
## [[1]]
## [1] 0.6740558
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "n"
#exploring structure
typeof(new_list)
## [1] "list"
#creating atomic vector
new_vec<-c(my_matrix[2,2],my_logical[2],my_letters[2])
print(new_vec)
## [1] "0.674055781913921" "TRUE" "n"
typeof(new_vec)
## [1] "character"
Problem 3
#creating data frame
my_unis<-runif(26,min=0,max=10)
my_letters<-sample(LETTERS)
print(my_letters)
## [1] "B" "J" "H" "G" "I" "F" "D" "Z" "W" "R" "O" "C" "U" "Q" "A" "Y" "L" "P" "S" "N" "M" "X"
## [23] "K" "T" "V" "E"
d_frame<-data.frame(my_unis,my_letters)
print(d_frame)
## my_unis my_letters
## 1 4.846598289 B
## 2 5.628513377 J
## 3 4.541730541 H
## 4 5.485230531 G
## 5 6.797263732 I
## 6 4.795097793 F
## 7 4.132910033 D
## 8 0.004729037 Z
## 9 8.380532188 W
## 10 2.550548050 R
## 11 5.970473439 O
## 12 9.533540800 C
## 13 1.533701031 U
## 14 8.425673323 Q
## 15 3.800795635 A
## 16 4.326085581 Y
## 17 1.866174859 L
## 18 0.151518488 P
## 19 2.003532536 S
## 20 8.146777186 N
## 21 1.835534316 M
## 22 0.476375290 X
## 23 9.696539417 K
## 24 2.209684418 T
## 25 5.072184470 V
## 26 2.557052858 E
#Filling random entries with 'NA'
d_frame[sample(1:26, size=4, replace=FALSE),1]<-NA
print(d_frame)
## my_unis my_letters
## 1 4.8465983 B
## 2 5.6285134 J
## 3 4.5417305 H
## 4 5.4852305 G
## 5 NA I
## 6 4.7950978 F
## 7 4.1329100 D
## 8 NA Z
## 9 8.3805322 W
## 10 2.5505480 R
## 11 5.9704734 O
## 12 9.5335408 C
## 13 1.5337010 U
## 14 8.4256733 Q
## 15 NA A
## 16 4.3260856 Y
## 17 1.8661749 L
## 18 0.1515185 P
## 19 2.0035325 S
## 20 8.1467772 N
## 21 1.8355343 M
## 22 0.4763753 X
## 23 9.6965394 K
## 24 2.2096844 T
## 25 5.0721845 V
## 26 NA E
#looking for missing value
which(!complete.cases(d_frame))
## [1] 5 8 15 26
#rearranging data frame to be in alphabetical order
df_ordered<-d_frame[order(d_frame$my_letters),]
print(df_ordered)
## my_unis my_letters
## 15 NA A
## 1 4.8465983 B
## 12 9.5335408 C
## 7 4.1329100 D
## 26 NA E
## 6 4.7950978 F
## 4 5.4852305 G
## 3 4.5417305 H
## 5 NA I
## 2 5.6285134 J
## 23 9.6965394 K
## 17 1.8661749 L
## 21 1.8355343 M
## 20 8.1467772 N
## 11 5.9704734 O
## 18 0.1515185 P
## 14 8.4256733 Q
## 10 2.5505480 R
## 19 2.0035325 S
## 24 2.2096844 T
## 13 1.5337010 U
## 25 5.0721845 V
## 9 8.3805322 W
## 22 0.4763753 X
## 16 4.3260856 Y
## 8 NA Z
#calculating column mean of first variable
#first make data frame without NA
d_frame2<-d_frame[which(complete.cases(d_frame)),]
print(d_frame2)
## my_unis my_letters
## 1 4.8465983 B
## 2 5.6285134 J
## 3 4.5417305 H
## 4 5.4852305 G
## 6 4.7950978 F
## 7 4.1329100 D
## 9 8.3805322 W
## 10 2.5505480 R
## 11 5.9704734 O
## 12 9.5335408 C
## 13 1.5337010 U
## 14 8.4256733 Q
## 16 4.3260856 Y
## 17 1.8661749 L
## 18 0.1515185 P
## 19 2.0035325 S
## 20 8.1467772 N
## 21 1.8355343 M
## 22 0.4763753 X
## 23 9.6965394 K
## 24 2.2096844 T
## 25 5.0721845 V
col_mean<-mean(d_frame2$my_unis)
print(col_mean)
## [1] 4.618589