Question 1 Assign to the variable n_dims a single random integer between 3 and 10

n_dims <- floor(runif(1, min =3, max= 10)) #Assign a single random integer between 3 and 10 to variable n_dims 
print(n_dims)
## [1] 7
my_vec <- seq(1:(n_dims^2)) #Create a vector of consecutive integers from 1 to n_dims^2


shuffle <- sample(my_vec, size = length(my_vec), replace = FALSE) #sample function randonly reshuffles the values 
print(shuffle)
##  [1] 41 11 13 42 22 16 48 33 30 27 17 20 25 35 24 26 38 45 46  8 36 18 31 40 32
## [26] 19  4 34 15 21 14 23 29  9 47 39  6 28 49 43  2 10 44  1 37  7  3  5 12
my_mat <- matrix(data = shuffle, ncol = sqrt(length(shuffle)), byrow = TRUE) #create a square matrix with these elements
print(my_mat) 
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   41   11   13   42   22   16   48
## [2,]   33   30   27   17   20   25   35
## [3,]   24   26   38   45   46    8   36
## [4,]   18   31   40   32   19    4   34
## [5,]   15   21   14   23   29    9   47
## [6,]   39    6   28   49   43    2   10
## [7,]   44    1   37    7    3    5   12
transposed_mat <- t(my_mat) #find a function in r to transpose the matrix
print(transposed_mat) 
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]   41   33   24   18   15   39   44
## [2,]   11   30   26   31   21    6    1
## [3,]   13   27   38   40   14   28   37
## [4,]   42   17   45   32   23   49    7
## [5,]   22   20   46   19   29   43    3
## [6,]   16   25    8    4    9    2    5
## [7,]   48   35   36   34   47   10   12
#the X and Y coordinates have flipped - ie. the value in [3,4] is now in [4,3]


#### calculate the sum and the mean of the elements in the first row and the last row ####
first_sum <- sum(transposed_mat[1,]) #sum of first row 
last_sum <- sum(transposed_mat[nrow(transposed_mat),])#sum of last row
print(first_sum)
## [1] 214
print(last_sum)
## [1] 222
first_mean <- mean(transposed_mat[1,]) #mean first row 
last_mean <- mean(transposed_mat[nrow(transposed_mat),])#mean last row
print(first_mean)
## [1] 30.57143
#read about the eigen() function and use it on your matrix
my_eigen <- eigen(my_mat)
my_eigen
## eigen() decomposition
## $values
## [1] 173.03595+ 0.00000i  -9.66452+24.54529i  -9.66452-24.54529i
## [4]  17.47618+12.34038i  17.47618-12.34038i -15.70087+ 0.00000i
## [7]  11.04159+ 0.00000i
## 
## $vectors
##               [,1]                  [,2]                  [,3]
## [1,] -0.3940826+0i -0.1113453+0.0329287i -0.1113453-0.0329287i
## [2,] -0.3995502+0i -0.3166683-0.1981376i -0.3166683+0.1981376i
## [3,] -0.4731827+0i  0.2902116+0.0935480i  0.2902116-0.0935480i
## [4,] -0.3866598+0i -0.0644870+0.1495160i -0.0644870-0.1495160i
## [5,] -0.3138600+0i -0.1642095+0.2571418i -0.1642095-0.2571418i
## [6,] -0.3858432+0i  0.7290624+0.0000000i  0.7290624+0.0000000i
## [7,] -0.2535109+0i -0.0166733-0.3203067i -0.0166733+0.3203067i
##                       [,4]                  [,5]           [,6]           [,7]
## [1,]  0.4856815+0.0000000i  0.4856815+0.0000000i  0.36989683+0i -0.35372765+0i
## [2,]  0.0975061-0.2267876i  0.0975061+0.2267876i  0.37950190+0i -0.71203111+0i
## [3,] -0.4660425+0.0580456i -0.4660425-0.0580456i -0.17129369+0i  0.43453810+0i
## [4,] -0.4037741+0.2484372i -0.4037741-0.2484372i -0.09107451+0i -0.16705360+0i
## [5,]  0.1347617-0.3302128i  0.1347617+0.3302128i  0.19677732+0i  0.27734326+0i
## [6,] -0.2404892+0.1146952i -0.2404892-0.1146952i -0.76737575+0i  0.05598376+0i
## [7,]  0.2375518+0.0568492i  0.2375518-0.0568492i -0.23223197+0i  0.26665467+0i
#Look carefully at the elements of $values and $vectors. What kind of numbers are these?
typeof(my_eigen$values) 
## [1] "complex"
typeof(my_eigen$vectors) # These are both a complex
## [1] "complex"

Question 2

my_matrix <- matrix(data = runif(16), ncol = 4) #a 4 x 4 matrix filled with random uniform values
print(my_matrix)
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.5667964 0.6583518 0.5063428 0.4427164
## [2,] 0.8231158 0.3314637 0.3126693 0.2786046
## [3,] 0.9579930 0.3097846 0.7624788 0.1602942
## [4,] 0.5015922 0.8043168 0.8228633 0.5848479
my_logical <-(0.5 > runif(100)) #a 100-element vector of TRUE or FALSE values
print(my_logical)
##   [1] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE
##  [13] FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
##  [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
##  [37] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
##  [49] FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
##  [61] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [73] FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
##  [85]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
##  [97]  TRUE  TRUE  TRUE  TRUE
my_letters <- sample(letters, size =length(letters)) #a 26-element vector of all the lower-case letters in random order
print(my_letters)
##  [1] "g" "i" "n" "s" "h" "c" "e" "m" "r" "j" "u" "l" "w" "x" "k" "d" "q" "a" "f"
## [20] "b" "v" "t" "y" "o" "p" "z"
list1 <- list(my_matrix, my_logical, my_letters) #Create list of the three elements


#### create a new list, which has the element[2,2] from the matrix, the second element of the logical vector, and the second element of the letters vector ####
new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
print(new_list)
## [[1]]
## [1] 0.3314637
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] "i"
#use the typeof() function to confirm the underlying data types of each component in this list
typeof(new_list[[1]]) #double
## [1] "double"
typeof(new_list[[2]]) #logical
## [1] "logical"
typeof(new_list[[3]])#character
## [1] "character"
#### combine the underlying elements from the new list into a single atomic vector with the c() function #####
new_vector <- c(my_matrix[2,2], my_logical[2], my_letters[2])
print(new_vector)
## [1] "0.331463702721521" "TRUE"              "i"
#what is the data type of this vector
typeof(new_vector) #new_vector is type character
## [1] "character"

Question 3

#Create a data frame with two variables (= columns) and 26 cases (= rows)

my_unis <- runif(26, min=0, max=10) #call the first variable my_unis and fill it with 26 random uniform values from 0 to 10
print(my_unis)
##  [1] 8.3984294 3.1869284 6.9307264 0.7894312 3.1509302 0.2863561 1.6254230
##  [8] 6.5490697 2.5147652 0.8310206 9.1487063 9.9173039 9.4890166 4.5229130
## [15] 9.1390049 4.1224908 0.4705535 5.2124093 4.8569107 4.7314735 7.1985481
## [22] 6.6895060 1.4630518 1.7759386 9.5983015 8.8984099
my_letters <- sample(LETTERS) #call the second variable my_letters and fill it with 26 capital letters in random order
print(my_letters)
##  [1] "Y" "U" "Q" "D" "H" "X" "C" "S" "E" "R" "P" "T" "W" "N" "K" "V" "Z" "G" "O"
## [20] "I" "M" "L" "J" "F" "B" "A"
my_df <- data.frame(my_unis, my_letters, stringsAsFactors = FALSE) # make df with the above variabls
print(my_df)
##      my_unis my_letters
## 1  8.3984294          Y
## 2  3.1869284          U
## 3  6.9307264          Q
## 4  0.7894312          D
## 5  3.1509302          H
## 6  0.2863561          X
## 7  1.6254230          C
## 8  6.5490697          S
## 9  2.5147652          E
## 10 0.8310206          R
## 11 9.1487063          P
## 12 9.9173039          T
## 13 9.4890166          W
## 14 4.5229130          N
## 15 9.1390049          K
## 16 4.1224908          V
## 17 0.4705535          Z
## 18 5.2124093          G
## 19 4.8569107          O
## 20 4.7314735          I
## 21 7.1985481          M
## 22 6.6895060          L
## 23 1.4630518          J
## 24 1.7759386          F
## 25 9.5983015          B
## 26 8.8984099          A
#### for the first variable, use a single line of code in R to select 4 random rows and replace the numerical values in those rows with NA ####
my_df[sample(length(my_df$my_unis), replace = FALSE, size = 4),1] = NA
print(my_df)
##      my_unis my_letters
## 1  8.3984294          Y
## 2  3.1869284          U
## 3  6.9307264          Q
## 4  0.7894312          D
## 5  3.1509302          H
## 6  0.2863561          X
## 7         NA          C
## 8  6.5490697          S
## 9  2.5147652          E
## 10        NA          R
## 11 9.1487063          P
## 12        NA          T
## 13 9.4890166          W
## 14 4.5229130          N
## 15        NA          K
## 16 4.1224908          V
## 17 0.4705535          Z
## 18 5.2124093          G
## 19 4.8569107          O
## 20 4.7314735          I
## 21 7.1985481          M
## 22 6.6895060          L
## 23 1.4630518          J
## 24 1.7759386          F
## 25 9.5983015          B
## 26 8.8984099          A
na_rows <- which(is.na(my_df)) #identify which rows have the missing values
print(na_rows)
## [1]  7 10 12 15
my_df_sort <- my_df[order(my_df$my_letters),] #for the second variable, sort it in alphabetical order
print(my_df_sort)
##      my_unis my_letters
## 26 8.8984099          A
## 25 9.5983015          B
## 7         NA          C
## 4  0.7894312          D
## 9  2.5147652          E
## 24 1.7759386          F
## 18 5.2124093          G
## 5  3.1509302          H
## 20 4.7314735          I
## 23 1.4630518          J
## 15        NA          K
## 22 6.6895060          L
## 21 7.1985481          M
## 14 4.5229130          N
## 19 4.8569107          O
## 11 9.1487063          P
## 3  6.9307264          Q
## 10        NA          R
## 8  6.5490697          S
## 12        NA          T
## 2  3.1869284          U
## 16 4.1224908          V
## 13 9.4890166          W
## 6  0.2863561          X
## 1  8.3984294          Y
## 17 0.4705535          Z
####Calculate the column mean for the first variable ####
df_mean <- mean(my_df$my_unis, na.rm=TRUE)
print(df_mean)
## [1] 4.999312