Question 1
Assign to the variable n_dims a single random integer between 3 and 10.
<- floor(runif(1,min=3, max=10))
n_dims print(n_dims)
## [1] 3
#create vector from 1 to n_dims^2
<- c(seq(1:(n_dims^2)))
vec1 print(vec1)
## [1] 1 2 3 4 5 6 7 8 9
#randomly reshuffle values
<- sample(vec1, (n_dims^2), replace = FALSE, prob = NULL)
vec1a print(vec1a)
## [1] 3 4 8 1 6 5 9 7 2
#create a square matrix
<- matrix(data=vec1a, nrow=n_dims)
matrix_1 print(matrix_1)
## [,1] [,2] [,3]
## [1,] 3 1 9
## [2,] 4 6 7
## [3,] 8 5 2
#transpose the matrix
<- t(matrix_1)
matrix_t print(matrix_t) #note that the columns and rows have flipped (the first row is now the first column)
## [,1] [,2] [,3]
## [1,] 3 4 8
## [2,] 1 6 5
## [3,] 9 7 2
#calculate sum and mean of first row and last row
sum(matrix_t[1,]) #first row
## [1] 15
sum(matrix_t[n_dims,]) #last row
## [1] 18
mean(matrix_t[1,]) #first row
## [1] 5
mean(matrix_t[n_dims,]) #last row
## [1] 6
#use eigen() on matrix
<- eigen(matrix_t, isSymmetric(matrix_t), only.values = FALSE, EISPACK = FALSE)
matrix_e print(matrix_e)
## eigen() decomposition
## $values
## [1] 14.818077 -6.606684 2.788607
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.6004101 0.5469276 -0.5945566
## [2,] -0.4448828 0.2708032 0.7253420
## [3,] -0.6645201 -0.7921716 -0.3469603
typeof(matrix_e$values) #complex values
## [1] "double"
typeof(matrix_e$vectors) #complex values
## [1] "double"
Question 2
Create a list with the following named elements:
# 4 x 4 matrix filled with random uniform values
<- runif(4)
my_vec <- matrix(my_vec, nrow=2)
my_matrix print(my_matrix)
## [,1] [,2]
## [1,] 0.73026602 0.42142398
## [2,] 0.07997085 0.05525232
# 100-element vector of TRUE or FALSE values
<- runif(100)
my_vec2 <- ifelse(my_vec2 > 0.5, TRUE, FALSE)
my_logical print(my_logical)
## [1] TRUE TRUE FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE
## [13] FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE
## [25] FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
## [37] TRUE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
## [49] FALSE FALSE TRUE TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE FALSE
## [61] FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
## [73] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE
## [85] FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE TRUE FALSE
## [97] FALSE TRUE TRUE FALSE
#26-element vector of all the lower-case letters in random order
<- sample(letters, 26,replace = FALSE, prob=NULL)
my_letters print(my_letters)
## [1] "s" "w" "r" "e" "g" "x" "m" "j" "h" "t" "a" "y" "f" "z" "d" "b" "l" "k" "v"
## [20] "i" "p" "c" "q" "n" "u" "o"
#new list, which has the element [2,2] from the matrix, second element of the logical vector, and the second element of the letter vector
<- list(my_matrix[2,2], my_logical[2], my_letters[2])
my_list print(my_list)
## [[1]]
## [1] 0.05525232
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] "w"
#confirm underlying data types
typeof(my_list[[1]]) #double
## [1] "double"
typeof(my_list[[2]]) #logical
## [1] "logical"
typeof(my_list[[3]]) #character
## [1] "character"
#combine the underlying elements from the new list into a single atomic vector with the c() function.
<- c(my_matrix[2,2], my_logical[2], my_letters[2])
single_vector typeof(single_vector) #data type is character
## [1] "character"
Question 3
Create a data frame with two variables (= columns) and 26 cases (= rows).
#fill it with 26 random uniform values from 0 to 10
<- runif(26, min = 0, max = 10)
my_unis print(my_unis)
## [1] 2.4697013 1.9579104 7.7944596 5.8160276 6.2884131 9.0719775 5.1900018
## [8] 8.0865862 3.2987474 7.9403497 1.7011935 9.1761178 7.8069657 9.0666651
## [15] 7.3510557 0.1563902 4.4244789 4.9009779 9.9239024 1.9883285 6.5683044
## [22] 5.2872677 1.2348623 3.3399610 8.5249399 3.8380060
# fill it with 26 capital letters in random order
<- sample(LETTERS, 26, replace = FALSE, prob=NULL)
my_letters print(my_letters)
## [1] "O" "S" "Z" "M" "R" "L" "F" "D" "J" "C" "B" "X" "E" "Y" "U" "N" "I" "K" "A"
## [20] "Q" "W" "P" "H" "V" "G" "T"
<- data.frame(my_unis, my_letters)
dFrame print(dFrame)
## my_unis my_letters
## 1 2.4697013 O
## 2 1.9579104 S
## 3 7.7944596 Z
## 4 5.8160276 M
## 5 6.2884131 R
## 6 9.0719775 L
## 7 5.1900018 F
## 8 8.0865862 D
## 9 3.2987474 J
## 10 7.9403497 C
## 11 1.7011935 B
## 12 9.1761178 X
## 13 7.8069657 E
## 14 9.0666651 Y
## 15 7.3510557 U
## 16 0.1563902 N
## 17 4.4244789 I
## 18 4.9009779 K
## 19 9.9239024 A
## 20 1.9883285 Q
## 21 6.5683044 W
## 22 5.2872677 P
## 23 1.2348623 H
## 24 3.3399610 V
## 25 8.5249399 G
## 26 3.8380060 T
#use a single line of code in R to select 4 random rows and replace the numerical values in those rows with NA
sample(length(dFrame$my_unis), size = 4, replace = FALSE, prob = NULL), 1] = NA
dFrame[print(dFrame)
## my_unis my_letters
## 1 NA O
## 2 1.9579104 S
## 3 7.7944596 Z
## 4 5.8160276 M
## 5 6.2884131 R
## 6 9.0719775 L
## 7 5.1900018 F
## 8 8.0865862 D
## 9 3.2987474 J
## 10 7.9403497 C
## 11 NA B
## 12 9.1761178 X
## 13 7.8069657 E
## 14 9.0666651 Y
## 15 7.3510557 U
## 16 0.1563902 N
## 17 4.4244789 I
## 18 4.9009779 K
## 19 9.9239024 A
## 20 1.9883285 Q
## 21 6.5683044 W
## 22 5.2872677 P
## 23 1.2348623 H
## 24 NA V
## 25 8.5249399 G
## 26 NA T
#for the first variable, write a single line of R code to identify which rows have the missing values
<- which(is.na(dFrame))
dFrameNA print(dFrameNA)
## [1] 1 11 24 26
#or the second variable, sort it in alphabetical order
<- dFrame[order(dFrame$my_letters),]
dFrameOrder print(dFrameOrder)
## my_unis my_letters
## 19 9.9239024 A
## 11 NA B
## 10 7.9403497 C
## 8 8.0865862 D
## 13 7.8069657 E
## 7 5.1900018 F
## 25 8.5249399 G
## 23 1.2348623 H
## 17 4.4244789 I
## 9 3.2987474 J
## 18 4.9009779 K
## 6 9.0719775 L
## 4 5.8160276 M
## 16 0.1563902 N
## 1 NA O
## 22 5.2872677 P
## 20 1.9883285 Q
## 5 6.2884131 R
## 2 1.9579104 S
## 26 NA T
## 15 7.3510557 U
## 24 NA V
## 21 6.5683044 W
## 12 9.1761178 X
## 14 9.0666651 Y
## 3 7.7944596 Z
#calculate the column mean for the first variable.
mean(dFrame$my_unis, na.rm = TRUE)
## [1] 5.993397