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

n_dims <- runif(1, min = 3, max = 11)
print(n_dims)
## [1] 7.582827
Create a vector of consecutive integers from 1 to n_dims2
vec_con<-1:(n_dims^2)
print(vec_con)
##  [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
## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## [51] 51 52 53 54 55 56 57
Use the sample function to randomly reshuffle these values.
vec_samp<-sample(vec_con)
print(vec_samp)
##  [1]  1 34 23 43 14 18 51 33 21 49 42 46 10  7  9 15 48 37 25 40 38 56 39 47 50
## [26] 20  3  6 45 29 30 36 32 35 12 27  8 55 22 44 28 19 41 53  2 13 16 11 57 31
## [51] 26 17  4 24 52 54  5
Create a square matrix with these elements.
m <- matrix(vec_samp, nrow = n_dims, ncol = n_dims)
## Warning in matrix(vec_samp, nrow = n_dims, ncol = n_dims): data length [57] is
## not a sub-multiple or multiple of the number of rows [7]
print(m)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    1   33    9   56   45   27   41
## [2,]   34   21   15   39   29    8   53
## [3,]   23   49   48   47   30   55    2
## [4,]   43   42   37   50   36   22   13
## [5,]   14   46   25   20   32   44   16
## [6,]   18   10   40    3   35   28   11
## [7,]   51    7   38    6   12   19   57
Find a function in r to transpose the matrix.
transposed_matrix<-t(m)
print(transposed_matrix)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
## [1,]    1   34   23   43   14   18   51
## [2,]   33   21   49   42   46   10    7
## [3,]    9   15   48   37   25   40   38
## [4,]   56   39   47   50   20    3    6
## [5,]   45   29   30   36   32   35   12
## [6,]   27    8   55   22   44   28   19
## [7,]   41   53    2   13   16   11   57
Calculate the sum and the mean of the elements in the first row and then the last row.
sum_first_row <- sum(transposed_matrix[1, ])
print(sum_first_row)
## [1] 184
mean_first_row <- mean(transposed_matrix[1, ])
print(mean_first_row)
## [1] 26.28571
sum_last_row <- sum(transposed_matrix[n_dims, ])
print(sum_last_row)
## [1] 193
mean_last_row <- mean(transposed_matrix[n_dims, ])
print(mean_last_row)
## [1] 27.57143
Read about the eigen() function and use it on your matrix
eigen_matrix <- eigen(transposed_matrix)
print(eigen_matrix$values)
## [1] 206.4520812+ 0.00000i -42.0415830+ 0.00000i  36.0707225+13.64676i
## [4]  36.0707225-13.64676i  22.9527086+ 0.00000i -22.3712528+ 0.00000i
## [7]  -0.1333988+ 0.00000i
print(eigen_matrix$vectors)
##               [,1]           [,2]                    [,3]
## [1,] -0.3378665+0i -0.70486405+0i  0.28858371+0.02192395i
## [2,] -0.3879549+0i  0.17661144+0i -0.34137898+0.14764595i
## [3,] -0.3904930+0i -0.35651104+0i -0.04922512-0.25889490i
## [4,] -0.4065137+0i  0.49583065+0i -0.11862771+0.20679474i
## [5,] -0.4012525+0i  0.11654236+0i -0.25963164+0.06931021i
## [6,] -0.3742594+0i  0.27821545+0i -0.21796296-0.23448749i
## [7,] -0.3413582+0i  0.08967138+0i  0.69058587+0.00000000i
##                         [,4]           [,5]           [,6]            [,7]
## [1,]  0.28858371-0.02192395i  0.35357642+0i -0.08886067+0i  0.383077242+0i
## [2,] -0.34137898-0.14764595i -0.47495397+0i  0.61052132+0i -0.717431031+0i
## [3,] -0.04922512+0.25889490i  0.03543112+0i -0.04010390+0i -0.270275284+0i
## [4,] -0.11862771-0.20679474i  0.13404280+0i -0.10803936+0i  0.349757772+0i
## [5,] -0.25963164-0.06931021i -0.45413088+0i -0.43990115+0i  0.002491901+0i
## [6,] -0.21796296+0.23448749i -0.30850569+0i  0.55064446+0i -0.147867198+0i
## [7,]  0.69058587+0.00000000i  0.57338380+0i -0.33070277+0i  0.348273451+0i
Dig in with the typeof() function to figure out their type.
typeof(eigen_matrix$values)
## [1] "complex"
typeof(eigen_matrix$vectors)
## [1] "complex"

2. Create a list with the following named elements:

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.2026923 0.14330438 0.8762692 0.4100841
## [2,] 0.7111212 0.23962942 0.7789147 0.8108702
## [3,] 0.1216919 0.05893438 0.7973088 0.6049333
## [4,] 0.2454885 0.64228826 0.4552745 0.6547239
A 100-element vector of TRUE or FALSE values
random_values <- runif(100)
my_logical <- random_values > 0.5
print(my_logical)
##   [1] FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE
##  [13] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
##  [25] FALSE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
##  [37]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
##  [49]  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE
##  [61]  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE
##  [73] FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE
##  [85] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE FALSE
##  [97]  TRUE FALSE  TRUE FALSE
A 26-element vector of all the lower-case letters in random order.
my_letters <- sample(letters)
print(my_letters)
##  [1] "z" "m" "b" "x" "s" "q" "w" "k" "c" "r" "n" "u" "y" "v" "h" "i" "t" "l" "j"
## [20] "g" "d" "e" "a" "f" "o" "p"
The list
my_list <- list(my_matrix = my_matrix, my_logical = my_logical, my_letters = my_letters)
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(my_matrix[2, 2], my_logical[2], my_letters[2])
print(my_list2)
## [[1]]
## [1] 0.2396294
## 
## [[2]]
## [1] FALSE
## 
## [[3]]
## [1] "m"
Use the typeof() function to confirm the underlying data types of each component in this list
sapply(my_list2, typeof)
## [1] "double"    "logical"   "character"
Combine the underlying elements from the new list into a single atomic vector with the c() function.
combined_vec <- c(my_list[[1]], my_list[[2]], my_list[[3]])
What is the data type of this vector?
typeof(combined_vec)
## [1] "character"

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
my_unis <- runif(26, min = 0, max = 10)
Call the second variable my_letters and fill it with 26 capital letters in random order.
my_letters <- sample(letters)
Create the data frame
my_data_frame <- data.frame(my_unis = my_unis, Letter = my_letters)
print(my_data_frame)
##     my_unis Letter
## 1  6.314202      h
## 2  3.900789      d
## 3  6.896278      q
## 4  6.894134      i
## 5  5.549006      u
## 6  4.296244      a
## 7  4.527201      t
## 8  3.064433      j
## 9  5.783539      n
## 10 9.103703      s
## 11 1.426041      w
## 12 4.150476      z
## 13 2.109258      e
## 14 4.287504      r
## 15 1.326900      f
## 16 4.600964      l
## 17 9.429571      b
## 18 7.619739      x
## 19 9.329098      o
## 20 4.706785      k
## 21 6.035881      g
## 22 4.849897      y
## 23 1.088063      p
## 24 2.477268      v
## 25 4.985145      c
## 26 3.728667      m
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$my_unis[sample(1:26, 4)] <- 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$my_unis))
## [1]  4  7 20 24
Re-order the entire data frame to arrange the second variable in alphabetical order
my_data_frame <- my_data_frame[order(my_data_frame$Letter), ]
print(my_data_frame)
##     my_unis Letter
## 6  4.296244      a
## 17 9.429571      b
## 25 4.985145      c
## 2  3.900789      d
## 13 2.109258      e
## 15 1.326900      f
## 21 6.035881      g
## 1  6.314202      h
## 4        NA      i
## 8  3.064433      j
## 20       NA      k
## 16 4.600964      l
## 26 3.728667      m
## 9  5.783539      n
## 19 9.329098      o
## 23 1.088063      p
## 3  6.896278      q
## 14 4.287504      r
## 10 9.103703      s
## 7        NA      t
## 5  5.549006      u
## 24       NA      v
## 11 1.426041      w
## 18 7.619739      x
## 22 4.849897      y
## 12 4.150476      z
Calculate the column mean for the first variable.
mean_my_unis <- mean(my_data_frame$my_unis, na.rm = TRUE)
print(mean_my_unis)
## [1] 4.994336