Link to Home

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))
print(n_dims)
## [1] 5
# Create a vector of consecutive integers from 1 to n_dims^2.
next_vector <- seq(1:n_dims^2)
print(next_vector)
##  [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
# Use the sample function to randomly reshuffle these values.
sample(next_vector)
##  [1]  4 21  5  6 16 11 12 15 22  3 19 20 10 13  7  2  8 25 24  1 14 18 17 23  9
# Create a square matrix with these elements.
my_matrix <- matrix(next_vector, nrow = n_dims, byrow = TRUE)

# Print out the matrix.
print(my_matrix)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
## [3,]   11   12   13   14   15
## [4,]   16   17   18   19   20
## [5,]   21   22   23   24   25
# Find a function in r to transpose the matrix. I found the function transpose which is t(matrix).
transposed_matrix <- t(my_matrix)

# Print it out again and note how it has changed.
print(transposed_matrix)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    6   11   16   21
## [2,]    2    7   12   17   22
## [3,]    3    8   13   18   23
## [4,]    4    9   14   19   24
## [5,]    5   10   15   20   25
# Calculate the sum and the mean of the elements in the first row and the last row.

## Sum
### First row
sum(transposed_matrix[1,])
## [1] 55
### Last row
sum(transposed_matrix[n_dims,])
## [1] 75
## Mean
### First row
mean(transposed_matrix[1,])
## [1] 11
### Last row
mean(transposed_matrix[n_dims,])
## [1] 15
# Read about the eigen() function and use it on your matrix.
eigen_lists <- eigen(transposed_matrix, only.values = FALSE, EISPACK = FALSE)
print(eigen_lists)
## eigen() decomposition
## $values
## [1]  6.864208e+01+0.000000e+00i -3.642081e+00+0.000000e+00i
## [3]  4.257350e-15+0.000000e+00i -1.270981e-16+4.588876e-16i
## [5] -1.270981e-16-4.588876e-16i
## 
## $vectors
##              [,1]           [,2]           [,3]                  [,4]
## [1,] 0.3800509+0i -0.76703416+0i  0.54621260+0i  0.1175132+0.0459634i
## [2,] 0.4124552+0i -0.48590617+0i -0.27228461+0i  0.4017692+0.0065072i
## [3,] 0.4448594+0i -0.20477817+0i -0.66418830+0i -0.7435988+0.0000000i
## [4,] 0.4772637+0i  0.07634982+0i -0.03961996+0i -0.1881630-0.2033750i
## [5,] 0.5096680+0i  0.35747782+0i  0.42988027+0i  0.4124793+0.1509044i
##                       [,5]
## [1,]  0.1175132-0.0459634i
## [2,]  0.4017692-0.0065072i
## [3,] -0.7435988+0.0000000i
## [4,] -0.1881630+0.2033750i
## [5,]  0.4124793-0.1509044i
# Look carefully at the elements of $values and $vectors. What kind of numbers are these?
## complex numbers

# Dig in with the typeof() function to figure out their type.
typeof(eigen_lists $ values)
## [1] "complex"
typeof(eigen_lists $ vectors)
## [1] "complex"
# 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.
## Yep!

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_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_letters, which is a 26-element vector of all the lower-case letters in random order.

my_matrix <- matrix(runif(16), nrow = 4, byrow = TRUE)
print(my_matrix)
##           [,1]      [,2]        [,3]      [,4]
## [1,] 0.4054653 0.7375106 0.174965083 0.4534360
## [2,] 0.8456502 0.2812026 0.572756951 0.3106423
## [3,] 0.9558637 0.5536983 0.039618828 0.9878152
## [4,] 0.2598813 0.8529409 0.009607129 0.2044559
my_logical <- runif(100) < 0.5
print(my_logical)
##   [1]  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE
##  [13]  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [25]  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
##  [37] FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE FALSE
##  [49]  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE
##  [61]  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE
##  [73]  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
##  [85]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE
##  [97] FALSE FALSE  TRUE  TRUE
my_letters <- sample(letters[seq(1:26)])
print(my_letters)
##  [1] "l" "n" "j" "w" "r" "m" "c" "x" "e" "s" "y" "u" "g" "d" "i" "t" "o" "v" "a"
## [20] "f" "k" "q" "b" "z" "h" "p"
my_list <- c(my_matrix, my_logical, my_letters)
print(my_list)
##   [1] "0.405465261312202"   "0.845650246134028"   "0.955863736337051"  
##   [4] "0.259881342528388"   "0.737510577309877"   "0.281202607322484"  
##   [7] "0.553698347182944"   "0.852940906537697"   "0.174965082900599"  
##  [10] "0.572756950976327"   "0.0396188276354223"  "0.00960712903179228"
##  [13] "0.453436034033075"   "0.310642284341156"   "0.987815206171945"  
##  [16] "0.204455913277343"   "TRUE"                "FALSE"              
##  [19] "TRUE"                "TRUE"                "FALSE"              
##  [22] "FALSE"               "TRUE"                "TRUE"               
##  [25] "FALSE"               "FALSE"               "TRUE"               
##  [28] "FALSE"               "TRUE"                "TRUE"               
##  [31] "TRUE"                "TRUE"                "FALSE"              
##  [34] "FALSE"               "TRUE"                "FALSE"              
##  [37] "TRUE"                "TRUE"                "TRUE"               
##  [40] "TRUE"                "TRUE"                "FALSE"              
##  [43] "FALSE"               "FALSE"               "TRUE"               
##  [46] "TRUE"                "TRUE"                "TRUE"               
##  [49] "FALSE"               "TRUE"                "TRUE"               
##  [52] "TRUE"                "FALSE"               "TRUE"               
##  [55] "TRUE"                "FALSE"               "FALSE"              
##  [58] "TRUE"                "TRUE"                "FALSE"              
##  [61] "TRUE"                "FALSE"               "FALSE"              
##  [64] "FALSE"               "TRUE"                "TRUE"               
##  [67] "FALSE"               "FALSE"               "FALSE"              
##  [70] "FALSE"               "TRUE"                "TRUE"               
##  [73] "FALSE"               "FALSE"               "TRUE"               
##  [76] "FALSE"               "TRUE"                "FALSE"              
##  [79] "FALSE"               "TRUE"                "TRUE"               
##  [82] "TRUE"                "FALSE"               "TRUE"               
##  [85] "TRUE"                "FALSE"               "TRUE"               
##  [88] "FALSE"               "TRUE"                "TRUE"               
##  [91] "TRUE"                "FALSE"               "FALSE"              
##  [94] "TRUE"                "FALSE"               "FALSE"              
##  [97] "TRUE"                "TRUE"                "TRUE"               
## [100] "TRUE"                "TRUE"                "FALSE"              
## [103] "TRUE"                "FALSE"               "TRUE"               
## [106] "FALSE"               "TRUE"                "FALSE"              
## [109] "FALSE"               "TRUE"                "FALSE"              
## [112] "TRUE"                "FALSE"               "FALSE"              
## [115] "TRUE"                "TRUE"                "l"                  
## [118] "n"                   "j"                   "w"                  
## [121] "r"                   "m"                   "c"                  
## [124] "x"                   "e"                   "s"                  
## [127] "y"                   "u"                   "g"                  
## [130] "d"                   "i"                   "t"                  
## [133] "o"                   "v"                   "a"                  
## [136] "f"                   "k"                   "q"                  
## [139] "b"                   "z"                   "h"                  
## [142] "p"
# 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_new_list <- list(my_matrix[2,2], my_logical[2], my_letters[2])
print(my_new_list)
## [[1]]
## [1] 0.2812026
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "n"
# Use the typeof() function to confirm the underlying data types of each component in this list

typeof(my_new_list[[1]])
## [1] "double"
typeof(my_new_list[[2]])
## [1] "logical"
typeof(my_new_list[[3]])
## [1] "character"
# Combine the underlying elements from the new list into a single atomic vector with the c() function.
my_new_vector <- c(my_matrix[2,2], my_logical[2], my_letters[2])
print(my_new_vector)
## [1] "0.281202607322484" "FALSE"             "n"
# What is the data type of this vector?
typeof(my_new_vector)
## [1] "character"

Question 3

# Create a data frame with two variables (= columns) and 26 cases (= rows).
# 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.

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

my_data_frame <- data.frame(my_unis, my_letters)
print(my_data_frame)
##      my_unis my_letters
## 1  1.1727871          F
## 2  9.7666903          C
## 3  6.3208389          Z
## 4  6.9522760          I
## 5  8.7615724          P
## 6  6.4675606          X
## 7  9.9679205          D
## 8  7.2858485          Y
## 9  6.6427360          W
## 10 5.3580503          Q
## 11 9.8159379          H
## 12 7.8776554          M
## 13 0.8709377          A
## 14 8.4918757          E
## 15 0.3298827          R
## 16 4.6599161          U
## 17 0.3112522          B
## 18 3.1477240          T
## 19 1.1016741          O
## 20 4.7288303          K
## 21 0.9973000          N
## 22 7.5666292          V
## 23 5.0968635          J
## 24 0.7133149          L
## 25 8.8798862          S
## 26 9.7930725          G
# 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_data_frame[,1] <- replace(x = my_data_frame[,1], list = sample(nrow(my_data_frame),4), values = NA)

# for the first variable, write a single line of R code to identify which rows have the missing values.
which(is.na(my_data_frame))
## [1]  3  9 10 18
# for the second variable, sort it in alphabetical order
my_data_frame[,2] <- sort(my_data_frame[,2], decreasing = FALSE)
print(my_data_frame)
##      my_unis my_letters
## 1  1.1727871          A
## 2  9.7666903          B
## 3         NA          C
## 4  6.9522760          D
## 5  8.7615724          E
## 6  6.4675606          F
## 7  9.9679205          G
## 8  7.2858485          H
## 9         NA          I
## 10        NA          J
## 11 9.8159379          K
## 12 7.8776554          L
## 13 0.8709377          M
## 14 8.4918757          N
## 15 0.3298827          O
## 16 4.6599161          P
## 17 0.3112522          Q
## 18        NA          R
## 19 1.1016741          S
## 20 4.7288303          T
## 21 0.9973000          U
## 22 7.5666292          V
## 23 5.0968635          W
## 24 0.7133149          X
## 25 8.8798862          Y
## 26 9.7930725          Z
#calculate the column mean for the first variable
mean(complete.cases(my_data_frame[,1]))
## [1] 0.8461538