Click here to return to the homepage

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 to the variable n_dims a single random integer between 3 and 10.
print(n_dims)
## [1] 3
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) #Use the sample function to randomly reshuffle these values
print(shuffle)
## [1] 2 8 5 1 6 9 4 3 7
my_mat <- matrix(data = shuffle, ncol = sqrt(length(shuffle)), byrow = TRUE) #create a square matrix with these elements
print(my_mat) #print out matrix
##      [,1] [,2] [,3]
## [1,]    2    8    5
## [2,]    1    6    9
## [3,]    4    3    7
transposed_mat <- t(my_mat) #find a function in r to transpose the matrix
print(transposed_mat) #print it out again and note how it has changed
##      [,1] [,2] [,3]
## [1,]    2    1    4
## [2,]    8    6    3
## [3,]    5    9    7
#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,]) #first row sum
last_sum <- sum(transposed_mat[nrow(transposed_mat),])#last row sum
print(first_sum)
## [1] 7
print(last_sum)
## [1] 21
first_mean <- mean(transposed_mat[1,]) #first row mean
last_mean <- mean(transposed_mat[nrow(transposed_mat),])#last row mean
print(first_mean)
## [1] 2.333333
print(last_mean)
## [1] 7
#read about the eigen() function and use it on your matrix
my_eigen <- eigen(my_mat)
my_eigen
## eigen() decomposition
## $values
## [1] 14.837006+0.000000i  0.081497+3.251924i  0.081497-3.251924i
## 
## $vectors
##               [,1]                  [,2]                  [,3]
## [1,] -0.5872357+0i  0.7395563+0.0000000i  0.7395563+0.0000000i
## [2,] -0.6092120+0i -0.0158888+0.5164931i -0.0158888-0.5164931i
## [3,] -0.5329305+0i -0.2583460-0.3453928i -0.2583460+0.3453928i
#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.6272155 0.15663178 0.78050213 0.9430455
## [2,] 0.3871717 0.52623584 0.57331025 0.1180106
## [3,] 0.1542068 0.75402953 0.03701128 0.5704448
## [4,] 0.3285452 0.08867132 0.14945828 0.6567017
my_logical <-(0.5 > runif(100)) #a 100-element vector of TRUE or FALSE values
print(my_logical)
##   [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE
##  [13] FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
##  [25] FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [37]  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE
##  [49]  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE
##  [61]  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE
##  [73]  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [85] FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
##  [97] FALSE  TRUE FALSE FALSE
my_letters <- sample(letters, size =length(letters)) #a 26-element vector of all the lower-case letters in random order
print(my_letters)
##  [1] "z" "q" "y" "k" "p" "h" "o" "j" "s" "v" "t" "b" "r" "i" "x" "w" "l" "u" "f"
## [20] "c" "e" "n" "a" "m" "d" "g"
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.5262358
## 
## [[2]]
## [1] TRUE
## 
## [[3]]
## [1] "q"
#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.526235835626721" "TRUE"              "q"
#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] 7.69499339 0.12723923 9.49360432 5.91882866 8.69455008 6.09453910
##  [7] 8.42178586 3.19295984 2.48176402 7.68871307 5.00965477 4.26164361
## [13] 4.88407691 3.71993994 8.60523291 9.59251878 7.17374178 5.84837745
## [19] 0.26191221 5.28827168 7.85062808 9.71214028 9.50451239 0.05560892
## [25] 7.89293468 9.80960126
my_letters <- sample(LETTERS) #call the second variable my_letters and fill it with 26 capital letters in random order
print(my_letters)
##  [1] "R" "N" "Q" "W" "K" "S" "U" "I" "C" "Y" "J" "A" "T" "O" "X" "B" "E" "F" "G"
## [20] "V" "M" "P" "Z" "L" "H" "D"
my_df <- data.frame(my_unis, my_letters, stringsAsFactors = FALSE) # make df with the above variabls
print(my_df)
##       my_unis my_letters
## 1  7.69499339          R
## 2  0.12723923          N
## 3  9.49360432          Q
## 4  5.91882866          W
## 5  8.69455008          K
## 6  6.09453910          S
## 7  8.42178586          U
## 8  3.19295984          I
## 9  2.48176402          C
## 10 7.68871307          Y
## 11 5.00965477          J
## 12 4.26164361          A
## 13 4.88407691          T
## 14 3.71993994          O
## 15 8.60523291          X
## 16 9.59251878          B
## 17 7.17374178          E
## 18 5.84837745          F
## 19 0.26191221          G
## 20 5.28827168          V
## 21 7.85062808          M
## 22 9.71214028          P
## 23 9.50451239          Z
## 24 0.05560892          L
## 25 7.89293468          H
## 26 9.80960126          D
#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  7.69499339          R
## 2  0.12723923          N
## 3  9.49360432          Q
## 4  5.91882866          W
## 5  8.69455008          K
## 6  6.09453910          S
## 7  8.42178586          U
## 8  3.19295984          I
## 9  2.48176402          C
## 10 7.68871307          Y
## 11 5.00965477          J
## 12 4.26164361          A
## 13         NA          T
## 14         NA          O
## 15         NA          X
## 16 9.59251878          B
## 17 7.17374178          E
## 18 5.84837745          F
## 19 0.26191221          G
## 20         NA          V
## 21 7.85062808          M
## 22 9.71214028          P
## 23 9.50451239          Z
## 24 0.05560892          L
## 25 7.89293468          H
## 26 9.80960126          D
na_rows <- which(is.na(my_df)) #identify which rows have the missing values
print(na_rows)
## [1] 13 14 15 20
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
## 12 4.26164361          A
## 16 9.59251878          B
## 9  2.48176402          C
## 26 9.80960126          D
## 17 7.17374178          E
## 18 5.84837745          F
## 19 0.26191221          G
## 25 7.89293468          H
## 8  3.19295984          I
## 11 5.00965477          J
## 5  8.69455008          K
## 24 0.05560892          L
## 21 7.85062808          M
## 2  0.12723923          N
## 14         NA          O
## 22 9.71214028          P
## 3  9.49360432          Q
## 1  7.69499339          R
## 6  6.09453910          S
## 13         NA          T
## 7  8.42178586          U
## 20         NA          V
## 4  5.91882866          W
## 15         NA          X
## 10 7.68871307          Y
## 23 9.50451239          Z
#Calculate the column mean for the first variable
df_mean <- mean(my_df$my_unis, na.rm=TRUE)
print(df_mean)
## [1] 6.217375