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

n_dims <- sample(seq(3:10), size=1)

Create a vector of consecutive integers from 1 to n_dims^2

vector <- seq(1:n_dims^2)

Use the sample function to randomly reshuffle these values. Create a square matrix with these elements. Print out the matrix.

matrix1 <- matrix(sample(vector), nrow=n_dims)

Find a function in r to transpose the matrix. Print it out again and note how it has changed.

print(t(matrix1))
##      [,1] [,2] [,3] [,4] [,5]
## [1,]   20    8    9    5    6
## [2,]    3   22   18   21   24
## [3,]   10   25   16   19   11
## [4,]    2    1   17   12   13
## [5,]    4   15   14   23    7

Calculate the sum and the mean of the elements in the first row and then the last row.

mean(matrix1[1,])
## [1] 7.8
sum(matrix1[1,])
## [1] 39
mean(matrix1[2,])
## [1] 14.2
sum(matrix1[2,])
## [1] 71

Read about the eigen() function and use it on your matrix. Look carefully at the elements of $values and $vectors in the output. What kind of numbers are these?

matrix_eigen <- eigen(matrix1)
print(matrix_eigen)
## eigen() decomposition
## $values
## [1]  66.705232+0.000000i  16.415327+0.000000i -12.833367+0.000000i
## [4]   3.356404+5.624819i   3.356404-5.624819i
## 
## $vectors
##               [,1]           [,2]           [,3]                  [,4]
## [1,] -0.1981743+0i -0.88775111+0i  0.08665852+0i  0.2109232+0.1183090i
## [2,] -0.4717885+0i -0.02813429+0i  0.43718380+0i  0.4054491+0.2626509i
## [3,] -0.5045084+0i  0.19426329+0i -0.21208234+0i -0.5840329+0.0000000i
## [4,] -0.5521803+0i  0.39426238+0i  0.45918797+0i -0.3380098-0.3580354i
## [5,] -0.4227419+0i  0.13388568+0i -0.73859875+0i  0.2810036-0.2136411i
##                       [,5]
## [1,]  0.2109232-0.1183090i
## [2,]  0.4054491-0.2626509i
## [3,] -0.5840329+0.0000000i
## [4,] -0.3380098+0.3580354i
## [5,]  0.2810036+0.2136411i

Dig in with the typeof() function to figure out their type.

typeof(matrix_eigen$values)
## [1] "complex"
typeof(matrix_eigen$vectors)
## [1] "complex"

Question 2: 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(data=runif(16), nrow=4)
print(my_matrix)
##           [,1]      [,2]      [,3]      [,4]
## [1,] 0.7498429 0.9204264 0.9773293 0.4019435
## [2,] 0.4073442 0.7549809 0.4747470 0.1741122
## [3,] 0.4699467 0.7782904 0.6291126 0.8022201
## [4,] 0.2694348 0.1356746 0.9551195 0.5286537

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) > .8)
print(my_logical)
##   [1]  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE
##  [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE
##  [25] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE
##  [49] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [61] FALSE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE
##  [73] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
##  [97] FALSE FALSE  TRUE FALSE

my_letters, which is a 26-element vector of all the lower-case letters in random order.

my_letters <- sample(letters[1:26])
print(my_letters)
##  [1] "y" "i" "c" "g" "a" "h" "n" "s" "w" "v" "m" "t" "r" "b" "z" "p" "d" "f" "k"
## [20] "j" "o" "l" "e" "q" "x" "u"

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.

list1 <- list(my_matrix, my_logical, my_letters)

new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])

Use the typeof() function to confirm the underlying data types of each component in this list

typeof(new_list[[1]])
## [1] "double"
typeof(new_list[[2]])
## [1] "logical"
typeof(new_list[[3]])
## [1] "character"

Combine the underlying elements from the new list into a single atomic vector with the c() function. What is the data type of this vector?

combined_elements_vector <- c(new_list[[1]], new_list[[2]], new_list[[3]])
print(combined_elements_vector)
## [1] "0.75498088565655" "FALSE"            "i"
typeof(combined_elements_vector)
## [1] "character"

Question 3: 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. Call the second variable my_letters and fill it with 26 capital letters in random order.

dataframe <- data.frame(my_unis=runif(26, min=0, max=10), my_letters=sample(LETTERS[1:26]))

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.

dataframe[sample(dataframe$my_unis, size=4, replace=TRUE),1]=NA

For the first variable, write a single line of R code to identify which rows have the missing values.

print(which(is.na(dataframe$my_unis)))
## [1] 2 4 6 9

Re-order the entire data frame to arrange the second variable in alphabetical order

dataframe[order(dataframe$my_letters),2]
##  [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"

Calculate the column mean for the first variable.

mean(dataframe$my_unis, na.rm=TRUE)
## [1] 5.077148