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]
## [1,] 6 3 4
## [2,] 5 2 9
## [3,] 1 7 8
Calculate the sum and the mean of the elements in the first row and then the last row.
mean(matrix1[1,])
## [1] 4
sum(matrix1[1,])
## [1] 12
mean(matrix1[2,])
## [1] 4
sum(matrix1[2,])
## [1] 12
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] 15.360737 4.309834 -3.670571
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.3494775 -0.8727896 0.3601329
## [2,] -0.4951951 0.2065875 -0.7943705
## [3,] -0.7953913 0.4422216 0.4891623
Dig in with the typeof() function to figure out their type.
typeof(matrix_eigen$values)
## [1] "double"
typeof(matrix_eigen$vectors)
## [1] "double"
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.8098659 0.8419255 0.50241902 0.9913003
## [2,] 0.5716011 0.7716107 0.93835581 0.6015658
## [3,] 0.8000746 0.3568776 0.10374204 0.8898169
## [4,] 0.4260249 0.3544592 0.08242483 0.2183593
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] FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE
## [25] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
## [37] FALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE
## [49] FALSE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE FALSE
## [61] FALSE TRUE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
## [73] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE TRUE TRUE
## [85] TRUE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE
## [97] FALSE FALSE FALSE 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] "b" "j" "n" "x" "g" "i" "f" "t" "p" "w" "u" "r" "e" "l" "a" "y" "q" "c" "v"
## [20] "z" "k" "o" "d" "s" "m" "h"
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.771610651398078" "FALSE" "j"
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] 3 4 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.404932