Problem 1

#create vector of consecutive int 1:ndims^2
n_dims<-sample(3:10,1,replace=TRUE)
print(n_dims)
## [1] 8
n_dims2<-n_dims^2
print(n_dims2)
## [1] 64
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 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## [51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#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]
## [1,]    2   40   16   30   52   18   49   37
## [2,]   64   44   50   63   55    8   10    3
## [3,]   42   48   36   29   27   58   32   33
## [4,]   28   24    7   21   23   46   51   54
## [5,]   22   57    9   34   19   38   11    5
## [6,]   31   20   15   62   26   61   45   39
## [7,]   12   35    4    6   53   17   13   56
## [8,]   41   60   47    1   14   25   43   59
#transposing the matrix via t() function 
m_transposed<-t(m)
print(m_transposed)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,]    2   64   42   28   22   31   12   41
## [2,]   40   44   48   24   57   20   35   60
## [3,]   16   50   36    7    9   15    4   47
## [4,]   30   63   29   21   34   62    6    1
## [5,]   52   55   27   23   19   26   53   14
## [6,]   18    8   58   46   38   61   17   25
## [7,]   49   10   32   51   11   45   13   43
## [8,]   37    3   33   54    5   39   56   59
#sum of elements in first row
row1_sum<-sum(m_transposed[1,])
print(row1_sum)
## [1] 242
#sum of elements in last row 
row4_sum<-sum(m_transposed[4,]) # This indexing needs to be vectorized
print(row4_sum)
## [1] 246
#exploring eigen
eigen_m_transposed<-eigen(m_transposed, symmetric=FALSE,only.values=FALSE)
print(eigen_m_transposed)
## eigen() decomposition
## $values
## [1] 259.99618+ 0.00000i  32.99080+39.88579i  32.99080-39.88579i
## [4] -40.15669+ 0.00000i -16.82708+29.73571i -16.82708-29.73571i
## [7]  25.80405+ 0.00000i -22.97097+ 0.00000i
## 
## $vectors
##               [,1]                    [,2]                    [,3]
## [1,] -0.3369127+0i -0.13196987-0.13653011i -0.13196987+0.13653011i
## [2,] -0.4441397+0i -0.09701426-0.24292390i -0.09701426+0.24292390i
## [3,] -0.2560692+0i -0.17252766-0.40849957i -0.17252766+0.40849957i
## [4,] -0.3429213+0i -0.27110293+0.32914163i -0.27110293-0.32914163i
## [5,] -0.3679333+0i -0.16557552+0.05370035i -0.16557552-0.05370035i
## [6,] -0.3477492+0i  0.04294835+0.34727703i  0.04294835-0.34727703i
## [7,] -0.3333630+0i  0.18563864+0.17052093i  0.18563864-0.17052093i
## [8,] -0.3723277+0i  0.54842230+0.00000000i  0.54842230+0.00000000i
##                [,4]                    [,5]                    [,6]
## [1,]  0.35018101+0i -0.35044184-0.16007567i -0.35044184+0.16007567i
## [2,] -0.39672582+0i  0.11225551-0.24084894i  0.11225551+0.24084894i
## [3,]  0.18396104+0i -0.42838728+0.00000000i -0.42838728+0.00000000i
## [4,]  0.34311168+0i -0.31143235-0.02758849i -0.31143235+0.02758849i
## [5,]  0.43494385+0i  0.20594730+0.25127707i  0.20594730-0.25127707i
## [6,] -0.38334093+0i  0.33664264+0.01698993i  0.33664264-0.01698993i
## [7,] -0.48078823+0i  0.03556999+0.36714631i  0.03556999-0.36714631i
## [8,]  0.03362812+0i  0.37785799-0.04099044i  0.37785799+0.04099044i
##                 [,7]           [,8]
## [1,] -0.030119553+0i  0.29688512+0i
## [2,] -0.226590114+0i  0.01965511+0i
## [3,]  0.570608556+0i -0.36022125+0i
## [4,] -0.553509844+0i -0.65170907+0i
## [5,] -0.471356721+0i -0.05730068+0i
## [6,]  0.189924101+0i  0.50585217+0i
## [7,] -0.002830901+0i -0.12397919+0i
## [8,]  0.239846043+0i  0.28713798+0i
print(eigen_m_transposed$values)
## [1] 259.99618+ 0.00000i  32.99080+39.88579i  32.99080-39.88579i
## [4] -40.15669+ 0.00000i -16.82708+29.73571i -16.82708-29.73571i
## [7]  25.80405+ 0.00000i -22.97097+ 0.00000i
print(eigen_m_transposed$vector)
##               [,1]                    [,2]                    [,3]
## [1,] -0.3369127+0i -0.13196987-0.13653011i -0.13196987+0.13653011i
## [2,] -0.4441397+0i -0.09701426-0.24292390i -0.09701426+0.24292390i
## [3,] -0.2560692+0i -0.17252766-0.40849957i -0.17252766+0.40849957i
## [4,] -0.3429213+0i -0.27110293+0.32914163i -0.27110293-0.32914163i
## [5,] -0.3679333+0i -0.16557552+0.05370035i -0.16557552-0.05370035i
## [6,] -0.3477492+0i  0.04294835+0.34727703i  0.04294835-0.34727703i
## [7,] -0.3333630+0i  0.18563864+0.17052093i  0.18563864-0.17052093i
## [8,] -0.3723277+0i  0.54842230+0.00000000i  0.54842230+0.00000000i
##                [,4]                    [,5]                    [,6]
## [1,]  0.35018101+0i -0.35044184-0.16007567i -0.35044184+0.16007567i
## [2,] -0.39672582+0i  0.11225551-0.24084894i  0.11225551+0.24084894i
## [3,]  0.18396104+0i -0.42838728+0.00000000i -0.42838728+0.00000000i
## [4,]  0.34311168+0i -0.31143235-0.02758849i -0.31143235+0.02758849i
## [5,]  0.43494385+0i  0.20594730+0.25127707i  0.20594730-0.25127707i
## [6,] -0.38334093+0i  0.33664264+0.01698993i  0.33664264-0.01698993i
## [7,] -0.48078823+0i  0.03556999+0.36714631i  0.03556999-0.36714631i
## [8,]  0.03362812+0i  0.37785799-0.04099044i  0.37785799+0.04099044i
##                 [,7]           [,8]
## [1,] -0.030119553+0i  0.29688512+0i
## [2,] -0.226590114+0i  0.01965511+0i
## [3,]  0.570608556+0i -0.36022125+0i
## [4,] -0.553509844+0i -0.65170907+0i
## [5,] -0.471356721+0i -0.05730068+0i
## [6,]  0.189924101+0i  0.50585217+0i
## [7,] -0.002830901+0i -0.12397919+0i
## [8,]  0.239846043+0i  0.28713798+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.34123608 0.31728139 0.23391515 0.89321565 0.96694475 0.22061073
##  [7] 0.44416242 0.08526605 0.45121222 0.42501580 0.34328200 0.71953230
## [13] 0.23417908 0.36363979 0.09223501 0.11044059
my_matrix<-matrix(data=ran_nums,nrow=4)
print(my_matrix)
##           [,1]       [,2]      [,3]       [,4]
## [1,] 0.3412361 0.96694475 0.4512122 0.23417908
## [2,] 0.3172814 0.22061073 0.4250158 0.36363979
## [3,] 0.2339152 0.44416242 0.3432820 0.09223501
## [4,] 0.8932157 0.08526605 0.7195323 0.11044059
#setting up logical vector 
my_vec<-runif(100)
print(my_vec)
##   [1] 0.055662853 0.358843010 0.394013800 0.606693717 0.823782476 0.845341045
##   [7] 0.163775091 0.710463488 0.269343998 0.902914635 0.117033961 0.795291981
##  [13] 0.379682068 0.063289981 0.708690422 0.497963710 0.557892766 0.859751834
##  [19] 0.380311174 0.851418529 0.369146110 0.585962800 0.726309430 0.518485208
##  [25] 0.596533214 0.178404082 0.486518828 0.655445585 0.819535977 0.865398607
##  [31] 0.215451475 0.523030014 0.236790237 0.002991203 0.052706758 0.034140383
##  [37] 0.484105828 0.308096161 0.616261254 0.235833700 0.930644440 0.052927766
##  [43] 0.017735572 0.788595066 0.055041954 0.581541821 0.846498143 0.719959300
##  [49] 0.725901863 0.443574517 0.132498864 0.587615341 0.561512916 0.288230835
##  [55] 0.474569759 0.519984851 0.541424591 0.892640205 0.901641506 0.601480458
##  [61] 0.422572416 0.219087600 0.576328384 0.294696980 0.268617409 0.640722939
##  [67] 0.245318910 0.254390221 0.272949517 0.467990584 0.572663390 0.370191558
##  [73] 0.556041574 0.920957847 0.435622696 0.804049841 0.008193215 0.950828146
##  [79] 0.488763630 0.037466196 0.847213561 0.456683338 0.555625094 0.659788160
##  [85] 0.781676905 0.730829437 0.318497128 0.409201699 0.725990058 0.595865374
##  [91] 0.088028586 0.874363649 0.365558384 0.603713819 0.894088338 0.927403840
##  [97] 0.810320475 0.432570042 0.582156119 0.092201290
my_logical<-(my_vec <0.5)
print(my_logical)
##   [1]  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE
##  [13]  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
##  [25] FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [37]  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE
##  [49] FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [61]  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
##  [73] FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE
##  [85] FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
##  [97] FALSE  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"
## [20] "t" "u" "v" "w" "x" "y" "z"
my_letters<-sample(x=my_alph)
print(my_letters)
##  [1] "t" "w" "o" "z" "r" "f" "p" "s" "x" "u" "b" "a" "l" "n" "j" "e" "m" "k" "c"
## [20] "i" "y" "g" "v" "d" "h" "q"
#new list 
new_list<-list(my_matrix[2,2],my_logical[2],my_letters[2])
print(new_list)
## [[1]]
## [1] 0.2206107
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] "w"
#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.220610730350018" "TRUE"              "w"
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] "N" "P" "B" "W" "Z" "S" "G" "I" "H" "K" "E" "C" "X" "R" "O" "M" "U" "Q" "F"
## [20] "L" "A" "D" "V" "T" "Y" "J"
d_frame<-data.frame(my_unis,my_letters)
print(d_frame)
##       my_unis my_letters
## 1  5.76765577          N
## 2  0.05329695          P
## 3  2.92975149          B
## 4  6.13598001          W
## 5  3.17470945          Z
## 6  9.57895243          S
## 7  8.28904993          G
## 8  2.71415219          I
## 9  0.75100800          H
## 10 7.17143444          K
## 11 8.90098474          E
## 12 9.85314433          C
## 13 2.52024793          X
## 14 6.42724293          R
## 15 9.00025405          O
## 16 9.04972590          M
## 17 9.29544833          U
## 18 0.74184256          Q
## 19 1.41052297          F
## 20 8.74967048          L
## 21 1.29515402          A
## 22 7.94036287          D
## 23 8.51257193          V
## 24 1.83076980          T
## 25 0.94957524          Y
## 26 6.57068564          J
#Filling random entries with 'NA'

d_frame[sample(1:26, size=4, replace=FALSE),1]<-NA
print(d_frame)
##       my_unis my_letters
## 1  5.76765577          N
## 2  0.05329695          P
## 3          NA          B
## 4          NA          W
## 5  3.17470945          Z
## 6  9.57895243          S
## 7  8.28904993          G
## 8          NA          I
## 9  0.75100800          H
## 10 7.17143444          K
## 11 8.90098474          E
## 12 9.85314433          C
## 13 2.52024793          X
## 14         NA          R
## 15 9.00025405          O
## 16 9.04972590          M
## 17 9.29544833          U
## 18 0.74184256          Q
## 19 1.41052297          F
## 20 8.74967048          L
## 21 1.29515402          A
## 22 7.94036287          D
## 23 8.51257193          V
## 24 1.83076980          T
## 25 0.94957524          Y
## 26 6.57068564          J
#looking for missing value 
which(!complete.cases(d_frame))
## [1]  3  4  8 14
#rearranging data frame to be in alphabetical order 

df_ordered<-d_frame[order(d_frame$my_letters),]
print(df_ordered)
##       my_unis my_letters
## 21 1.29515402          A
## 3          NA          B
## 12 9.85314433          C
## 22 7.94036287          D
## 11 8.90098474          E
## 19 1.41052297          F
## 7  8.28904993          G
## 9  0.75100800          H
## 8          NA          I
## 26 6.57068564          J
## 10 7.17143444          K
## 20 8.74967048          L
## 16 9.04972590          M
## 1  5.76765577          N
## 15 9.00025405          O
## 2  0.05329695          P
## 18 0.74184256          Q
## 14         NA          R
## 6  9.57895243          S
## 24 1.83076980          T
## 17 9.29544833          U
## 23 8.51257193          V
## 4          NA          W
## 13 2.52024793          X
## 25 0.94957524          Y
## 5  3.17470945          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  5.76765577          N
## 2  0.05329695          P
## 5  3.17470945          Z
## 6  9.57895243          S
## 7  8.28904993          G
## 9  0.75100800          H
## 10 7.17143444          K
## 11 8.90098474          E
## 12 9.85314433          C
## 13 2.52024793          X
## 15 9.00025405          O
## 16 9.04972590          M
## 17 9.29544833          U
## 18 0.74184256          Q
## 19 1.41052297          F
## 20 8.74967048          L
## 21 1.29515402          A
## 22 7.94036287          D
## 23 8.51257193          V
## 24 1.83076980          T
## 25 0.94957524          Y
## 26 6.57068564          J
col_mean<-mean(d_frame2$my_unis)
print(col_mean)
## [1] 5.518503