# Question 1 : Assign to the variable n_dims a single random integer between 3 and 10.
# make this reproducible by using the set.seed() however, if you want new numbers you can comment this out
# set.seed(25)
# randomly assign a single random integer between 3 and 10
n_dims <- sample(3:10,
size = 1)
print(n_dims)
## [1] 5
# part a: Create a vector of consecutive integers from 1 to n_dims^2.
# we need to create a vector of numbers 1: n_dims^2
numbers <- 1:(n_dims^2)
print(numbers)
## [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
#part b: Use the sample function to randomly reshuffle these values.
numbers <- sample(numbers)
print(numbers)
## [1] 24 4 18 14 20 22 8 25 1 21 10 6 2 15 9 11 3 19 13 23 17 12 5 16 7
# part c: create a square matrix with these elements.
# this needs to be square so we need to use 9 X 9 matrix because
square_matrix <- matrix(numbers,
nrow = n_dims,
ncol = n_dims)
# part d: print out the matrix.
print(square_matrix)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 24 22 10 11 17
## [2,] 4 8 6 3 12
## [3,] 18 25 2 19 5
## [4,] 14 1 15 13 16
## [5,] 20 21 9 23 7
# part e: find a function in r to transpose the matrix.
# I found the t() function to transpose my 9 X 9 matrix
transposed <- t(square_matrix)
# part f: print it out again and note how it has changed.
print(transposed) # it flipped flopped the columns and the rows in the matrix. For example, the value in [1,2] of 67 for the square_matrix matrix moved to [2,1] in the transposed matrix
## [,1] [,2] [,3] [,4] [,5]
## [1,] 24 4 18 14 20
## [2,] 22 8 25 1 21
## [3,] 10 6 2 15 9
## [4,] 11 3 19 13 23
## [5,] 17 12 5 16 7
# part g: calculate the sum and the mean of the elements in the first row and then the last row.
# For the first row of the transposed matrix
sum_first_row <- sum(transposed[1, ])
print(sum_first_row)
## [1] 80
mean_first_row <- mean(transposed[1, ])
print(mean_first_row)
## [1] 16
# For the last row of the transposed matrix
sum_last_row <- sum(transposed[n_dims, ])
print(sum_last_row)
## [1] 57
mean_last_row <- mean(transposed[n_dims, ])
print(mean_last_row)
## [1] 11.4
# part h: read about the eigen() function and use it on your matrix
?eigen()
eigen_matrix <- eigen(transposed)
print(eigen_matrix)
## eigen() decomposition
## $values
## [1] 64.486783+0.000000i -20.191957+0.000000i 5.935218+4.970024i
## [4] 5.935218-4.970024i -2.165262+0.000000i
##
## $vectors
## [,1] [,2] [,3] [,4]
## [1,] 0.5395556+0i -0.1940362+0i -0.1378091+0.37982811i -0.1378091-0.37982811i
## [2,] 0.5043716+0i -0.5476069+0i 0.7286739+0.00000000i 0.7286739+0.00000000i
## [3,] 0.2999474+0i 0.3182782+0i -0.1382904-0.11462015i -0.1382904+0.11462015i
## [4,] 0.4400241+0i -0.4716701+0i -0.3503231-0.28381310i -0.3503231+0.28381310i
## [5,] 0.4133998+0i 0.5819819+0i 0.2540396-0.07549404i 0.2540396+0.07549404i
## [,5]
## [1,] 0.06904687+0i
## [2,] 0.04042404+0i
## [3,] -0.64901971+0i
## [4,] -0.29896423+0i
## [5,] 0.69497643+0i
# part i: look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?
eigen_values <- eigen_matrix$values
print(eigen_values)
## [1] 64.486783+0.000000i -20.191957+0.000000i 5.935218+4.970024i
## [4] 5.935218-4.970024i -2.165262+0.000000i
eigen_vectors <- eigen_matrix$vectors
print(eigen_vectors)
## [,1] [,2] [,3] [,4]
## [1,] 0.5395556+0i -0.1940362+0i -0.1378091+0.37982811i -0.1378091-0.37982811i
## [2,] 0.5043716+0i -0.5476069+0i 0.7286739+0.00000000i 0.7286739+0.00000000i
## [3,] 0.2999474+0i 0.3182782+0i -0.1382904-0.11462015i -0.1382904+0.11462015i
## [4,] 0.4400241+0i -0.4716701+0i -0.3503231-0.28381310i -0.3503231+0.28381310i
## [5,] 0.4133998+0i 0.5819819+0i 0.2540396-0.07549404i 0.2540396+0.07549404i
## [,5]
## [1,] 0.06904687+0i
## [2,] 0.04042404+0i
## [3,] -0.64901971+0i
## [4,] -0.29896423+0i
## [5,] 0.69497643+0i
# I have read the R help file on the output from the eigen() function. The help file states that the values and vectors may contain complex numbers.
# part j: dig in with the typeof() function to figure out their type.
# Check the typeof() for the values of of the eigen matrix
typeof(eigen_matrix$values)
## [1] "complex"
# this returned that the values are complex
# What is the typeof() of the vectors in the eigen table?
typeof(eigen_matrix$vectors)
## [1] "complex"
# This also returned a complex type
# part k: if have set your code up properly, you should be able to re-run it and create a matrix of different size because n_dims will change.
# I re ran the code and it changed the n_dims. However, if I wanted to keep the same table, I would run the set.seed() function to prevent it from changing each time and it is reproducible.
# Question 2 ----------------------------------
# part a: Create a list with the following named elements: my_matrix, which is a 4 x 4 matrix filled with random uniform values
my_matrix <- matrix(runif(16), nrow = 4, ncol = 4)
print(my_matrix)
## [,1] [,2] [,3] [,4]
## [1,] 0.1525024 0.3117838 0.3056491 0.3483066
## [2,] 0.3082364 0.5521831 0.1632382 0.3170717
## [3,] 0.7307887 0.1294181 0.5985261 0.1186007
## [4,] 0.1660367 0.9723065 0.8642924 0.5411977
# part b: my_logical which is a 100-element vector of TRUE or FALSE values. Do this efficiently by setting up a vector of random values and then applying an inequality to it.
my_logical <- runif(100) > 0.5
print(my_logical)
## [1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
## [13] FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE
## [25] FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE
## [37] FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
## [49] TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
## [61] FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE
## [73] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
## [85] FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
## [97] FALSE TRUE FALSE TRUE
# part c: my_letters, which is a 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters)
print(my_letters)
## [1] "q" "n" "p" "x" "h" "e" "s" "v" "g" "u" "m" "c" "l" "b" "j" "f" "w" "r" "d"
## [20] "i" "z" "t" "y" "o" "k" "a"
# Now I need to create a list of all of these.
# Create the list with all three named elements
my_list <- list(
my_matrix = my_matrix,
my_logical = my_logical,
my_letters = my_letters
)
str(my_list)
## List of 3
## $ my_matrix : num [1:4, 1:4] 0.153 0.308 0.731 0.166 0.312 ...
## $ my_logical: logi [1:100] FALSE TRUE FALSE TRUE FALSE FALSE ...
## $ my_letters: chr [1:26] "q" "n" "p" "x" ...
print(my_list)
## $my_matrix
## [,1] [,2] [,3] [,4]
## [1,] 0.1525024 0.3117838 0.3056491 0.3483066
## [2,] 0.3082364 0.5521831 0.1632382 0.3170717
## [3,] 0.7307887 0.1294181 0.5985261 0.1186007
## [4,] 0.1660367 0.9723065 0.8642924 0.5411977
##
## $my_logical
## [1] FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
## [13] FALSE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE TRUE
## [25] FALSE TRUE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE
## [37] FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE
## [49] TRUE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
## [61] FALSE TRUE TRUE TRUE FALSE FALSE TRUE FALSE FALSE TRUE TRUE FALSE
## [73] FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
## [85] FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE
## [97] FALSE TRUE FALSE TRUE
##
## $my_letters
## [1] "q" "n" "p" "x" "h" "e" "s" "v" "g" "u" "m" "c" "l" "b" "j" "f" "w" "r" "d"
## [20] "i" "z" "t" "y" "o" "k" "a"
# part d: 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.
my_list2 <- list(
matrix_element = my_list$my_matrix[2,2],
logical_element = my_list$my_logical[2],
letter_element = my_list$my_letters[2]
)
str(my_list2)
## List of 3
## $ matrix_element : num 0.552
## $ logical_element: logi TRUE
## $ letter_element : chr "n"
print(my_list2)
## $matrix_element
## [1] 0.5521831
##
## $logical_element
## [1] TRUE
##
## $letter_element
## [1] "n"
# part e: use the typeof() function to confirm the underlying data types of each component in this list
typeof(my_list2$matrix_element) # double
## [1] "double"
typeof(my_list2$logical_element) # logical
## [1] "logical"
typeof(my_list2$letter_element) # character
## [1] "character"
# part f: combine the underlying elements from the new list into a single atomic vector with the c() function.
vector <- c(my_list2$matrix_element,
my_list2$logical_element,
my_list2$letter_element)
# part g: what is the data type of this vector?
typeof(vector)
## [1] "character"
# it is a character
# Question 3 ------------------------------------
# part a: Create a data frame with the two variables (= columns) and 26 cases (= rows) below: call the first variable my_unis and fill it with 26 random uniform values from 0 to 10
# First we need to create my_unis
# part b: call the second variable my_letters and fill it with 26 capital letters in random order.
# now we need to make my_letters
my_df <- data.frame(
my_unis = runif(26, min = 0, max = 10),
my_letters = sample(LETTERS)
)
print(my_df)
## my_unis my_letters
## 1 2.81356952 G
## 2 3.62581074 W
## 3 3.57326747 Z
## 4 5.36993842 O
## 5 4.72651470 N
## 6 4.36914095 I
## 7 7.96255343 Y
## 8 2.94145953 U
## 9 4.86720150 S
## 10 3.92564595 T
## 11 8.54411017 M
## 12 8.21640915 R
## 13 4.93741335 F
## 14 5.65551987 J
## 15 3.12581503 X
## 16 2.14525906 V
## 17 3.71728848 A
## 18 2.08115117 Q
## 19 6.17386189 K
## 20 1.39581061 E
## 21 8.21316858 P
## 22 5.79049621 L
## 23 4.22821079 C
## 24 0.18255045 D
## 25 0.07539585 H
## 26 4.10999261 B
# part c: 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$my_unis[sample(1:26, 4)] <- NA
# part d: for the first variable, write a single line of R code to identify which rows have the missing values.
which(is.na(my_df$my_unis)) # columns will change if you rerun the runif() function above
## [1] 7 19 22 24
# part e: re-order the entire data frame to arrange the second variable in alphabetical order
my_df <- my_df[order(my_df$my_letters), ]
print(my_df)
## my_unis my_letters
## 17 3.71728848 A
## 26 4.10999261 B
## 23 4.22821079 C
## 24 NA D
## 20 1.39581061 E
## 13 4.93741335 F
## 1 2.81356952 G
## 25 0.07539585 H
## 6 4.36914095 I
## 14 5.65551987 J
## 19 NA K
## 22 NA L
## 11 8.54411017 M
## 5 4.72651470 N
## 4 5.36993842 O
## 21 8.21316858 P
## 18 2.08115117 Q
## 12 8.21640915 R
## 9 4.86720150 S
## 10 3.92564595 T
## 8 2.94145953 U
## 16 2.14525906 V
## 2 3.62581074 W
## 15 3.12581503 X
## 7 NA Y
## 3 3.57326747 Z
# part f: calculate the column mean for the first variable.
mean_variable1 <- mean(my_df$my_unis, na.rm = TRUE)
print(mean_variable1)# 5.15755
## [1] 4.211732
End of Homework 5