Problem 1

#create n_dims and assign it a random integer 

n_dims <- sample(3:10,1,replace=TRUE)
print(n_dims)
## [1] 10
#create a vector of consecutive integers from 1 to n_dims

nd_c <- (1:(n_dims)^2)

print(nd_c)
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
#use sample to randomly reshuffle

nd_cs <- sample(nd_c,length(nd_c),replace=FALSE)

print(nd_cs)
##   [1]  41  82  56  74  17  20  94  11  10  47  86   8  69  62  22  33  84  98
##  [19]  26  60  55  99  42   3  32  67  16  81  91  93  38  96  49   7  34  92
##  [37]  36  18  95  76  13  14  63  46  31  43   9  30  90  85  29  77  53  39
##  [55]  89  61  64  51  68  65  12  88  97  21  66  79  71  75   2  54  52  50
##  [73]  23  83  45 100   1  87  72   5  48  28   6  80  44  35  19  27  59  25
##  [91]  24   4  40  57  15  37  78  58  70  73
str(nd_cs)
##  int [1:100] 41 82 56 74 17 20 94 11 10 47 ...
#create a square matrix with these values

#now we can make a square matrix

sm <- matrix(data = nd_cs,nrow = sqrt(length(nd_cs)) )

#print out the matrix

print(sm)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]   41   86   55   38   13   29   12   52   48    24
##  [2,]   82    8   99   96   14   77   88   50   28     4
##  [3,]   56   69   42   49   63   53   97   23    6    40
##  [4,]   74   62    3    7   46   39   21   83   80    57
##  [5,]   17   22   32   34   31   89   66   45   44    15
##  [6,]   20   33   67   92   43   61   79  100   35    37
##  [7,]   94   84   16   36    9   64   71    1   19    78
##  [8,]   11   98   81   18   30   51   75   87   27    58
##  [9,]   10   26   91   95   90   68    2   72   59    70
## [10,]   47   60   93   76   85   65   54    5   25    73
#the simplest way to transpose a matrix, flip it, is just t (like in matlab)

sm.t <- t(sm)

#lets look at the before and after

print(sm)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]   41   86   55   38   13   29   12   52   48    24
##  [2,]   82    8   99   96   14   77   88   50   28     4
##  [3,]   56   69   42   49   63   53   97   23    6    40
##  [4,]   74   62    3    7   46   39   21   83   80    57
##  [5,]   17   22   32   34   31   89   66   45   44    15
##  [6,]   20   33   67   92   43   61   79  100   35    37
##  [7,]   94   84   16   36    9   64   71    1   19    78
##  [8,]   11   98   81   18   30   51   75   87   27    58
##  [9,]   10   26   91   95   90   68    2   72   59    70
## [10,]   47   60   93   76   85   65   54    5   25    73
print(sm.t)
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]   41   82   56   74   17   20   94   11   10    47
##  [2,]   86    8   69   62   22   33   84   98   26    60
##  [3,]   55   99   42    3   32   67   16   81   91    93
##  [4,]   38   96   49    7   34   92   36   18   95    76
##  [5,]   13   14   63   46   31   43    9   30   90    85
##  [6,]   29   77   53   39   89   61   64   51   68    65
##  [7,]   12   88   97   21   66   79   71   75    2    54
##  [8,]   52   50   23   83   45  100    1   87   72     5
##  [9,]   48   28    6   80   44   35   19   27   59    25
## [10,]   24    4   40   57   15   37   78   58   70    73
#interesting, so now the first row becomes the first column and the second row the second column, etc.

#calculate the sum and mean of the elements of the first row and last row

rsum1 <- sum(sm.t[1,])
rsumL <- sum(sm.t[nrow(sm.t),])

rmean1 <- mean(sm.t[1,])
rmeanL <- mean(sm.t[nrow(sm.t),])

#now let's learn about eigen values (oh boy.)

#note this is commented out for the markdown but ran in practice
# ?eigen()

#the most important part of this is that we need to tell it whether our matrix is symmetrical or not. It is not, so symmetric will be FALSE

#this will calculate both the eigen values and eigen vectors

sm.t.Vec <- eigen(sm,symmetric=FALSE,only.values = FALSE)

str(sm.t.Vec)
## List of 2
##  $ values : cplx [1:10] 505.4+0i 48.3+67.6i 48.3-67.6i ...
##  $ vectors: cplx [1:10, 1:10] 0.252+0i 0.33+0i 0.299+0i ...
##  - attr(*, "class")= chr "eigen"
#ah so the output is a list and within that list the values, the values (output) are complex numbers and the vectors are a matrix of complex numbers let's verify

typeof(sm.t.Vec$values)
## [1] "complex"
typeof(sm.t.Vec$vectors)
## [1] "complex"
#they complex. 

#just for fun lets separate them

sm.t.val <- sm.t.Vec$values
sm.t.vectors <- sm.t.Vec$vectors

#now lets' run the code again. First we'll store the old matrix to compare
# 
# sm1 <- sm

#and we commented the previous line out so it doesn't override

#rerun the code

#new sm

sm2 <- sm

#this time we had a 3x3 matrix which gave very different values which were not complex, they were doubles

Problem 2

#it's time to make lists babes! Let's go!!!!!

# first element of the list

#we need to create a matrix filled with random uniform values. we'll generate some values first (we need 16)

rv <- runif(16,min = 1,max = 100)

#make the matrix
my_matrix <- matrix(data = rv,nrow = sqrt(length(rv)))
#check it
print(my_matrix)
##          [,1]     [,2]     [,3]     [,4]
## [1,] 56.07112 13.11969 75.17794 32.66987
## [2,] 20.40206 75.45032 81.50903 66.18436
## [3,] 52.32097 31.25523 14.84807 33.59501
## [4,] 51.12763 87.07333 44.63276 14.62767
#looks good moving on

#element two, a logical.

#we need to create a series of 100 TRUE or FALSE values.

#I'm a fan of binary so let's go with that

#first we'll create a 100 element binary vector by sampling from 1 or 0

rbi <- sample(0:1,100,replace = TRUE)

#now we will create an inequality. we'll keep it simple with a >0

rbi.logic <- rbi>0

#and move it to its own object

my_logical <- rbi.logic

#last element in the list will be a letters vector.

#we'll use the sample function to randomly select the letters in a random order and put that into an object

my_letters<- sample(letters[1:26],26,replace = FALSE)

# THE ALL TOGETHER!
my_big_bad_list <- list(my_matrix=my_matrix,my_logical=my_logical,my_letters=my_letters)

#check it out now, funk, soul, brother
print(my_big_bad_list)
## $my_matrix
##          [,1]     [,2]     [,3]     [,4]
## [1,] 56.07112 13.11969 75.17794 32.66987
## [2,] 20.40206 75.45032 81.50903 66.18436
## [3,] 52.32097 31.25523 14.84807 33.59501
## [4,] 51.12763 87.07333 44.63276 14.62767
## 
## $my_logical
##   [1] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
##  [13]  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE
##  [25]  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE
##  [37] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE
##  [49]  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [61] FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
##  [73]  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
##  [85]  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE
##  [97]  TRUE FALSE FALSE FALSE
## 
## $my_letters
##  [1] "s" "h" "d" "a" "m" "q" "i" "f" "u" "g" "c" "j" "l" "o" "v" "x" "r" "p" "t"
## [20] "e" "b" "k" "y" "w" "n" "z"
#we now need to grab specific elements and put them into a new list

new_list <- list(mini_mat=my_big_bad_list$my_matrix[2,2],mini_log=my_big_bad_list$my_logical[2],mini_let=my_big_bad_list$my_letters[2])

#lets store each of the element types as their own thing

t1 <- typeof(new_list$mini_mat)
t2 <- typeof(new_list$mini_log)
t3 <- typeof(new_list$mini_let)

#and then voltron them together
ta <- c(t1,t2,t3)

#check the type
typeof(ta)
## [1] "character"
#its quite the character......hahahahhahahhahahahah

Problem 3

#alright, we're in the Dataframe wild west now pawtnah! 

#lets make a new dataframe but first we'll make the stuffin

#uniform values

ru <- runif(26,min = 0,max = 10)

#get random letters

rl <- sample(LETTERS[1:26],size = 26,replace = FALSE)

#and smoosh it into a dataframe. 

df <- data.frame(my_unis=ru,my_letters=rl)

#check it yo
head(df)
##    my_unis my_letters
## 1 7.872141          O
## 2 8.008226          I
## 3 2.317294          X
## 4 3.571743          Y
## 5 5.821101          L
## 6 9.075456          W
#we'll make a copy so we don't fork up the original
df2 <- df

#and in one line BAM! we infect it with NAs

df2[c(sample(1:26,size=4,replace=FALSE)),1] <- NA

#we'll identify the NAs and might as well grab them why not

df2[c(which(!complete.cases(df2))),]
##    my_unis my_letters
## 4       NA          Y
## 13      NA          K
## 17      NA          E
## 24      NA          D
#and we can reorder them! using the order function. The important thing is that order give you the index 

df3<- df2[order(df2$my_letters,decreasing = FALSE),]

#and lastly lets get the mean of our numerical column

mean(df3$my_unis)
## [1] NA
#oh sneaky sneaky there are NAs in there so we have to remove them first we'll just take the complete cases and put them into a new dataframe

df4 <- df3[c(which(complete.cases(df3))),]

#lol we got this. Alright, once again with the mean

myunis.mean <- mean(df4$my_unis)

print(myunis.mean)
## [1] 5.740233